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:

$ erl
 Eshell V5.7.4 (abort with ^G)
1> 0.1 + 0.2.
 0.30000000000000004

You got a really unexpected result:

0.30000000000000004

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

Round Types

Value = 0.1 + 0.2.
 0.30000000000000004
round(Value). % Will round to integer
 0
round(Value * 10000000) / 10000000.
 0.3
io:format(“~f~n”, [Value]). 0.300000
ok
io:format(“~.14f~n”, [Value]) 0.30000000000000
ok
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.