You can use Python decimal.Decimal with PyCall , but efficiency will be related to Python
Import the package:
julia> using PyCall julia> @pyimport decimal julia> const Dec = decimal.Decimal PyObject <class 'decimal.Decimal'>
Meta-definition operations (I think all of these definitions should be part of PyCall !):
julia> py_methods = Dict( :+ => :__add__, :* => :__mul__, :- => :__sub__, (:/) => :__truediv__ ) Dict{Symbol,Symbol} with 4 entries: :/ => :__truediv__ :+ => :__add__ :* => :__mul__ :- => :__sub__ julia> for (op, meth) in py_methods op = Expr(:quote, op) meth = Expr(:quote, meth) @eval Base.($op){T<:PyObject}(x::T, y::T) = x[$meth](y) end
Do the math with them:
julia> x = Dec("0.4") PyObject Decimal('0.4') julia> x * x PyObject Decimal('0.16') julia> x + x PyObject Decimal('0.8') julia> x - x PyObject Decimal('0.0') julia> x / x PyObject Decimal('1') julia> y = x + x * x / x - x PyObject Decimal('0.4')
Get the result:
julia> y[:to_eng_string]() |> float 0.4
source share