0

Is there a better way to test if a double has decimal digits than this?

double value = 7.2;
bool isInt = value == double(round(value));

BTW: I use C++ 11 and gcc AND MSVC2015

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Marcus
  • 1,910
  • 2
  • 16
  • 27
  • Better in terms of what? – Jabberwocky Aug 23 '18 at 07:59
  • The problem with floating point numbers is that what you think is `9.0` (for example) might actually not be. See [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) for some details. – Some programmer dude Aug 23 '18 at 08:00
  • This is why i ask. I think it won't work for every number. But i can't figure out which one – Marcus Aug 23 '18 at 08:01
  • Just to highlight points from the duplicate: [`modf`](https://en.cppreference.com/w/cpp/numeric/math/modf), [`trunc`](https://en.cppreference.com/w/cpp/numeric/math/trunc). – BoBTFish Aug 23 '18 at 08:01
  • thx, i use the solution from the other question. – Marcus Aug 23 '18 at 08:04

0 Answers0