Skip to content
This repository has been archived by the owner on Feb 3, 2025. It is now read-only.

Build warnings - comparison between doubles #459

Closed
osrf-migration opened this issue Jan 29, 2013 · 5 comments
Closed

Build warnings - comparison between doubles #459

osrf-migration opened this issue Jan 29, 2013 · 5 comments

Comments

@osrf-migration
Copy link

Original report (archived issue) by Ian Chen (Bitbucket: Ian Chen, GitHub: iche033).


MsgFactory test spits out warnings on unsafe floating point comparison using the == operator which is what gtest uses inside EXPECT_EQ.

A way around this it to do a subtraction between the two and check if the abs value is less than some epsilon.

#!c++

In file included from /home/ian/depot/gazebo/gazebo/msgs/test/MsgFactory.cc:18:0:
/home/ian/depot/gazebo/test/gtest/include/gtest/gtest.h: In function ‘testing::AssertionResult testing::internal::CmpHelperEQ(const char*, const char*, const T1&, const T2&) [with T1 = double, T2 = double]’:
/home/ian/depot/gazebo/test/gtest/include/gtest/gtest.h:1353:30:   instantiated from ‘static testing::AssertionResult testing::internal::EqHelper<lhs_is_null_literal>::Compare(const char*, const char*, const T1&, const T2&) [with T1 = double, T2 = double, bool lhs_is_null_literal = false]’
/home/ian/depot/gazebo/gazebo/msgs/test/MsgFactory.cc:73:3:   instantiated from here
/home/ian/depot/gazebo/test/gtest/include/gtest/gtest.h:1316:3: warning: comparing floating point with == or != is unsafe [-Wfloat-equal]

@osrf-migration
Copy link
Author

Original comment by Steve Peters (Bitbucket: Steven Peters, GitHub: scpeters).


You can also use EXPECT_NEAR, ie. for x == y:

EXPECT_NEAR(x, y, 1e-6);

@osrf-migration
Copy link
Author

Original comment by Jose Luis Rivero (Bitbucket: Jose Luis Rivero, GitHub: j-rivero).


@scpeters is right. More information can be found in google test floating point comparison chapter.

@osrf-migration
Copy link
Author

Original comment by Nate Koenig (Bitbucket: Nathan Koenig).


I'm fixing this now.

@osrf-migration
Copy link
Author

Original comment by Nate Koenig (Bitbucket: Nathan Koenig).


  • changed state from "new" to "resolved"

Fixed in pull request #258

@osrf-migration
Copy link
Author

Original comment by Nate Koenig (Bitbucket: Nathan Koenig).


  • changed state from "resolved" to "closed"

qaz6906 pushed a commit to qaz6906/gazebo-classic_Hexa_vtol that referenced this issue Feb 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant