%reload_ext rubberize
from rubberize.config import configNumbers
Integers
Integers are whole numbers and are represented as such.
%%tap -g
0; 1; 312; 525_600Thousands Separator
@thousands_separator specifies the character used to separate thousands in rendered integers:
config.thousands_separator' '
It can be changed to any of these values:
| Separator | Value | Rendered as |
|---|---|---|
| None | "" |
\(\displaystyle 525600\) |
| Thin space | " " |
\(\displaystyle 525\,600\) |
| Comma | "," |
\(\displaystyle 525{,}600\) |
| Dot | "." |
\(\displaystyle 525{.}600\) |
| Apostrophe | "'" |
\(\displaystyle 525\text{’}600\) |
According to BIPM (1948)1, numbers may be divided in groups of three using a thin space to facilitate reading. Other separators are regional conventions.
Floats
Floats are rendered according to the configured float formatting options.
Formats
@float_format specifies the appearance of float values:
config.float_format'FIX'
It can be changed to any of these values:
| Number Format | Value | Rendered as2 | Explanation |
|---|---|---|---|
| Fixed-point notation | "FIX" |
\(\displaystyle 69\,420.134\) | Displays the number with a fixed number of decimals. |
| General notation | "GEN" |
\(\displaystyle 6.94 \times 10^{4}\) | Automatically switches between fixed-point and scientific notation based on value size. |
| Scientific notation | "SCI" |
\(\displaystyle 6.942 \times 10^{4}\) | Represents the number in scientific notation with a single digit before the decimal point. |
| Engineering notation | "ENG" |
\(\displaystyle 69.420 \times 10^{3}\) | Similar to scientific notation but uses exponents that are multiples of three. |
Display Precision
@float_prec specifies the displayed precision of the rendered float:
config.float_prec2
If @float_format="FIX", @float_prec is the number of decimal digits to be displayed. Otherwise, it is the number of significant figures to be displayed.
Shortcut keywords for setting the format precision are: @0, @1, … @6.
@float_prec only controls how floats are formatted, not how they are computed. Even if you format a number to show only, say, 2 decimal places, Python still retains all the extra digits internally.
Thousands Separator
@thousands_separator also controls the character used to separate thousands in rendered floats.
Decimal Marker
@decimal_marker specifies the character used for the decimal marker in rendered floats:
config.decimal_marker'.'
In accordance with BIPM (2003)3, decimal markers can be:
| Decimal Marker | Value | Rendered as |
|---|---|---|
| Dot | "." |
\(\displaystyle 1\,069.42\) |
| Comma | "," |
\(\displaystyle 1\,069{,}42\) |
E notation
@use_e_not specifies whether to use E notation or the standard scientific notation format:
config.use_e_notFalse
%%tap
69_420.134 # @float_format="SCI"
69_420.134 # @float_format="SCI" @use_e_not=TrueLarge and Small Numbers
The following rules are applied when @float_format="FIX":
- Large numbers: Numbers with more digits than the value set by
@float_max_digitswill automatically switch to scientific notation for readability.
config.float_max_digits15
Small numbers: If the number of decimal digits after rounding is less than the precision specified by
@float_prec, the number will also be formatted in scientific notation.For very small numbers close to zero: If the absolute value is less than
@zero_float_threshold, it will be rendered as zero. This behavior accounts for floating-point precision errors in computations, such as those involving trigonometric functions.
config.zero_float_threshold1e-12
%%tap
import math
531_8008_000_000_000.0 # A large number
0.0000000042 # A small number
math.cos(math.pi / 2) # Theoretically zero number
str(math.cos(math.pi / 2)) # Actual valueInfinity and NaN
Special numbers like infinity and NaN are rendered:
%%tap
a = 1e308 * 2 # overflow result
-a
a * 0 # NaNDecimals
Decimal objects are rendered from their exact decimal representation rather than being formatted as floating-point values that are rounded to @float_prec.
%%tap
from decimal import Decimal
Decimal("0.0000011900")
Decimal("420.000000000069")
Decimal("43_220.00000000001990")
Decimal("273e10")Thousands Separator
@thousands_separator also controls the character used to separate thousands in rendered Decimal objects.
Decimal Marker
@decimal_marker also controls the character used for the decimal marker in rendered Decimal objects.
E notation
@use_e_not also controls the use of E notation in rendered Decimal objects that are represented as scientific notation.
%%tap --grid
Decimal("2.73e12")
Decimal("2.73e12") # @use_e_not=TrueInfinity and NaN
Similar to floats, special Decimal numbers like infinity and NaN are rendered:
%%tap --grid
Decimal("inf")
Decimal("nan")Fractions
Fraction objects are rendered using their exact rational representation, with the numerator and denominator formatted as integers.
%%tap --grid
from fractions import Fraction
Fraction("1/2")
Fraction("5.1294")
Fraction(1_234, 5_678)Complex Numbers
Rubberize uses \(\mathrm{i}\) notation for the imaginary unit. Formatting rules for floats also apply to both real and imaginary parts of a complex number.
If the complex number is 1j, it is rendered as the imaginary unit \(\mathrm{i}\). If the complex number is 0j, it is rendered as zero.
%%tap
0j
1j
123.4 + 456.7j
complex(1337.42, 0.042) # @float_format="SCI" @use_e_not=TruePolar form
@use_polar specifies if complex numbers is rendered in its polar form:
config.use_polarFalse
%%tap @use_polar=True
0j
1j
123.4 + 456.7j
complex(1337.42, 0.042) # @float_format="SCI" @use_e_not=True@use_polar_deg specifies if complex numbers in polar form will be rendered with phase angles converted to radians:
config.use_polar_degTrue
%%tap @use_polar=True @use_polar_deg=False
0j
1j
123.4 + 456.7j
complex(1337.42, 0.042) # @float_format="SCI" @use_e_not=TrueTo render phase angles in radians, set @use_polar_deg config option (a bool, default is True) to False.
Footnotes
CGPM, “Resolution 7 of the 9th General Conference on Weights and Measures (1948),” Bureau International des Poids et Mesures (BIPM). Available: https://www.bipm.org/en/committees/cg/cgpm/9-1948/resolution-7↩︎
with
@float_prec=3↩︎CGPM, “Resolution 10 of the 22nd General Conference on Weights and Measures (2003),” Bureau International des Poids et Mesures (BIPM). Available: https://www.bipm.org/en/committees/cg/cgpm/22-2003/resolution-10↩︎