You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dec128 corresponds to 34 significant digits in decimal:
julia>precision(Dec128, base=10)
34
Adding 10^-20 to 10^20 would require 41 significant digits. As a result, the 20th decimal place in the result gets rounded off to 0.
This is fundamental to how floating-point arithmetic, of any type, works — only ever have a finite number of significant digits.
To get 10^20 + 10^-20 exactly, you'd need to use a type with even more precision (more digits) than Dec128. You could use an arbitrary precision type like Julia's built-in BigFloat type:
julia>setprecision(100, base=10); # use roughly 100 decimal digits
julia>big"1e20"+big"1e-20"1.00000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000003e+20
Even this is not quite exact — see the 3 in the 80th digit — because BigFloat is a "binary" floating-point type, which represents integers times powers of 2 exactly (up to some number of binary digits), but not integers times powers of 10, so 1e-20 needs to be rounded to represent it as the nearest BigFloat. To do this operation exactly you'd need a high-precision decimal floating-point type, e.g. Decimals.jl.
Based on How can I get the exact result of 10^20 + 10^-20 in Python? , I have tried this
where it must be
"100000000000000000000.00000000000000000001"
Note that
Why a + b miss the fraction.
Regards;
The text was updated successfully, but these errors were encountered: