-4

I found many questions.But none helps me

      float x = 0.1;
      x == 0.1

The above code returns false. since i try to compare double precision value with single precision x.

      float x = 0.5
      x == 0.5

This one returns true. I cant find why is it returning true?? Any suggestions ??

EDIT : So how do i identify which value has same representation in both precisions??

Gibbs
  • 21,904
  • 13
  • 74
  • 138

2 Answers2

5
x == 0.1

0.1 is not of type float but of type double. float and double don't have the same precision. 0.1f is of type float.

Why it works with 0.5 is because 0.5 has an exact representation in both float and double (in binary IEEE-754) types.

ouah
  • 142,963
  • 15
  • 272
  • 331
0

It is because of float/double representation in memory, you are seeing this behavior.

Please see strange output in comparison of float with float literal

Community
  • 1
  • 1
µtex
  • 900
  • 5
  • 12