Skip to content

Commit 23fc0e9

Browse files
committed
Numbers
1 parent 08b4c8a commit 23fc0e9

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,20 +473,28 @@ Format
473473

474474
Numbers
475475
-------
476+
### Built-in Types
476477
```python
477478
<int> = int(<float/str/bool>) # Or: math.floor(<float>)
478479
<float> = float(<int/str/bool>)
479480
<complex> = complex(real=0, imag=0) # Or: <real> + <real>j
480-
<Fraction> = fractions.Fraction(numerator=0, denominator=1)
481481
```
482482
* **`'int(<str>)'` and `'float(<str>)'` raise ValueError on malformed strings.**
483483

484+
### Special Types
485+
```python
486+
<Fraction> = fractions.Fraction(numerator=0, denominator=1)
487+
<Decimal> = decimal.Decimal(<int/float/str>)
488+
```
489+
* **Decimal numbers can be represented exactly, unlike floats where `'1.1 + 2.2 == 3.3000000000000003'`.**
490+
* **Their precision can be adjusted with `'decimal.getcontext().prec = <int>'`.**
491+
484492
### Basic Functions
485493
```python
486-
<num> = pow(<num>, <num>) # Or: <num> ** <num>
487-
<real> = abs(<num>)
488-
<int> = round(<real>)
489-
<real> = round(<real>, ±ndigits) # `round(126, -1) == 130`
494+
<num> = pow(<num>, <num>) # Or: <num> ** <num>
495+
<num> = abs(<num>)
496+
<int> = round(<num>)
497+
<num> = round(<num>, ±ndigits) # `round(126, -1) == 130`
490498
```
491499

492500
### Math

index.html

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -552,19 +552,27 @@
552552
{<span class="hljs-number">90</span>:b} <span class="hljs-comment"># '1011010'</span>
553553
</code></pre></div>
554554

555-
<div><h2 id="numbers"><a href="#numbers" name="numbers">#</a>Numbers</h2><pre><code class="python language-python hljs">&lt;int&gt; = int(&lt;float/str/bool&gt;) <span class="hljs-comment"># Or: math.floor(&lt;float&gt;)</span>
555+
<div><h2 id="numbers"><a href="#numbers" name="numbers">#</a>Numbers</h2><div><h3 id="builtintypes">Built-in Types</h3><pre><code class="python language-python hljs">&lt;int&gt; = int(&lt;float/str/bool&gt;) <span class="hljs-comment"># Or: math.floor(&lt;float&gt;)</span>
556556
&lt;float&gt; = float(&lt;int/str/bool&gt;)
557557
&lt;complex&gt; = complex(real=<span class="hljs-number">0</span>, imag=<span class="hljs-number">0</span>) <span class="hljs-comment"># Or: &lt;real&gt; + &lt;real&gt;j</span>
558-
&lt;Fraction&gt; = fractions.Fraction(numerator=<span class="hljs-number">0</span>, denominator=<span class="hljs-number">1</span>)
559-
</code></pre></div>
558+
</code></pre></div></div>
559+
560560

561561
<ul>
562562
<li><strong><code class="python hljs"><span class="hljs-string">'int(&lt;str&gt;)'</span></code> and <code class="python hljs"><span class="hljs-string">'float(&lt;str&gt;)'</span></code> raise ValueError on malformed strings.</strong></li>
563563
</ul>
564-
<div><h3 id="basicfunctions">Basic Functions</h3><pre><code class="python language-python hljs">&lt;num&gt; = pow(&lt;num&gt;, &lt;num&gt;) <span class="hljs-comment"># Or: &lt;num&gt; ** &lt;num&gt;</span>
565-
&lt;real&gt; = abs(&lt;num&gt;)
566-
&lt;int&gt; = round(&lt;real&gt;)
567-
&lt;real&gt; = round(&lt;real&gt;, ±ndigits) <span class="hljs-comment"># `round(126, -1) == 130`</span>
564+
<div><h3 id="specialtypes">Special Types</h3><pre><code class="python language-python hljs">&lt;Fraction&gt; = fractions.Fraction(numerator=<span class="hljs-number">0</span>, denominator=<span class="hljs-number">1</span>)
565+
&lt;Decimal&gt; = decimal.Decimal(&lt;int/float/str&gt;)
566+
</code></pre></div>
567+
568+
<ul>
569+
<li><strong>Decimal numbers can be represented exactly, unlike floats where <code class="python hljs"><span class="hljs-string">'1.1 + 2.2 == 3.3000000000000003'</span></code>.</strong></li>
570+
<li><strong>Their precision can be adjusted with <code class="python hljs"><span class="hljs-string">'decimal.getcontext().prec = &lt;int&gt;'</span></code>.</strong></li>
571+
</ul>
572+
<div><h3 id="basicfunctions">Basic Functions</h3><pre><code class="python language-python hljs">&lt;num&gt; = pow(&lt;num&gt;, &lt;num&gt;) <span class="hljs-comment"># Or: &lt;num&gt; ** &lt;num&gt;</span>
573+
&lt;num&gt; = abs(&lt;num&gt;)
574+
&lt;int&gt; = round(&lt;num&gt;)
575+
&lt;num&gt; = round(&lt;num&gt;, ±ndigits) <span class="hljs-comment"># `round(126, -1) == 130`</span>
568576
</code></pre></div>
569577

570578
<div><h3 id="math">Math</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> e, pi, inf, nan

0 commit comments

Comments
 (0)