3

Below control of bigger than 0.0 for a double variable works correctly. I am curious about whether the compiled exe with this function could behave differently on different systems.

bool MyFunction(double x)
{
    if (x > 0.0) 
        return true; 
    else 
        return false;
}

I mean do the below lines of main() behave differently on different systems?

double x = 0.0;
cout << MyFunction(x);
cigien
  • 57,834
  • 11
  • 73
  • 112
KC_
  • 89
  • 11
  • 5
    `0.0` can be represented exactly, so I cannot see how that would ever change? Not in your example, at least. Floating point math will introduce rounding errors, and even doing a bunch of additions, followed by subtractions which should equal zero doesn't necessarily equal zero – ChrisMM Apr 09 '20 at 13:53
  • 2
    Unrelated: `if (x>0.0) return TRUE; else return FALSE;` is better as `return x>0.0;` – Ted Lyngmo Apr 09 '20 at 13:58
  • There are no numbers that are sometimes positive and sometimes not. – molbdnilo Apr 09 '20 at 14:03
  • If you did a sequence of operations on floating point numbers that have inexact values in binary, they may not equal zero. For example, 0.1, 0.3. A compiler will often do literal constant folding so if you write `double x = 0.1 + 0.1 + 0.1 - 0.3` that might come out exactly as 0.0. But doing it in a sequence of function calls, it will probably have a few bits left over and will not be exactly 0.0. – Zan Lynx Apr 09 '20 at 14:06
  • 1
    [related](https://stackoverflow.com/questions/22785934/c-static-assert-of-ieee754) You can add a static_assert, then then upgrade your code on a case-by-case basis for any strange exotic systems that don't conform to ieee754. – JohnFilleau Apr 09 '20 at 14:08
  • "could behave differently on different systems" --> Yes, depends how `TRUE, FALSE, BOOL` are defined - those are not standard - but since code posted does not use the result, it is a don't care. As far as floating-point is concerned, the functionality is the same on all compliant systems. – chux - Reinstate Monica Apr 09 '20 at 14:28
  • 1
    [this](https://stackoverflow.com/a/38799236/13101806) is not directly related, but may be interesting to you. – Zuodian Hu Apr 09 '20 at 14:53

1 Answers1

4

While the C++ standard doesn't specify how floating point are represented, I doubt that there are any representations used in practice that cannot represent 0.0 precisely. It is safe to assume that the function returns false.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • So unless there is IEEE754 compliance problem as @JohnFilleau denoted above, which is my case I assume, it is safe to assume FALSE. – KC_ Apr 09 '20 at 14:25
  • 1
    @KC_ I don't see any problem mentioned. In the likely case of IEEE754 compliance, the function is definitely guaranteed to return false. In the unlikely case of non-conforming representation, the function is still likely to be guaranteed to return false unless the representation is intentionally bad. – eerorika Apr 09 '20 at 14:27
  • Ok. Thank you for the answer. – KC_ Apr 09 '20 at 14:37
  • Whether a C++ implementation can represent zero in a floating-point format is not a matter of doubt. Floating point formats vary in precision, exponent range, and other features, but they are defined to represent numbers as a sign, a sequence of digits in a certain base, and a scaling by a power of that base. If all the digits are zero, the number represented is zero. – Eric Postpischil Apr 09 '20 at 20:13