Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Numeric types and precision

Select an integer, binary float, decimal or rational representation to match the calculation.

Version: Python 3.14; repository image, production runtime not attested

Syntax

Fraction(1, 10) + Fraction(2, 10)

Select an integer, binary float, decimal or rational representation to match the calculation.

Integers have arbitrary precision subject to resources, while binary floats cannot exactly represent most decimal fractions. Decimal built from strings preserves those decimal inputs and arithmetic uses the current context.

Fraction reduces exact numerator-denominator pairs and rejects a zero denominator.

Examples

from decimal import Decimal
from fractions import Fraction
print((2 ** 100).bit_length(), 0.1 + 0.2 == 0.3)
print(Decimal("0.1") + Decimal("0.2"), Fraction(1, 10) + Fraction(2, 10))
try:
    Fraction(1, 0)
except ZeroDivisionError:
    print("zero denominator")

Result

101 False
0.3 3/10
zero denominator

Fraction reduces exact numerator-denominator pairs and rejects a zero denominator.

Sources