A package for working with huge or tiny numbers beyond the dynamic range of ordinary floating point numbers.
Exports the number type Huge{T} <: Real
which is a lot like a floating point number,
except it has an exponentially larger range.
You can use this for calculating extremely small probabilities, which would normally underflow a float, or extremely large quantities which would normally overflow.
To convert a number x
to a Huge
number:
- Do
convert(Huge, x)
orHuge(x)
. - If you already know
ix = hlog(x)
thenhexp(Huge, ix)
is faster. - If you already know
lx = log(x)
thenexp(Huge, lx)
is faster.
The following operations are implemented and accurate:
- Arithmetic:
+
,-
,*
,/
,^
,inv
. - Ordering:
==
,<
,cmp
,isequal
,isless
,sign
,signbit
,abs
. - Logarithm:
log
,log2
,log10
]. - Conversion:
float
,widen
,big
. - Special values:
zero
,one
,typemin
,typemax
. - Predicates:
iszero
,isone
,isinf
,isfinite
,isnan
. - IO:
show
,read
,write
. - Misc:
nextfloat
,prevfloat
,hash
. - Note: Any functions not mentioned here might be inaccurate.
It is natural to use this package in conjunction with other packages which return
logarithms. The general pattern is that you can use exp(Huge, logfunc(args...))
instead of func(args...)
to get the answer as a logarithmic number. Here are some
possibilities for func
:
- StatsFuns.jl:
normpdf
,normcdf
,normccdf
, plus equivalents for other distributions. - Distributions.jl:
pdf
,cdf
,ccdf
. - SpecialFunctions.jl:
gamma
,factorial
,beta
,erfc
,erfcx
.
Relationship to LogarithmicNumbers
Whereas a logarithmic number stores log(x)
, we store a different quantity hlog(x)
.
This is the inverse of hexp(x)
, which has the following nice properties:
- It is an increasing, continuous, differentiable, invertible function on the real numbers.
- It grows exponentially for large
x
and shrinks exponentially for smallx
. hexp(x) = x
forx
in-Inf
,-1
,0
,1
andInf
.-hexp(x) = hexp(-x)
.1/hexp(x) = hexp(1/x)
.
The crucial difference between log(x)
and hlog(x)
is that the former is only valid
for positive numbers. This means that a logrithmic number requires an extra sign bit if you
need to store a sign, whereas a Huge{T}
can be negative provided T
can be negative.
The other properties mean that many arthmetic and comparison operations are trivial to implement - often much simpler than with logarithmic numbers.
hexp(x)
is defined as:
exp(x - 1)
ifx ≥ 1
exp(1 - 1/x)
ifx ≥ 0
-exp(1 + 1/x)
ifx ≥ -1
-exp(-x - 1)
otherwise