1
int main() {
// Write C code here
float x = 3.14;
if(x==3.14){
    printf("true");
}

return 0;

}

I am unable to execute this part of the if conditon.

  • 2
    `3.14` is a double, try `if (x == 3.14f)`. – alex01011 Sep 19 '21 at 16:30
  • 1
    As a general rule, don't try to compare floating-pont numbers for exact equality. Floating-point numbers are almost never exact. And even when it looks like they just have to be equal (like, when you're testing whether a value is what you thought it already was), there are a number of things that can go on "under the hood" to make the equality comparison fail. That's what's happening here. – Steve Summit Sep 19 '21 at 16:30
  • 1
    Additionally please see [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) and [Why Are Floating Point Numbers Inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Weather Vane Sep 19 '21 at 16:35
  • [Interesting article about float comparisons](https://floating-point-gui.de/errors/comparison). – Luca Polito Sep 19 '21 at 16:36
  • For `float x = 3.14;` MS VC does issue a warning: `truncation from 'double' to 'float'`. If your compiler didn't do this, crank up the warning level. If it did, please don't ignore warnings - some are definitely errors. – Weather Vane Sep 19 '21 at 16:46
  • As other answers and comments have pointed out, your problem is that you're comparing apples and oranges (or, at least, Macintosh and Granny Smith apples). The number 3.14 cannot be represented exactly in binary, neither as a `float` nor a `double`. Your variable `x` is a `float` and will have a value of about 3.1400001, while the constant `3.14` is a `double` and will have value of about 3.1400000000000001. Oviously those numbers are not equal. – Steve Summit Sep 19 '21 at 18:27
  • For anyone interested in *pi*, you should probably use `M_PI` from `math.h`. – Cheatah Sep 20 '21 at 02:00

3 Answers3

1

Instead of writing x = 3.14; write x = 3.14f and instead of if(x == 3.14) write if(x == 3.14f) because 3.14 is a double and 3.14f is a float.

  • You're correct, but `x == 3.14f` may still return false. – Luca Polito Sep 19 '21 at 16:38
  • 1
    @LucaPolito: `x == 3.14f` will evaluate as true. – Eric Postpischil Sep 19 '21 at 16:39
  • 1
    @EricPostpischil It depends: in this case yes, but if `float x = 3.04f + 0.1f;`, `x == 3.14f` will probably return false. It is not a good method to check if `x` is `3.14f`. – Luca Polito Sep 19 '21 at 16:45
  • @LucaPolito: That is a different issue. I have written repeatedly about [floating-point comparison](https://stackoverflow.com/a/21261885/298225): It is a mistake to single-out comparison as a problem. Given some floating-point calculations that have produced results `x` and `y`, the problem is not that you cannot compare `x` and `y` but that `x` and `y` are not the numbers you want. Ideally, you would have some numbers *x* and *y* computed with real-number arithmetic. But you do not. And then your problem is not that you cannot compare them, but that you do not have the numbers you want… – Eric Postpischil Sep 19 '21 at 17:27
  • … The fact you do not have the numbers you want means not only that you “cannot” compare them, but you do not know what there difference is, you do not know what the correct values are, you do not know if a machine made using `x` and `y` as measurements instead of *x* and *y* will work, you do not know if multiplying `x` and `y` will give the desired result that multiplying *x* and *y* would, and so on. Fundamentally, the problem lies in that earlier calculations have produced rounding errors. Laying all this on the comparison operation is a mistake… – Eric Postpischil Sep 19 '21 at 17:28
  • … Further, it is ironic, because the comparison operation is one of the few in floating-point operation that **has no errors at all**. `x == y` evaluates as true if and only if `x` is a number with the same value as `y`. There is never any rounding error in this operation. So warnings about comparisons are misplaced; they tell people to look in the wrong place for the errors. They are not in the comparisons; they are in the arithmetic (including conversions between bases). Look to your arithmetic operations to understand errors. – Eric Postpischil Sep 19 '21 at 17:30
  • @EricPostpischil You are 100% correct. My intention was just to point out that `x == 3.14f` may produce unexpected behavior if used carelessly (because of the problems you also explained). It is not because comparison is broken or not exact. Correct float comparisons need a close analysis of the context and of the operations executed *before* the comparison takes place. There is no one "universally correct" method to compare floats, it depends on the specific piece of code. And that's why I said that `x == 3.14f` may return false. – Luca Polito Sep 19 '21 at 18:06
1

3.14 is a double constant.

float x = 3.14; converts 3.14 to a float.

x==3.14 compares the float in x to the double 3.14. They are not equal.

The double and float formats use different representations of numbers and cannot represent all of the same values. (The float numbers are a subset of the double numbers.)

x == 3.14f will compare the float in x to the float constant 3.14f.

Preferably, use float constants with float types, so initialize x with float x = 3.14f; rather than float x = 3.14;.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

Wrap it inside float: if(x==float(3.14))

Edit: realised it's in c. Why not cast (float) 3.14?

hydra324
  • 13
  • 3