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 BasicType

>=: (%, %) -> Boolean

from PartialOrder

>: (%, %) -> Boolean

from PartialOrder

^: (%, %) -> %

from ElementaryFunctionCategory

^: (%, Fraction Integer) -> %

from RadicalCategory

^: (%, Integer) -> %

from DivisionRing

^: (%, NonNegativeInteger) -> %

from MagmaWithUnit

^: (%, PositiveInteger) -> %

from Magma

~=: (%, %) -> Boolean

from BasicType

abs: % -> %

from OrderedRing

acos: % -> %

from ArcTrigonometricFunctionCategory

acosh: % -> %

from ArcHyperbolicFunctionCategory

acot: % -> %

from ArcTrigonometricFunctionCategory

acoth: % -> %

from ArcHyperbolicFunctionCategory

acsc: % -> %

from ArcTrigonometricFunctionCategory

acsch: % -> %

from ArcHyperbolicFunctionCategory

annihilate?: (%, %) -> Boolean

from Rng

antiCommutator: (%, %) -> %

from NonAssociativeSemiRng

asec: % -> %

from ArcTrigonometricFunctionCategory

asech: % -> %

from ArcHyperbolicFunctionCategory

asin: % -> %

from ArcTrigonometricFunctionCategory

asinh: % -> %

from ArcHyperbolicFunctionCategory

associates?: (%, %) -> Boolean

from EntireRing

associator: (%, %, %) -> %

from NonAssociativeRng

atan: % -> %

from ArcTrigonometricFunctionCategory

atan: (%, %) -> %

atan(x, y) computes the arc tangent from x with phase y.

atanh: % -> %

from ArcHyperbolicFunctionCategory

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 -> %

from Algebra 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 DoubleFloat x to a Float.

cos: % -> %

from TrigonometricFunctionCategory

cosh: % -> %

from HyperbolicFunctionCategory

cot: % -> %

from TrigonometricFunctionCategory

coth: % -> %

from HyperbolicFunctionCategory

csc: % -> %

from TrigonometricFunctionCategory

csch: % -> %

from HyperbolicFunctionCategory

D: % -> %

from DifferentialRing

D: (%, NonNegativeInteger) -> %

from DifferentialRing

decreasePrecision: Integer -> PositiveInteger

from FloatingPointSystem

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: % -> %

from ElementaryFunctionCategory

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

factor: % -> Factored %

from UniqueFactorizationDomain

float: (Integer, Integer) -> %

from FloatingPointSystem

float: (Integer, Integer, PositiveInteger) -> %

from FloatingPointSystem

floor: % -> %

from RealNumberSystem

fractionPart: % -> %

from RealNumberSystem

gcd: (%, %) -> %

from GcdDomain

gcd: List % -> %

from GcdDomain

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

increasePrecision: Integer -> PositiveInteger

from FloatingPointSystem

inv: % -> %

from DivisionRing

latex: % -> String

from SetCategory

lcm: (%, %) -> %

from GcdDomain

lcm: List % -> %

from GcdDomain

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 for x to base 10.

log10: () -> %

log10() returns ln 10: 2.3025809299....

log2: % -> %

log2(x) computes the logarithm for x to base 2.

log2: () -> %

log2() returns ln 2, i.e. 0.6931471805....

log: % -> %

from ElementaryFunctionCategory

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) normalizes x 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, with n digits displayed after the decimal point.

outputFloating: () -> Void

outputFloating() sets the output mode to floating (scientific) notation, i.e. mantissa * 10 exponent is displayed as 0.mantissa E exponent.

outputFloating: NonNegativeInteger -> Void

outputFloating(n) sets the output mode to floating (scientific) notation with n 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 with n significant digits displayed.

outputSpacing: NonNegativeInteger -> NonNegativeInteger

outputSpacing(n) inserts an underscore after n (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: () -> %

from TranscendentalFunctionCategory

plenaryPower: (%, PositiveInteger) -> %

from NonAssociativeAlgebra %

positive?: % -> Boolean

from OrderedRing

precision: () -> PositiveInteger

from FloatingPointSystem

precision: PositiveInteger -> PositiveInteger

from FloatingPointSystem

prime?: % -> Boolean

from UniqueFactorizationDomain

principalIdeal: List % -> Record(coef: List %, generator: %)

from PrincipalIdealDomain

quo: (%, %) -> %

from EuclideanDomain

rationalApproximation: (%, NonNegativeInteger) -> Fraction Integer

rationalApproximation(f, n) computes a rational approximation r to f with relative error < 10^(-n).

rationalApproximation: (%, NonNegativeInteger, NonNegativeInteger) -> Fraction Integer

rationalApproximation(f, n, b) computes a rational approximation r to f 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, when y \~= 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: % -> %

from TrigonometricFunctionCategory

sech: % -> %

from HyperbolicFunctionCategory

set_output_mode: (String, Integer) -> Void

set_output_mode(mode, precision) sets output mode and precision.

shift: (%, Integer) -> %

shift(x, n) adds n to the exponent of float x.

sign: % -> Integer

from OrderedRing

sin: % -> %

from TrigonometricFunctionCategory

sinh: % -> %

from HyperbolicFunctionCategory

sizeLess?: (%, %) -> Boolean

from EuclideanDomain

smaller?: (%, %) -> Boolean

from Comparable

sqrt: % -> %

from RadicalCategory

squareFree: % -> Factored %

from UniqueFactorizationDomain

squareFreePart: % -> %

from UniqueFactorizationDomain

subtractIfCan: (%, %) -> Union(%, failed)

from CancellationAbelianMonoid

tan: % -> %

from TrigonometricFunctionCategory

tanh: % -> %

from HyperbolicFunctionCategory

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

AbelianGroup

AbelianMonoid

AbelianSemiGroup

Algebra %

Algebra Fraction Integer

Approximate

arbitraryExponent

arbitraryPrecision

ArcHyperbolicFunctionCategory

ArcTrigonometricFunctionCategory

BasicType

BiModule(%, %)

BiModule(Fraction Integer, Fraction Integer)

CancellationAbelianMonoid

canonicalsClosed

canonicalUnitNormal

CharacteristicZero

CoercibleFrom Fraction Integer

CoercibleFrom Integer

CoercibleTo DoubleFloat

CoercibleTo OutputForm

CommutativeRing

CommutativeStar

Comparable

ConvertibleTo DoubleFloat

ConvertibleTo Float

ConvertibleTo InputForm

ConvertibleTo Pattern Float

ConvertibleTo String

DifferentialRing

DivisionRing

ElementaryFunctionCategory

EntireRing

EuclideanDomain

Field

FloatingPointSystem

GcdDomain

Hashable

HyperbolicFunctionCategory

IntegralDomain

LeftModule %

LeftModule Fraction Integer

LeftOreRing

Magma

MagmaWithUnit

Module %

Module Fraction Integer

Monoid

NonAssociativeAlgebra %

NonAssociativeAlgebra Fraction Integer

NonAssociativeRing

NonAssociativeRng

NonAssociativeSemiRing

NonAssociativeSemiRng

noZeroDivisors

OpenMath

OrderedAbelianGroup

OrderedAbelianMonoid

OrderedAbelianSemiGroup

OrderedCancellationAbelianMonoid

OrderedRing

OrderedSet

PartialOrder

PatternMatchable Float

PrincipalIdealDomain

RadicalCategory

RealConstant

RealNumberSystem

RetractableTo Fraction Integer

RetractableTo Integer

RightModule %

RightModule Fraction Integer

Ring

Rng

SemiGroup

SemiRing

SemiRng

SetCategory

TranscendentalFunctionCategory

TrigonometricFunctionCategory

TwoSidedRecip

UniqueFactorizationDomain

unitsKnown