exact_number#
Utilities for using exact representations of real numbers.
This module contains ExactNumber
and ExactNumberInput
.
ExactNumber
’s are used as inputs for components that privacy guarantees
depend on. They are used because they can be exactly represented and manipulated using
SymPy, which avoids errors such as privacy
violations due to numerical imprecision, as well as more mundane user errors from the
limitations of floating point arithmetic. See
Handling Real Numbers for more information.
ExactNumberInput
’s are values which can be automatically converted into
ExactNumber
’s. They can be any of the following:
- Any
int
orfractions.Fraction
: >>> ExactNumber(5) 5 >>> ExactNumber(-1000000000000000) -1000000000000000 >>> ExactNumber(Fraction(1, 10)) 1/10 >>> ExactNumber(Fraction(127, 128)) 127/128
- Any
sympy.Expr
that meets all of the following criteria: No free symbols
>>> x = sp.symbols("x") >>> x_plus_1 = x + 1 >>> ExactNumber(x_plus_1) Traceback (most recent call last): core.exceptions.UnsupportedSympyExprError: x + 1 contains free symbols >>> ExactNumber(x_plus_1.subs(x, 3)) 4
No undefined functions
>>> f = sp.core.function.Function('f') >>> ExactNumber(f(2) + 1) Traceback (most recent call last): core.exceptions.UnsupportedSympyExprError: f(2) + 1 has an undefined function >>> f = sp.Lambda(x, x**2) >>> ExactNumber(f(2) + 1) 5
No floating point values
>>> ExactNumber(sp.Float(3.14)) Traceback (most recent call last): core.exceptions.UnsupportedSympyExprError: 3.14000000000000 is represented using floating point precision >>> ExactNumber(sp.Float(3.14) * (sp.Integer(2) ** sp.pi)) Traceback (most recent call last): core.exceptions.UnsupportedSympyExprError: 3.14*2**pi is invalid: 3.14000000000000 is represented using floating point precision >>> ExactNumber(sp.Rational("3.14") * (sp.Float(3) ** sp.pi)) Traceback (most recent call last): core.exceptions.UnsupportedSympyExprError: 157*3.0**pi/50 is invalid: Base of 3.0**pi is invalid: 3.00000000000000 is represented using floating point precision
Is a real number (or +/- infinity)
>>> i = sp.sqrt(sp.Integer(-1)) >>> ExactNumber(i) Traceback (most recent call last): core.exceptions.UnsupportedSympyExprError: I has an imaginary component >>> ExactNumber(1 + 2*i) Traceback (most recent call last): core.exceptions.UnsupportedSympyExprError: 1 + 2*I has an imaginary component >>> ExactNumber(i**2) -1 >>> ExactNumber(sp.oo) oo >>> ExactNumber(-sp.oo) -oo >>> ExactNumber(1 + sp.oo*i) Traceback (most recent call last): core.exceptions.UnsupportedSympyExprError: 1 + oo*I is invalid: oo*I is invalid: I has an imaginary component >>> ExactNumber(sp.oo + i) Traceback (most recent call last): core.exceptions.UnsupportedSympyExprError: oo + I is invalid: I has an imaginary component
- Any
str
that can be: Exactly interpreted as a Rational number or
Converted to a valid
sympy.Expr
bysympy.parsing.sympy_parser.parse_expr()
>>> ExactNumber("0.5") 1/2 >>> ExactNumber("0.123456789") 123456789/1000000000 >>> ExactNumber("2 + 7**2") 51 >>> ExactNumber("2 * pi**2") 2*pi**2 >>> ExactNumber("sqrt(5/3)") sqrt(15)/3 >>> ExactNumber("pi + I") Traceback (most recent call last): core.exceptions.UnsupportedSympyExprError: pi + I has an imaginary component >>> ExactNumber("x + 1") Traceback (most recent call last): core.exceptions.UnsupportedSympyExprError: x + 1 contains free symbols
- float(‘inf’) and -float(‘inf’) are allowed:
>>> ExactNumber(float('inf')) oo >>> ExactNumber(-float('inf')) -oo >>> ExactNumber(3.5) Traceback (most recent call last): core.exceptions.UnsupportedSympyExprError: Expected +/-float('inf'), not 3.5 Floating point values typically do not exactly represent the value they are intended to represent, and so are not automatically converted. See tmlt.core.utils.exact_number.from_float for more information.
- Finally
ExactNumber
’s are allowed: >>> ExactNumber(ExactNumber(3)) 3 >>> ExactNumber(ExactNumber("0.5")) 1/2
ExactNumber
’s support many common mathematical operations, and can be used in
combination with ExactNumberInput
’s.
Examples
>>> ExactNumber(1) + ExactNumber("0.5")
3/2
>>> ExactNumber(sp.Integer(7)) - 3
4
>>> ExactNumber(5) ** 2
25
>>> -2 ** ExactNumber(Fraction(1, 2))
-sqrt(2)
>>> 2 / ExactNumber(6)
1/3
Data#
- ExactNumberInput#
A type alias for exact representations of real numbers.
See
exact_number
for more information.
Classes#
An exact representation of a real number or +/- infinity. |
- class ExactNumber(value)#
An exact representation of a real number or +/- infinity.
See
exact_number
for more information and examples.Methods# Returns a
sympy.Expr
representation.Returns whether self represents an integer.
Returns whether self represents a finite number.
Returns self as a float.
Returns an
ExactNumber
from afloat
.Returns absolute value of self.
Returns the additive inverse of self.
Returns quotient from dividing self by other.
Returns quotient from dividing other by self.
Returns product of self and other.
Returns sum of self and other.
Returns difference of self and other.
Returns difference of other and self.
Returns power obtained by raising self to other.
Returns power obtained by raising other to self.
Returns True if other and self represent the same value.
Returns True if self is strictly less than other.
Returns True if self is less than or equal to other.
Returns True if self is strictly greater than other.
Returns True if self is greater than or equal to other.
- Parameters
value (ExactNumberInput) –
- __init__(value)#
Constructor.
- Parameters
value (
ExactNumber
|float
|int
|str
|Fraction
|Expr
Union
[ExactNumber
,float
,int
,str
,Fraction
,Expr
]) – AnExactNumberInput
that represents a real number or +/- infinity.
- property expr#
Returns a
sympy.Expr
representation.- Return type
sympy.Expr
- to_float(round_up)#
Returns self as a float.
- static from_float(value, round_up)#
Returns an
ExactNumber
from afloat
.Warning
Floating point values do not have the same algebraic properties as real numbers (For example, operations such as addition and multiplication are not associative or distributive). It is strongly recommended to use exact representations where possible and to only use this method when an exact representation is no longer needed.
- Parameters
value (float) – A
float
to convert to anExactNumber
.round_up (bool) – If True, returned value is greater than or equal to value. Otherwise, it is less than or equal to value.
- Return type
- __abs__()#
Returns absolute value of self.
- Return type
- __neg__()#
Returns the additive inverse of self.
- Return type
- __truediv__(other)#
Returns quotient from dividing self by other.
- Parameters
other (ExactNumberInput) –
- Return type
- __rtruediv__(other)#
Returns quotient from dividing other by self.
- Parameters
other (ExactNumberInput) –
- Return type
- __mul__(other)#
Returns product of self and other.
- Parameters
other (ExactNumberInput) –
- Return type
- __add__(other)#
Returns sum of self and other.
- Parameters
other (ExactNumberInput) –
- Return type
- __sub__(other)#
Returns difference of self and other.
- Parameters
other (ExactNumberInput) –
- Return type
- __rsub__(other)#
Returns difference of other and self.
- Parameters
other (ExactNumberInput) –
- Return type
- __pow__(other)#
Returns power obtained by raising self to other.
- Parameters
other (ExactNumberInput) –
- Return type
- __rpow__(other)#
Returns power obtained by raising other to self.
- Parameters
other (ExactNumberInput) –
- Return type
- __eq__(other)#
Returns True if other and self represent the same value.
- Parameters
other (Any) –
- Return type
- __lt__(other)#
Returns True if self is strictly less than other.
- Parameters
other (Any) –
- Return type
- __le__(other)#
Returns True if self is less than or equal to other.
- Parameters
other (Any) –
- Return type
- __gt__(other)#
Returns True if self is strictly greater than other.
- Parameters
other (Any) –
- Return type