INTERNAL: Casting a Float to a Double in IDL
Anonym
Topic:
"Casting a Float to a Double in IDL seems to return corrupted numbers after the seventh decimal place".Discussion:
There are two possible sources of what is perceived as corrupt data.
1) Typecasting a float to a double is not equivalent to initializing a double. A float has only 7 decimal places of accuracy. So typecasting to double will retain only 7 decimal places of accuracy
2) Alternatively, IEEE standard (32-bit) doubles have up to 16 decimal places of accuracy. Therefore, corrupted numbers will appear after the 16th decimal place.
Below is a small C-code example which demonstrates the same behavior
A good referencewith regard to these topics is:
Gerald, Curtis F. and Patrick O. Wheatley, "Applied Numerical Analysis," 5th Edition, Addison Wesley Publ., 1994Solution:
void main()
{
float dummyFloat;
double dummyDouble;
dummyFloat = 3.14159265358979323;
dummyDouble = 3.14159265358979323;
printf("Value with 17 decimal places: 3.14159265358979323 ");
printf("float typecast to double: %20.18g ", (double)dummyFloat);
dummyFloat = 3.14159265358979323;
printf("actual IEEE double: %20.18g ", dummyDouble);
return;
}
Value with 17 decimal places: 3.14159265358979323
float typecast to double: 3.14159274101257324
actual IEEE double: 3.14159265358979312