Numbers

%reload_ext rubberize
from rubberize.config import config

Integers

Integers are whole numbers and are represented as such.

%%tap -g
0; 1; 312; 525_600
\( \displaystyle 0 \)
\( \displaystyle 1 \)
\( \displaystyle 312 \)
\( \displaystyle 525\,600 \)

Thousands 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\)
Tip

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_prec
2

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.

Note

@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_not
False
%%tap
69_420.134  # @float_format="SCI"
69_420.134  # @float_format="SCI" @use_e_not=True
\( \displaystyle 6.94 \times 10^{4} \)
\( \displaystyle 6.94\mathrm{E}{+04} \)

Large 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_digits will automatically switch to scientific notation for readability.
config.float_max_digits
15
  • 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_threshold
1e-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 value
\( \displaystyle 5.32 \times 10^{15} \)
A large number
\( \displaystyle 4.20 \times 10^{-9} \)
A small number
\( \displaystyle \cos \left( \frac{\pi}{2} \right) = 0.00 \)
Theoretically zero number
\( \displaystyle \operatorname{str} \left( \cos \left( \frac{\pi}{2} \right) \right) = \text{“6.123233995736766e-17”} \)
Actual value

Infinity and NaN

Special numbers like infinity and NaN are rendered:

%%tap
a = 1e308 * 2  # overflow result
-a
a * 0  # NaN
\( \displaystyle a = \left( 1.00 \times 10^{308} \right) \cdot 2 = \infty \)
overflow result
\( \displaystyle -a = -\infty \)
\( \displaystyle a \cdot 0 = \infty \cdot 0 = \text{NaN} \)
NaN

Decimals

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")
\( \displaystyle 0.0000011900 \)
\( \displaystyle 420.000000000069 \)
\( \displaystyle 43\,220.00000000001990 \)
\( \displaystyle 2.73 \times 10^{12} \)

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=True
\( \displaystyle 2.73 \times 10^{12} \)
\( \displaystyle 2.73\mathrm{E}{+12} \)

Infinity and NaN

Similar to floats, special Decimal numbers like infinity and NaN are rendered:

%%tap --grid
Decimal("inf")
Decimal("nan")
\( \displaystyle \infty \)
\( \displaystyle \text{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)
\( \displaystyle \frac{1}{2} \)
\( \displaystyle \frac{25\,647}{5\,000} \)
\( \displaystyle \frac{617}{2\,839} \)

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=True
\( \displaystyle 0.00 \)
\( \displaystyle \mathrm{i} \)
\( \displaystyle 123.40 + 456.70\,\mathrm{i} \)
\( \displaystyle \left( 1.34\mathrm{E}{+03} + 4.20\mathrm{E}{-02} \right)\,\mathrm{i} \)

Polar form

@use_polar specifies if complex numbers is rendered in its polar form:

config.use_polar
False
%%tap @use_polar=True
0j
1j
123.4 + 456.7j
complex(1337.42, 0.042)  # @float_format="SCI" @use_e_not=True
\( \displaystyle 0.00 \)
\( \displaystyle \mathrm{i} \)
\( \displaystyle 123.40 + 456.70\,\mathrm{i} \)
\( \displaystyle \left( 1.34\mathrm{E}{+03} + 4.20\mathrm{E}{-02} \right)\,\mathrm{i} \)

@use_polar_deg specifies if complex numbers in polar form will be rendered with phase angles converted to radians:

config.use_polar_deg
True
%%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=True
\( \displaystyle 0.00 \)
\( \displaystyle \mathrm{i} \)
\( \displaystyle 123.40 + 456.70\,\mathrm{i} \)
\( \displaystyle \left( 1.34\mathrm{E}{+03} + 4.20\mathrm{E}{-02} \right)\,\mathrm{i} \)

To render phase angles in radians, set @use_polar_deg config option (a bool, default is True) to False.

Footnotes

  1. 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↩︎

  2. with @float_prec=3↩︎

  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↩︎