From mboxrd@z Thu Jan 1 00:00:00 1970 From: Sam Lantinga To: egcs@egcs.cygnus.com Subject: Rounding errors using doubles? Date: Thu, 18 Mar 1999 15:41:00 -0000 Message-id: X-SW-Source: 1999-03/msg00626.html On a Pentium II, the following code gives an incorrect result when using doubles, but a correct result when using floats: #include #ifdef USE_FLOAT /* This works */ typedef float percentage_type; #else /* This doesn't */ typedef double percentage_type; #endif main() { int value = 86; percentage_type percentage; printf("Starting Percentage is: %d\n", value); percentage = ((percentage_type)value)/100.0; printf("Ending Percentage is: %d\n", (int)(percentage * 100.0)); } Incorrect output: Starting Percentage is: 86 Ending Percentage is: 85 This is VERY bad for our software. An analysis of the offending assembly output shows only one difference between the broken and the working versions: --- t-bad.s Thu Mar 18 13:32:32 1999 +++ t.s Thu Mar 18 13:33:40 1999 @@ -28,8 +28,8 @@ fstp %st(0) fldl .LC1 fdivrp %st,%st(1) - fstpl -12(%ebp) - fldl -12(%ebp) + fstps -8(%ebp) + flds -8(%ebp) fldl .LC1 fstp %st(0) fldl .LC1 This is using egcs 1.1.1. Any ideas? Thanks, -Sam Lantinga (slouken@devolution.com) Lead Programmer, Loki Entertainment Software -- Author of Simple DirectMedia Layer - http://www.devolution.com/~slouken/SDL/ -- From mboxrd@z Thu Jan 1 00:00:00 1970 From: Sam Lantinga To: egcs@egcs.cygnus.com Subject: Rounding errors using doubles? Date: Wed, 31 Mar 1999 23:46:00 -0000 Message-ID: X-SW-Source: 1999-03n/msg00631.html Message-ID: <19990331234600.e1XTCOS3sAsPw4RZrbHNLTPL0aV6U9QYlB3WSPDS-Gs@z> On a Pentium II, the following code gives an incorrect result when using doubles, but a correct result when using floats: #include #ifdef USE_FLOAT /* This works */ typedef float percentage_type; #else /* This doesn't */ typedef double percentage_type; #endif main() { int value = 86; percentage_type percentage; printf("Starting Percentage is: %d\n", value); percentage = ((percentage_type)value)/100.0; printf("Ending Percentage is: %d\n", (int)(percentage * 100.0)); } Incorrect output: Starting Percentage is: 86 Ending Percentage is: 85 This is VERY bad for our software. An analysis of the offending assembly output shows only one difference between the broken and the working versions: --- t-bad.s Thu Mar 18 13:32:32 1999 +++ t.s Thu Mar 18 13:33:40 1999 @@ -28,8 +28,8 @@ fstp %st(0) fldl .LC1 fdivrp %st,%st(1) - fstpl -12(%ebp) - fldl -12(%ebp) + fstps -8(%ebp) + flds -8(%ebp) fldl .LC1 fstp %st(0) fldl .LC1 This is using egcs 1.1.1. Any ideas? Thanks, -Sam Lantinga (slouken@devolution.com) Lead Programmer, Loki Entertainment Software -- Author of Simple DirectMedia Layer - http://www.devolution.com/~slouken/SDL/ --