BasedReal

class kanon.units.radices.BasedReal(*args, remainder=Decimal('0'), sign=1)[source] [edit on github]

Bases: PreciseNumber, Real

Abstract class to represent a number in a specific base.

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
  • multiple int representing an integral number in current base

>>> Sexagesimal(21, 1, 3)
21,01,03 ;
Parameters:
  • remainder (Decimal, optional) – When a computation requires more precision than the precision of this number, we store a Decimal remainder to keep track of it, defaults to 0.0

  • sign (int, optional) – The sign of this number, defaults to 1

Raises:

ValueError – Unexpected or illegal arguments

Return type:

BasedReal

Attributes Summary

base

Base of this BasedReal, (integer part, fractional part)

decimal

This BasedReal converted as a Decimal

imag

Real numbers have no imaginary component.

left

Tuple of values at integer positions

mixed

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

right

Tuple of values at fractional positions

sign

Sign of this BasedReal

significant

Precision of this BasedReal (equals to length of fractional part)

Methods Summary

ceil([significant])

conjugate()

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

minimize_precision()

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 yields BasedReal numbers.

resize(significant)

Resizes and returns a new BasedReal object to the specified precision

shift(i)

Shifts number to the left (-) or the right (+).

sqrt([iteration])

Returns the square root, using Babylonian method

subunit_quantity(i)

Convert this sexagesimal to the integer value from the specified fractional point.

to_fraction()

return:

this BasedReal as a Fraction object.

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

base

Base of this BasedReal, (integer part, fractional part)

>>> Sexagesimal(1).base
((..., 60, ...), (..., 60, ...))
Return type:

RadixBase

decimal

This BasedReal converted as a Decimal

>>> 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, …]

mixed
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')
Returns:

Remainder of this BasedReal

Return type:

Decimal

right

Tuple of values at fractional positions

>>> Sexagesimal((1,2,3), (4,5)).right
(4, 5)
Return type:

Tuple[int, …]

sign

Sign of this BasedReal

>>> Sexagesimal(1,2,3, sign=-1).sign
-1
Return type:

Sign

significant

Precision of this BasedReal (equals to length of fractional part)

>>> Sexagesimal((1,2,3), (4,5)).significant
2
Return type:

int

Methods Documentation

ceil(significant: int | None = None) TBasedReal[source] [edit on github]
conjugate() [edit on github]

Conjugate is a no-op for Reals.

equals(other: 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
Parameters:

other (BasedReal) – The other BasedReal to be compared with the first

Returns:

True if both objects are the same, False otherwise

Return type:

bool

floor(significant: int | None = None) TBasedReal[source] [edit on github]
classmethod from_decimal(dec: Decimal, significant: int) TBasedReal[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) TBasedReal[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: Fraction, significant: int | None = None) TBasedReal[source] [edit on github]
Parameters:
  • fraction – a Fraction object

  • significant – significant precision desired

Returns:

a BasedReal object computed from a Fraction

classmethod from_int(value: int, significant=0) TBasedReal[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() TBasedReal[source] [edit on github]

Removes unnecessary zeros from fractional part of this BasedReal.

Returns:

Minimized BasedReal

classmethod one(significant=0) TBasedReal[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 yields BasedReal numbers.

Yield:

BasedReal integers.

resize(significant: int) TBasedReal[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) TBasedReal[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:

BasedReal

sqrt(iteration: int | None = None) TBasedReal[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
Parameters:

i (int) – Rank of the subunit to compute from.

Returns:

Integer amount of the specified subunit.

Return type:

int

to_fraction() Fraction[source] [edit on github]
Returns:

this BasedReal as a Fraction object.

truncate(significant: int | None = None) TBasedReal[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) TBasedReal[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