Reputation: 509
I need to be able to calculate more fractional digits than the Float
type seems to allow: 15.
iex(19)> Float.round(1/7, 15)
0.142857142857143
iex(20)> Float.round(1/7, 16)
** (FunctionClauseError) no function clause matching in Float.round/2
(elixir) lib/float.ex:163: Float.round(0.14285714285714285, 16)
According to the Pragmatic Programmers' Programming Elixir book:
Floats are IEEE 754 double precision, giving them about 16 digits of accuracy
Is there anything similar to Ruby's BigDecimal
which allows arbitrary-precision floating point decimal arithmetic? Or some way to achieve that easily in Elixir?
Upvotes: 0
Views: 866
Reputation: 9109
There is the Decimal library which allows arbitrary precision, but won't have all the available math routines that you'd get with Float.
https://hex.pm/packages/decimal
Decimal works by attempting to preserve as much of the initial precision of the input numbers as possible and only truncates to the precision specified when there are unavoidable round off errors.
Upvotes: 4