A logarithmic number system for Julia.
Provides the signed Logarithmic
and unsigned ULogarithmic
types for representing real
numbers on a logarithmic scale.
This is useful when numbers are too big or small to fit accurately into a Float64
and you
only really care about magnitude.
For example, it can be useful to represent probabilities in this form, and you don't need to worry about getting zero when multiplying many of them together.
pkg> add LogarithmicNumbers
julia> using LogarithmicNumbers
julia> ULogarithmic(2.7)
exp(0.9932517730102834)
julia> float(ans)
2.7
julia> x = exp(ULogarithmic, 1000) - exp(ULogarithmic, 998)
exp(999.8545865421312)
julia> float(x) # overflows
Inf
julia> log(x)
999.8545865421312
ULogarithmic{T}
represents a non-negative real number by its logarithm of typeT
.Logarithmic{T}
represents a real number by its absolute value as aULogarithmic{T}
and a sign bit.LogFloat64
is an alias forLogarithmic{Float64}
. There are alsoULogFloat16
,ULogFloat32
,ULogFloat64
,LogFloat16
, andLogFloat32
.
ULogarithmic(x)
andLogarithmic(x)
represent the numberx
.exp(ULogarithmic, logx)
representsexp(logx)
, andlogx
can be huge. Use this when you already know the logarithmlogx
of your numberx
.
- Arithmetic:
+
,-
,*
,/
,^
,inv
,prod
,sum
,sqrt
,cbrt
,fourthroot
. - Ordering:
==
,<
,≤
,cmp
,isless
,isequal
,sign
,signbit
,abs
. - Logarithm:
log
,log2
,log10
,log1p
. These are returned as the base (non-logarithmic) type. - Conversion:
float
,unsigned
,signed
,widen
,big
. These also operate on types. - Special values:
zero
,one
,typemin
,typemax
. - Predicates:
iszero
,isone
,isinf
,isfinite
,isnan
. - IO:
show
,write
,read
. - Random:
rand(ULogarithmic)
is a random number in the unit interval. - 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(ULogarithmic, 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
.
On Julia 1.9+, if you load ForwardDiff.jl, you should be allowed to compute
- derivatives of functions involving
exp(Logarithmic, x)
- derivatives of functions evaluated at
Logarithmic(x)