What Every Programmers Should Know About Floating-Point Arithmetic — Erlang
Erlang case — Why don’t my numbers add up? So, try this sample at your console:
Eshell V5.7.4 (abort with ^G)
1> 0.1 + 0.2.
You got a really unexpected result:
Maybe you asked for help on some forum and got pointed to a *long article with lots of formulas* that didn’t see to help with your problem. Well, this site is here to:
- Explain concisely why you get that unexpected result
- Tell you know to deal with this problem
- If you’re interested, provide in-depth explanations of why floating point number have to work like that and what other problems can arise.
You must look at this site.
Floating-Point Cheat Sheets for Erlang
Value = 0.1 + 0.2.
round(Value). % Will round to integer
round(Value * 10000000) / 10000000.
io:format(“~f~n”, [Value]). 0.300000
io:format(“~.14f~n”, [Value]) 0.30000000000000