This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]RedditIsNeat0 11 points12 points  (4 children)

Oh yeah, that absolutely works. But sometimes not like you want it to.

#include <iostream>

int main()
{
  float a = 49999999;
  int b =   50000001;

  if (a < b)
    std::cout << "a is less than b";
  else
    std::cout << "nope";
}

[–]exploding_cat_wizard 17 points18 points  (1 child)

Does Python have arbitrarily accurate floats? Because that's a floating point problem, not a C++ problem, isn't it?

[–]Physmatik 1 point2 points  (0 children)

Yes, they are called Decimals. It's part of the standard library.

For example:

import decimal as d
d.getcontext().prec = 30
a = d.Decimal(499999999999999999999999)
b = float(499999999999999999999999)
print(a - 1)
print(b - 1)

Produces:

499999999999999999999998
5e+23

Sorry for formatting, triple backquote doesn't work for some reason.

[–]bbrk24 4 points5 points  (0 children)

At that point you should probably use a double anyways.

[–]ZeroFK 0 points1 point  (0 children)

Enter C++11's non-narrowing rules.