Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Floating Point Related Idioms #325

Open
louy2 opened this issue Oct 25, 2022 · 1 comment
Open

Floating Point Related Idioms #325

louy2 opened this issue Oct 25, 2022 · 1 comment
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
Projects

Comments

@louy2
Copy link

louy2 commented Oct 25, 2022

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 using hypot ("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 and mul_add (aka fma, "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.

@simonsan simonsan added A-idiom Area: Idioms C-enhancement Category: Enhancements to content C-addition Category: Adding new content, something that didn't exist in the repository before labels Oct 25, 2022
@simonsan
Copy link
Collaborator

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.

Agreed. Though the focus of the interpreter article is not really on that, I would agree that it would be worth to have a new article about the topic within idioms.

Would you be willing to write that? You seem to be deep in the topic here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
Content
Awaiting triage
Development

No branches or pull requests

2 participants