(doubleValueOfOne > 1.0) - gives me true; how to avoid that?

Hello,
My question is like in subject. Where doubleValueOfOne is calculated from operation:
0.4 + 0.4 + 0.4 - 1.0 +0.4 + 0.4 So on the end it’s 1.0.

I know it’s the case of not ideal precision of double.

So I suppose I could use something like:
double valueOfOne > 1.00000001

But I feel it’s not good solution. And I’ve heard on different machines there could be various precision of double. I am not sure if it’s true. But I need some good and smart solution.
Could anyone give some hint?
Thanks in advance for any help.
Best Regards

Floats are troubling you lately, aren’t they? :slight_smile:
To that question there are many many answers or approaches. But without knowing the context or the behavior you want to achieve, we can’t give you the best solution for your problem. Could you tell us about your specific application?

In general: there is something called eps, which is the smallest possible number representable with floats, basically the resolution of floating point if you want so. In most cases, you could use that :slight_smile:

1 Like

I’d say your best bet is to set your own precision for values: testing for things that are nearly equal!

Some C++ different implementations of this are found here . I’m sure there are may be JUCE library tools that can replace the standard library.

This way you can allow for a precision that is necessary for your application whilst eradicating the problem you post above.

danielrudrich. Yes it looks like float bothering me :slight_smile:
You are asking about context. Sorry it’s too complicated for me to explain (not worth your time and my time) :slight_smile: I am just still testing some strange variations of FFT, and I met described problem when tried to interpolate some zero padding FFT output. But I am even not sure what I want to do. I just like that :slight_smile:
I think I will try solutions from ford004 link. Thanks.

But please could you explain what is EPSILON in those solutions?

@pajczur Epsilon is the difference between 1.0 and your floating point number.

So if you look at the accepted example (Dmitriy): FLT_EPSILON is the difference in precision between 1 and the next biggest re-presentable number.

thanks

Yes, but that’s C - in C++ you should be using std::numeric_limits<double>::epsilon()

1 Like