Crazy floats
Crazy floats
- Subject: Crazy floats
- From: Lorenzo <email@hidden>
- Date: Fri, 23 Jul 2004 15:27:13 +0200
Hi,
I have a floats with 3 values but if I add many times the value 0.2 to one
item, it gets some decimal value more. Instead of -0.693000, I get
-0.693001. Why do I get the 1 at the end?
float positions[3];
positions[0] = -0.693000; // currentPoint
positions[1] = positions[0] + 2; // endPoint
positions[2] = increment = 0.2; // increment
so I try to add the increment to the "currentPoint" value until it is equal
to the "end" value.
while(positions[0] != positions[1]){
positions[0] += positions[2];
NSLog(@"currentPoint %f, end %f, increm %f",
positions[0], positions[1], positions[2]);
}
And since they are never equal, this produces an "infinite loop" (I am not
speaking about the Apple address).
My log:
--------
currentPoint -2.693000, end -0.693000, increm 0.020000
...
currentPoint -0.693002, end -0.693000, increm 0.020000
currentPoint -0.673002, end -0.693000, increm 0.020000
...
currentPoint 0.006998, end -0.693000, increm 0.020000
Best Regards
--
Lorenzo
email: email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.