FloatΒΆ
float.spad line 1 [edit on github]
Float implements arbitrary precision floating point arithmetic. The number of significant digits of each operation can be set to an arbitrary value (the default is 20 decimal digits). The operation float(mantissa, exponent, base) for integer mantissa
, exponent
specifies the number mantissa
* base ^ exponent
The underlying representation for floats is binary not decimal. The implications of this are described below. The model adopted is that arithmetic operations are rounded to to nearest unit in the last place, that is, accurate to within 2^(-bits). Also, the elementary functions and constants are accurate to one unit in the last place. A float is represented as a record of two integers, the mantissa and the exponent. The base of the representation is binary, hence a Record(m: mantissa, e: exponent)
represents the number m * 2 ^ e
. Though it is not assumed that the underlying integers are represented with a binary base, the code will be most efficient when this is the the case (this is true
in most implementations of Lisp). The decision to choose the base to be binary has some unfortunate consequences. First, decimal numbers like 0.3 cannot be represented exactly. Second, there is a further loss of accuracy during conversion to decimal for output. To compensate for this, if d
digits of precision are specified, 1 + ceiling(log2(10^d))
bits are used. Two numbers that are displayed identically may therefore be not equal. On the other hand, a significant efficiency loss would be incurred if we chose to use a decimal base when the underlying integer base is binary. Algorithms used: For the elementary functions, the general approach is to apply identities so that the taylor series can be used, and, so that it will converge within O( sqrt n )
steps. For example, using the identity exp(x) = exp(x/2)^2
, we can compute exp(1/3)
to n
digits of precision as follows. We have exp(1/3) = exp(2 ^ (-sqrt s) / 3) ^ (2 ^ sqrt s)
. The taylor series will converge in less than sqrt n
steps and the exponentiation requires sqrt n
multiplications for a total of 2 sqrt n
multiplications. Assuming integer multiplication costs O( n^2 )
the overall running time is O( sqrt(n) n^2 )
. This approach is the best known approach for precisions up to about 10, 000 digits at which point the methods of Brent which are O( log(n) n^2 )
become competitive. Note also that summing the terms of the taylor series for the elementary functions is done using integer operations. This avoids the overhead of floating point operations and results in efficient code at low precisions. This implementation makes no attempt to reuse storage, relying on the underlying system to do spadgloss{garbage collection}. I
estimate that the efficiency of this package at low precisions could be improved by a factor of 2 if in-place operations were available. Running times: in the following, n
is the number of bits of precision *
, /
, sqrt
, pi
, exp1
, log2
, log10
: `` O( n^2 )`` exp
, log
, sin
, atan
: `` O( sqrt(n) n^2 )`` The other elementary functions are coded in terms of the ones above.
- 0: %
from AbelianMonoid
- 1: %
from MagmaWithUnit
- *: (%, %) -> %
from Magma
- *: (%, Fraction Integer) -> %
from RightModule Fraction Integer
- *: (Fraction Integer, %) -> %
from LeftModule Fraction Integer
- *: (Integer, %) -> %
from AbelianGroup
- *: (NonNegativeInteger, %) -> %
from AbelianMonoid
- *: (PositiveInteger, %) -> %
from AbelianSemiGroup
- +: (%, %) -> %
from AbelianSemiGroup
- -: % -> %
from AbelianGroup
- -: (%, %) -> %
from AbelianGroup
- /: (%, %) -> %
from Field
- /: (%, Integer) -> %
from FloatingPointSystem
- <=: (%, %) -> Boolean
from PartialOrder
- <: (%, %) -> Boolean
from PartialOrder
- >=: (%, %) -> Boolean
from PartialOrder
- >: (%, %) -> Boolean
from PartialOrder
- ^: (%, %) -> %
- ^: (%, Fraction Integer) -> %
from RadicalCategory
- ^: (%, Integer) -> %
from DivisionRing
- ^: (%, NonNegativeInteger) -> %
from MagmaWithUnit
- ^: (%, PositiveInteger) -> %
from Magma
- abs: % -> %
from OrderedRing
- acos: % -> %
- acosh: % -> %
- acot: % -> %
- acoth: % -> %
- acsc: % -> %
- acsch: % -> %
- annihilate?: (%, %) -> Boolean
from Rng
- antiCommutator: (%, %) -> %
- asec: % -> %
- asech: % -> %
- asin: % -> %
- asinh: % -> %
- associates?: (%, %) -> Boolean
from EntireRing
- associator: (%, %, %) -> %
from NonAssociativeRng
- atan: % -> %
- atan: (%, %) -> %
atan(x, y)
computes the arc tangent fromx
with phasey
.
- atanh: % -> %
- base: () -> PositiveInteger
from FloatingPointSystem
- bits: () -> PositiveInteger
from FloatingPointSystem
- bits: PositiveInteger -> PositiveInteger
from FloatingPointSystem
- ceiling: % -> %
from RealNumberSystem
- characteristic: () -> NonNegativeInteger
from NonAssociativeRing
- coerce: % -> %
from Algebra %
- coerce: % -> DoubleFloat
from CoercibleTo DoubleFloat
- coerce: % -> OutputForm
from CoercibleTo OutputForm
- coerce: Fraction Integer -> %
- coerce: Integer -> %
from NonAssociativeRing
- commutator: (%, %) -> %
from NonAssociativeRng
- convert: % -> DoubleFloat
from ConvertibleTo DoubleFloat
- convert: % -> Float
from ConvertibleTo Float
- convert: % -> InputForm
from ConvertibleTo InputForm
- convert: % -> Pattern Float
from ConvertibleTo Pattern Float
- convert: % -> String
from ConvertibleTo String
- convert: DoubleFloat -> %
convert(x)
converts a DoubleFloatx
to a Float.
- cos: % -> %
- cosh: % -> %
- cot: % -> %
- coth: % -> %
- csc: % -> %
- csch: % -> %
- D: % -> %
from DifferentialRing
- D: (%, NonNegativeInteger) -> %
from DifferentialRing
- differentiate: % -> %
from DifferentialRing
- differentiate: (%, NonNegativeInteger) -> %
from DifferentialRing
- digits: () -> PositiveInteger
from FloatingPointSystem
- digits: PositiveInteger -> PositiveInteger
from FloatingPointSystem
- divide: (%, %) -> Record(quotient: %, remainder: %)
from EuclideanDomain
- euclideanSize: % -> NonNegativeInteger
from EuclideanDomain
- exp1: () -> %
exp1()
returns exp 1:2.7182818284...
.
- exp: % -> %
- exponent: % -> Integer
from FloatingPointSystem
- expressIdealMember: (List %, %) -> Union(List %, failed)
from PrincipalIdealDomain
- exquo: (%, %) -> Union(%, failed)
from EntireRing
- extendedEuclidean: (%, %) -> Record(coef1: %, coef2: %, generator: %)
from EuclideanDomain
- extendedEuclidean: (%, %, %) -> Union(Record(coef1: %, coef2: %), failed)
from EuclideanDomain
- float: (Integer, Integer) -> %
from FloatingPointSystem
- float: (Integer, Integer, PositiveInteger) -> %
from FloatingPointSystem
- floor: % -> %
from RealNumberSystem
- fractionPart: % -> %
from RealNumberSystem
- gcdPolynomial: (SparseUnivariatePolynomial %, SparseUnivariatePolynomial %) -> SparseUnivariatePolynomial %
from GcdDomain
- get_output_mode: () -> Record(mode: String, prec: Integer)
get_output_mode()
returns current output mode and precision
- hash: % -> SingleInteger
from Hashable
- hashUpdate!: (HashState, %) -> HashState
from Hashable
- inv: % -> %
from DivisionRing
- latex: % -> String
from SetCategory
- lcmCoef: (%, %) -> Record(llcm_res: %, coeff1: %, coeff2: %)
from LeftOreRing
- leftPower: (%, NonNegativeInteger) -> %
from MagmaWithUnit
- leftPower: (%, PositiveInteger) -> %
from Magma
- leftRecip: % -> Union(%, failed)
from MagmaWithUnit
- log10: % -> %
log10(x)
computes the logarithm forx
to base 10.
- log10: () -> %
log10()
returnsln 10
:2.3025809299...
.
- log2: % -> %
log2(x)
computes the logarithm forx
to base 2.
- log2: () -> %
log2()
returnsln 2
, i.e.0.6931471805...
.
- log: % -> %
- mantissa: % -> Integer
from FloatingPointSystem
- max: (%, %) -> %
from OrderedSet
- max: () -> % if
from FloatingPointSystem
- min: (%, %) -> %
from OrderedSet
- min: () -> % if
from FloatingPointSystem
- multiEuclidean: (List %, %) -> Union(List %, failed)
from EuclideanDomain
- negative?: % -> Boolean
from OrderedRing
- norm: % -> %
from RealNumberSystem
- normalize: % -> %
normalize(x)
normalizesx
at current precision.
- nthRoot: (%, Integer) -> %
from RadicalCategory
- OMwrite: % -> String
from OpenMath
- OMwrite: (%, Boolean) -> String
from OpenMath
- OMwrite: (OpenMathDevice, %) -> Void
from OpenMath
- OMwrite: (OpenMathDevice, %, Boolean) -> Void
from OpenMath
- one?: % -> Boolean
from MagmaWithUnit
- opposite?: (%, %) -> Boolean
from AbelianMonoid
- order: % -> Integer
from FloatingPointSystem
- outputFixed: () -> Void
outputFixed()
sets the output mode to fixed point notation; the output will contain a decimal point.
- outputFixed: NonNegativeInteger -> Void
outputFixed(n)
sets the output mode to fixed point notation, withn
digits displayed after the decimal point.
- outputFloating: () -> Void
outputFloating()
sets the output mode to floating (scientific) notation, i.e.mantissa * 10 exponent
is displayed as0.mantissa E exponent
.
- outputFloating: NonNegativeInteger -> Void
outputFloating(n)
sets the output mode to floating (scientific) notation withn
significant digits displayed after the decimal point.
- outputGeneral: () -> Void
outputGeneral()
sets the output mode (default mode) to general notation; numbers will be displayed in either fixed or floating (scientific) notation depending on the magnitude.
- outputGeneral: NonNegativeInteger -> Void
outputGeneral(n)
sets the output mode to general notation withn
significant digits displayed.
- outputSpacing: NonNegativeInteger -> NonNegativeInteger
outputSpacing(n)
inserts an underscore aftern
(default 10) digits on output; outputSpacing(0) means no underscores are inserted. Returns old setting.
- patternMatch: (%, Pattern Float, PatternMatchResult(Float, %)) -> PatternMatchResult(Float, %)
from PatternMatchable Float
- pi: () -> %
- plenaryPower: (%, PositiveInteger) -> %
from NonAssociativeAlgebra %
- positive?: % -> Boolean
from OrderedRing
- precision: () -> PositiveInteger
from FloatingPointSystem
- precision: PositiveInteger -> PositiveInteger
from FloatingPointSystem
- principalIdeal: List % -> Record(coef: List %, generator: %)
from PrincipalIdealDomain
- quo: (%, %) -> %
from EuclideanDomain
- rationalApproximation: (%, NonNegativeInteger) -> Fraction Integer
rationalApproximation(f, n)
computes a rational approximationr
tof
with relative error< 10^(-n)
.
- rationalApproximation: (%, NonNegativeInteger, NonNegativeInteger) -> Fraction Integer
rationalApproximation(f, n, b)
computes a rational approximationr
tof
with relative error< b^(-n)
, that is|(r-f)/f| < b^(-n)
.
- recip: % -> Union(%, failed)
from MagmaWithUnit
- relerror: (%, %) -> %
relerror(x, y)
computes the absolute value of(x - y)/y
, wheny \~= 0
.
- rem: (%, %) -> %
from EuclideanDomain
- retract: % -> Fraction Integer
from RetractableTo Fraction Integer
- retract: % -> Integer
from RetractableTo Integer
- retractIfCan: % -> Union(Fraction Integer, failed)
from RetractableTo Fraction Integer
- retractIfCan: % -> Union(Integer, failed)
from RetractableTo Integer
- rightPower: (%, NonNegativeInteger) -> %
from MagmaWithUnit
- rightPower: (%, PositiveInteger) -> %
from Magma
- rightRecip: % -> Union(%, failed)
from MagmaWithUnit
- round: % -> %
from RealNumberSystem
- sample: %
from AbelianMonoid
- sec: % -> %
- sech: % -> %
- set_output_mode: (String, Integer) -> Void
set_output_mode(mode, precision)
sets outputmode
and precision.
- shift: (%, Integer) -> %
shift(x, n)
addsn
to the exponent of floatx
.
- sign: % -> Integer
from OrderedRing
- sin: % -> %
- sinh: % -> %
- sizeLess?: (%, %) -> Boolean
from EuclideanDomain
- smaller?: (%, %) -> Boolean
from Comparable
- sqrt: % -> %
from RadicalCategory
- squareFree: % -> Factored %
- squareFreePart: % -> %
- subtractIfCan: (%, %) -> Union(%, failed)
- tan: % -> %
- tanh: % -> %
- toString: % -> String
from FloatingPointSystem
- toString: (%, NonNegativeInteger) -> String
from FloatingPointSystem
- truncate: % -> %
from RealNumberSystem
- unit?: % -> Boolean
from EntireRing
- unitCanonical: % -> %
from EntireRing
- unitNormal: % -> Record(unit: %, canonical: %, associate: %)
from EntireRing
- wholePart: % -> Integer
from RealNumberSystem
- zero?: % -> Boolean
from AbelianMonoid
Algebra %
ArcTrigonometricFunctionCategory
BiModule(%, %)
BiModule(Fraction Integer, Fraction Integer)
CoercibleFrom Fraction Integer
Module %
NonAssociativeAlgebra Fraction Integer
OrderedCancellationAbelianMonoid
RetractableTo Fraction Integer