Why float rounds the number ?

Hi
Try this code:
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
int main(){
	float x = 1.1;
	float y = 123.1;
	float z = 123.2;
	printf("x = %f\n", x);
	printf("y = %f\n",y);
	printf("z = %f\n", z);
	return 0;
}


I expected

1
2
3
x = 1.100000
y = 123.100000
z = 123.200000


but i get
1
2
3
x = 1.100000
y = 123.099998
z = 123.199997


Why is that?
Because the real numbers are innumerable and have therefore no representation in the set of binary representable numbers with size not larger than "Aleph 0" (somehow either the board software or my browser don't want to print the א with a subscript). In other words, not all numbers can be represented, and considering the fixed and very limited size of a float, in fact, only very few can (and the numbers you entered cannot). Therefore, they are rounded to the next representable number.
See also http://www.cplusplus.com/forum/articles/3827/
Thanks ... i get the point.
Real number are continuous, uninterrupted.
As theys representation is made in Binary, therefore uncontinuous, not all mumbers have a precise representation.
About the Aleph numbers ... thanks ... new for me ;)
Topic archived. No new replies allowed.