Floating Point Related Idioms #325
Labels
A-idiom
Area: Idioms
C-addition
Category: Adding new content, something that didn't exist in the repository before
C-enhancement
Category: Enhancements to content
Floating point numbers are known to be an imperfect model of real numbers, causing rounding errors in calculations and difficulty for equality comparison.
For example, the Euclidean length example in the Interpreter chapter using
sqrt
has an average error of 2.9 bits for 4-dimension and 13.9 bits for 2-dimension, with more and bigger errors (over 56-bit errors) happening at points very close to the axes. This can be improved by usinghypot
("hypotenus") instead. (Herbie page for 4D Euclid length) (Herbie page for 2D Euclid length)Similarly, taking the angle between two 2-dimentional vectors has an average error of 34.3 bits, again with more and bigger errors (64-bit errors) closer to the axes, can be improved with
hypot
andmul_add
(akafma
, "fused multiply add") to cut the average error down to 18.9 bits. (Herbie page for angle between vectors)(The error figures above are taken from the results of Herbie project.)
A related problem is that nominally equal floating point expressions often do not compare equal due to rounding errors. This necessitates something like the crate
float-cmp
.These are not Rust specific idioms per se, but I feel like this is a good place to raise awareness of these errors and list Rust specific solutions.
The text was updated successfully, but these errors were encountered: