BasedReal
- class kanon.units.radices.BasedReal(*args, remainder=Decimal('0'), sign=1)[source] [edit on github]
Bases:
kanon.units.precision.PreciseNumber
,numbers.Real
Abstract class allowing to represent a value in a specific
RadixBase
. Each time a newRadixBase
object is recorded, a new class inheriting from BasedReal is created and recorded inradix_registry
. TheRadixBase
to be used will be placed in the class attributebase
- Class attributes:
base : A
RadixBase
object (will be attributed dynamically to the children inheriting this class)
Constructs a number with a given radix.
Arguments:
>>> Sexagesimal("-2,31;12,30") -02,31 ; 12,30
2
Sequence[int]
representing integral part and fractional part
>>> Sexagesimal((2,31), (12,30), sign=-1) -02,31 ; 12,30 >>> Sexagesimal([2,31], [12,30]) 02,31 ; 12,30
a
BasedReal
with a significant number of digits,
>>> Sexagesimal(Sexagesimal("-2,31;12,30"), 1) -02,31 ; 12 |r0.5
>>> Sexagesimal(21, 1, 3) 21,01,03 ;
- Parameters
- Raises
ValueError – Unexpected or illegal arguments
- Return type
Attributes Summary
RadixBase
of this BasedRealReal numbers have no imaginary component.
Tuple of values at integer positions
Real numbers are their real component.
When a computation requires more significant figures than the precision of this number, we store a
Decimal
remainder to keep track of itTuple of values at fractional positions
Sign of this
BasedReal
Precision of this
BasedReal
(equals to length of fractional part)Methods Summary
ceil
([significant])Conjugate is a no-op for Reals.
equals
(other)Tests strict equivalence between this BasedReal and another
floor
([significant])from_decimal
(dec, significant)Class method to produce a new BasedReal object from a Decimal number
from_float
(floa, significant[, ...])Class method to produce a new BasedReal object from a floating number
from_fraction
(fraction[, significant])- param fraction
a
Fraction
object
from_int
(value[, significant])Class method to produce a new BasedReal object from an integer number
Removes unnecessary zeros from fractional part of this BasedReal.
one
([significant])Class method to produce a unit number of the specified precision
range
()Range generator, equivalent to
range
builtin but yieldsBasedReal
numbers.resize
(significant)Resizes and returns a new
BasedReal
object to the specified precisionshift
(i)Shifts number to the left (-) or the right (+).
sqrt
([iteration])Returns the square root, using Babylonian method
Convert this sexagesimal to the integer value from the specified fractional point.
truncate
([significant])Truncate this BasedReal object to the specified precision
zero
([significant])Class method to produce a zero number of the specified precision
Attributes Documentation
- decimal
This
BasedReal
converted as aDecimal
>>> Sexagesimal((1,2,3), (15,36)).decimal Decimal('3723.26')
- Return type
Decimal
- imag
Real numbers have no imaginary component.
- left
Tuple of values at integer positions
>>> Sexagesimal(1,2,3).left (1, 2, 3)
- Return type
Tuple[int, …]
- real
Real numbers are their real component.
- remainder
When a computation requires more significant figures than the precision of this number, we store a
Decimal
remainder to keep track of it>>> Sexagesimal(1,2,3, remainder=Decimal("0.2")).remainder Decimal('0.2')
- right
Tuple of values at fractional positions
>>> Sexagesimal((1,2,3), (4,5)).right (4, 5)
- Return type
Tuple[int, …]
- significant
Precision of this
BasedReal
(equals to length of fractional part)>>> Sexagesimal((1,2,3), (4,5)).significant 2
- Return type
Methods Documentation
- ceil(significant: Optional[int] = None) kanon.units.radices.BasedReal [source] [edit on github]
- conjugate() [edit on github]
Conjugate is a no-op for Reals.
- equals(other: kanon.units.radices.BasedReal) bool [source] [edit on github]
Tests strict equivalence between this BasedReal and another
>>> Sexagesimal("1,2;3").equals(Sexagesimal("1,2;3")) True >>> Sexagesimal("1,2;3").equals(Sexagesimal("1,2;3,0")) False
- floor(significant: Optional[int] = None) kanon.units.radices.BasedReal [source] [edit on github]
- classmethod from_decimal(dec: decimal.Decimal, significant: int) kanon.units.radices.BasedReal [source] [edit on github]
Class method to produce a new BasedReal object from a Decimal number
>>> Sexagesimal.from_decimal(Decimal('0.1'), 4) 00 ; 06,00,00,00
- Parameters
dec – floating value of the number
significant – precision of the number
- Returns
a new BasedReal object
- classmethod from_float(floa: float, significant: int, remainder_threshold: float = 0.999999) kanon.units.radices.BasedReal [source] [edit on github]
Class method to produce a new BasedReal object from a floating number
>>> Sexagesimal.from_float(1/3, 4) 00 ; 20,00,00,00
- Parameters
floa – floating value of the number
significant – precision of the number
- Returns
a new BasedReal object
- classmethod from_fraction(fraction: fractions.Fraction, significant: Optional[int] = None) kanon.units.radices.BasedReal [source] [edit on github]
- classmethod from_int(value: int, significant=0) kanon.units.radices.BasedReal [source] [edit on github]
Class method to produce a new BasedReal object from an integer number
>>> Sexagesimal.from_int(12, 4) 12 ; 00,00,00,00
- Parameters
value – integer value of the number
significant – precision of the number
- Returns
a new BasedReal object
- minimize_precision() kanon.units.radices.BasedReal [source] [edit on github]
Removes unnecessary zeros from fractional part of this BasedReal.
- Returns
Minimized BasedReal
- classmethod one(significant=0) kanon.units.radices.BasedReal [source] [edit on github]
Class method to produce a unit number of the specified precision
>>> Sexagesimal.one(5) 01 ; 00,00,00,00,00
- Parameters
significant – desired precision
- Returns
a unit number
- classmethod range(stop: int) Generator[BasedReal, None, None] [source] [edit on github]
- classmethod range(start: int, stop: int, step=1) Generator[BasedReal, None, None]
Range generator, equivalent to
range
builtin but yieldsBasedReal
numbers.- Yield
BasedReal
integers.
- resize(significant: int) kanon.units.radices.BasedReal [source] [edit on github]
Resizes and returns a new
BasedReal
object to the specified precision>>> n = Sexagesimal('02, 02; 07, 23, 55, 11, 51, 21, 36') >>> n 02,02 ; 07,23,55,11,51,21,36 >>> n.remainder Decimal('0') >>> n1 = n.resize(4) >>> n1.right (7, 23, 55, 11) >>> n1.remainder Decimal('0.8560000000000000000000000000') >>> n1.resize(7) 02,02 ; 07,23,55,11,51,21,36
- Parameters
significant – Number of desired significant positions
- Returns
Resized
BasedReal
- shift(i: int) kanon.units.radices.BasedReal [source] [edit on github]
Shifts number to the left (-) or the right (+). Prefer using >> and << operators (right-shift and left-shift).
>>> Sexagesimal(3).shift(-1) 03,00 ; >>> Sexagesimal(3).shift(2) 00 ; 00,03
- Parameters
i – Amount to shift this BasedReal
- Returns
Shifted number
- Return type
- sqrt(iteration: Optional[int] = None) kanon.units.radices.BasedReal [source] [edit on github]
Returns the square root, using Babylonian method
- Parameters
iteration (Optional[int], optional) – Number of iterations, defaults to the significant number
- subunit_quantity(i: int) int [source] [edit on github]
Convert this sexagesimal to the integer value from the specified fractional point.
>>> number = Sexagesimal("1,0;2,30")
Amount of minutes in
number
>>> number.subunit_quantity(1) 3602
Amount of zodiacal signs in
number
>>> number.subunit_quantity(-1) 1
- to_fraction() fractions.Fraction [source] [edit on github]
- truncate(significant: Optional[int] = None) kanon.units.radices.BasedReal [source] [edit on github]
Truncate this BasedReal object to the specified precision
>>> n = Sexagesimal('02, 02; 07, 23, 55, 11, 51, 21, 36') >>> n 02,02 ; 07,23,55,11,51,21,36 >>> n = n.truncate(3); n 02,02 ; 07,23,55 >>> n = n.resize(7); n 02,02 ; 07,23,55,00,00,00,00
- Parameters
n – Desired significant positions
- Returns
Truncated BasedReal
- classmethod zero(significant=0) kanon.units.radices.BasedReal [source] [edit on github]
Class method to produce a zero number of the specified precision
>>> Sexagesimal.zero(7) 00 ; 00,00,00,00,00,00,00
- Parameters
significant – desired precision
- Returns
a zero number