Comparing doubles (LT, GE, GT...)

126 views
Skip to first unread message

A. Robert S.

unread,
Dec 22, 2014, 2:50:38 AM12/22/14
to cppu...@googlegroups.com
I am courious what others are doing in case you want to check for a double to be within a certain range. We tried
 
DOUBLES_EQUAL(0.25, double_value, 0.25) // actually testing positive double for <= 0.5

In case of failure, the failure message is not exactly clear on what was intended or what went wrong.
 
We also tried, obviously:
 
CHECK(double_value <= 0.5)

This is clear, but you have to go into the debugger if you want to know exactly what was the failing value.
 
Essentially, we need to test floats for falling into a certain range (all within 0.0f 1.0 in this particular case), rather than specifying an expected value with a tolerance. We would also like useful output, such as:
 
0.5013 < 0.5 is not true

Actually, coming up with a meaningful failure message also isn't as easy as it might seem.
 
Any ideas?
 
Robert

Ryan Hartlage

unread,
Dec 22, 2014, 5:39:59 PM12/22/14
to cppu...@googlegroups.com
I don't use doubles on my projects so I was confused about how this could be an issue.  What we currently have is

    expected <0.25> but was <0.75> threshold used is <0.25>

I think the confusing part of the current error message is that the epsilon is referred to as the 'threshold'.  It wasn't clear at all that this was the threshold for the quantity abs(a - b) until I actually looked at the code.

Interval notation or tolerances seem much more natural to me:

    expected <0.25 +/-0.25> but was <0.75>

    expected <[0, 0.5]> but was <0.75>

How do you feel about either of these?

Ryan


--
You received this message because you are subscribed to the Google Groups "cpputest" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cpputest+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

A. Robert S.

unread,
Dec 23, 2014, 4:36:44 AM12/23/14
to cppu...@googlegroups.com
I like the second one, as it clearly states the range within which we expect the value to fall.
 
I will try to implement this as a macro in our tests.
 
Robert
Reply all
Reply to author
Forward
0 new messages