REQUIRE fails on floating point assertion but the value is correct

29 views
Skip to first unread message

Ruben Di Battista

unread,
Aug 20, 2018, 12:41:43 PM8/20/18
to CATCH
I'm validating a floating point method that should return 0 for a particular case
```
    SECTION("Test 1-ring gauss curvature") {
        REQUIRE(std::abs(point0.G()) == Approx(0));
    }
```
but when I run the catch test executable, It fails the assert, with:
```
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test_HG is a Catch v2.3.0 host application.
Run with -? for options

-------------------------------------------------------------------------------
Test on a simple roof-shaped PolyData
  Test 1-ring gauss curvature
-------------------------------------------------------------------------------
/PhD/HG/tests/test_roof.cxx:99
...............................................................................

/PhD/HG/tests/test_roof.cxx:100: FAILED:
  REQUIRE( std::abs(point0.G()) == Approx(0.0) )
with expansion:
  0.0 == Approx( 0.0 )

===============================================================================
test cases:  1 |  0 passed | 1 failed
assertions: 13 | 12 passed | 1 failed
```

I stepped in the execution with GDB, and indeed `point0.G()` is something in the order of magnitude of `1e-14`.

How can I further investigate the cause for the failure?

Martin Hořeňovský

unread,
Aug 23, 2018, 9:15:22 AM8/23/18
to CATCH
The simple answer is that the function didn't return exactly 0, but something close to 0. By default, Approx has a little bit of relative tolerance and no absolute tolerance, and relative tolerance breaks down at 0, for 2 simple causes

1) 0 times anything is still a 0
2) Anything +/- times less than 1 cannot become 0.

What you need to do is to decide if you have some absolute tolerance for your results, and if yes, set Approx::margin appropriately.

Martin Hořeňovský

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

Ruben Di Battista

unread,
Aug 24, 2018, 11:17:08 AM8/24/18
to martin.h...@gmail.com, catch...@googlegroups.com
Dear Martin, 

thank you. `Approx::margin` was exactly what I was looking for! 

Thanks!
--
          _  
-.     .´  |∞∞∞∞
  ',  ;    |∞∞∞∞∞∞
    ˜˜     |∞∞∞∞∞∞∞∞∞ RdB 
    ,.,    |∞∞∞∞∞∞
  .'   '.  |∞∞∞∞
-'       `'

Reply all
Reply to author
Forward
0 new messages