
Boost Users : 
Subject: Re: [Boostusers] [Interval] Help debugging compiler optimization
From: Paul A. Bristow (pbristow_at_[hidden])
Date: 20180308 14:25:51
From: Boostusers [mailto:boostusersbounces_at_[hidden]] On Behalf Of degski via Boostusers
Sent: 08 March 2018 13:03
To: boostusers_at_[hidden]
Cc: degski
Subject: Re: [Boostusers] [Interval] Help debugging compiler optimization
On 8 March 2018 at 02:36, Tim van Erven via Boostusers <boostusers_at_[hidden]> wrote:
My (somewhat naive) view of compilers is that they can only do optimization that has no visible effects on the behavior of the program.
Here is what the different floating point behaviors (strict/fast/precise) mean with VC (I realize you're on linux, but the same must count/exist on linux as these are CPU features). As you can see, it's intricate.
In general what you are trying to do is not possible (that way), you'll need to allow for some delta between values that if the difference is smaller that delta, this qualifies them as being equal. So far so good so. We have our friends FLT_EPSILON and DBL_EPSILON from the STL <http://en.cppreference.com/w/cpp/types/climits> . These don't help you much, the delta defined by those two is the difference between 2 consecutive floats on the interval from 1. to 2.. So towards 0., the EPSILON is getting smaller, over 2. it's getting bigger, suggesting different approaches are needed for different parts of the real numbers line.
Daniel Lemire <https://lemire.me/blog/2017/02/28/howmanyfloatingpointnumbersareintheinterval01/> has written about this subject and has suggested solutions in the past (search the blog for more articles pertaining to this subject), for only to (partially) revert those opinions later. It's quite a problem really. In case you are really working with fractions (as in your example 3 * (1/3)), it would be better to use a fractions library.
The above assumes IEEE754 floating point numbers <https://en.wikipedia.org/wiki/Floatingpoint_arithmetic#IEEE_754:_floating_point_in_modern_computers> .
Boost.math function float_distance
http://www.boost.org/doc/libs/1_66_0/libs/math/doc/html/math_toolkit/next_float/float_distance.html
will show you the â€˜(signed) numbers of bits differentâ€™ between two values (or any floating point type)
or more formally
â€œReturns the distance between a and b: the result is always a signed integer value (stored in floatingpoint type FPT) representing the number of distinct representations between a and b.â€
This more helpful than std::numeric_limits<FPT>::epsilon()
You can also use the related functions float_next and float_prior (and next_after) to show how the values are shown in decimal.
But if you really, really want rationals, then you probably should use the rationals library?
http://www.boost.org/doc/libs/1_66_0/libs/rational/index.html
HTH
Paul
 Paul A. Bristow Prizet Farmhouse Kendal UK LA8 8AB +44 (0) 1539 561830
Boostusers list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net