Why is comparison of floating point numbers impossible?

Possible duplicate:
strange output in comparison of float with floating literal

#include<stdio.h>
int main()
{
float me = 1.7;
if(me==1.7)
   printf("C");
else
   printf("C++");
}

Output: C++

Now the reason for this behavior is that many floating point numbers cannot be represented with absolute precision in binary format.

My question is - if the computer thinks and manipulates in binary format. Any inconsistency in presentation mewould be the same for 1.7comparison. Therefore, both must be equal.

ALSO, how does typing solve this problem? (float)1.7

+3
source share
7 answers

You are comparing a float to a double. literal 1.7is double.

, , double, me == 1.7 1.7 float ( double) 1.7 double.

me == 1.7f me double double me = 1.7;

, , , epsilon, @duffymo.

, .

+13

+2

1.7 float double, float .

, , , , , - - (x + y) + z x + (y + z) ( , , , , ).

, (100 - (100 -.0000000001))! =.00000000001 IEEE-754 , , . , , .00000000001, . , , , .

IEEE-754 , , .

" ": http://docs.sun.com/source/806-3568/ncg_goldberg.html

+2

IEEE-754, 1.7f 1.7 :

1.7f == 1.7000000476837158203125
1.7  == 1.6999999999999999555910790149937383830547332763671875

, , .

+2

float me = 1.7f;

+1

numerical analysis, , , , , , .

0

1.7 . , 1.7 1.7d, , , () epsilons.

0

Source: https://habr.com/ru/post/1762428/


All Articles