Inherent imprecision of floating point variables

Kaleb Keithley kaleb at mars.jpl.nasa.gov
Thu Jun 28 06:48:02 AEST 1990


In article <b3f.2688bfce at ibmpcug.co.uk> dylan at ibmpcug.CO.UK (Matthew Farwell) writes:
-In article <44436 at ism780c.isc.com> marv at ism780.UUCP (Marvin Rubenstein) writes:
-
-Consider the code segment:-
-
-main()
-{
-	float f;
-
-	f = 0.0;
-	while (1) {
-		if (f == 10.0) break;
-		printf("%f\n", f);
-		f += 0.1;
-	}
-	printf("Stopped\n");
-}
-
-If its all to do with conversion routines, why doesn't this stop when f
-reaches 10?
-

Because (10.0 - 0.1) + 0.1 will never be exactly equal to 10.0.


kaleb at thyme.jpl.nasa.gov            Jet Propeller Labs
Kaleb Keithley

"So that's what an invisible barrier looks like"



More information about the Comp.lang.c mailing list