3 ms·
There are algorithms that behave correctly but require intermediate Inf or NaN to work. If I recall there is one related to finding bounding box intersections i
by ChrisLomont 5y ago
There are algorithms that behave correctly but require intermediate Inf or NaN to work. If I recall there is one related to finding bounding box intersections in ray tracing. I know when I introduce such code into a codebase I put a large "DO NOT CHANGE THIS..." style comment with an explanation so some enterprising programmer doesn't put some if statements in to remove what they think is an error, when it is not.
I'd suspect the same is true for NaN. These things were introduced for reasons. If you don't have a deep knowledge of why, don't assume everyone else is the same.
Edit: I found examples using a NaN and Inf intermediates to get a large speed increase [1,2].
[1] https://tavianator.com/2015/ray_box_nan.html https://tavianator.com/2015/ray_box_nan.html
[2] https://tavianator.com/2011/ray_box.html https://tavianator.com/2011/ray_box.html
- dkersten 5y agoSure, of course they have uses otherwise why would they exist. But I would argue that the examples you have aren’t the common case — most people’s floating point calculations do not rely on these special values and seeing them denotes something went wrong.
- ChrisLomont 5y agoOf course they're not the common case, but the parent wanted examples where they are used in calculations that do have nice answers. So they are useful. They are more useful than most people realize that simply assume they only mark errors. And without them in your case, you would not be alerted that a calculation went wrong. So even there they are very useful. Not using them correctly is like ignoring file API errors in your programs - sure they are rare, but you need to understand and handle them if you want to make good software.
- dkersten 5y agoAh right, fair enough! They are, indeed, good examples of the usefulness.