How to cast float to double without random value addition?
Hi all,
I would like to know how to cast float to double without the addition of random value. I can do it by converting the float into a string first, but this way is to slow.
Thanks a lot.
float x = 123.4545f;double y = x;
The value of y will be 123.454498291016, not 123.4545
How to get rid of this extraneous value?
float x = 123.1234;
double y = x;
y -= ( y - x );
Will this work?
Nope is the answer lol.
ace
[Edited by - ace_lovegrove on August 10, 2005 4:10:39 AM]
double y = x;
y -= ( y - x );
Will this work?
Nope is the answer lol.
ace
[Edited by - ace_lovegrove on August 10, 2005 4:10:39 AM]
Unless i'm mistken, your problem is not coming from the float-to-double convertion. It is a problem of storing a perfect floating point value into an IEEE floating point variable, which cannot represent perfectly some numbers. To convince yourself, just display the actual value of "x" on the output, before the cast to a double. You'll see (with 6 digits) it values 123.454498
Y.
Y.
Quote:
float x = 123.1234;
double y = x;
y -= ( y - x );
Will this work?
No. During the operation: y -= ( y - x ), the x is also automatically casted to double, which produces: y -= 0
Quote:Original post by budiproQuote:
float x = 123.1234;
double y = x;
y -= ( y - x );
Will this work?
No. During the operation: y -= ( y - x ), the x is also automatically casted to double, which produces: y -= 0
Just tried it :P :(
Quote:Original post by budiprofloat x = 123.4545f;double y = x;
The value of y will be 123.454498291016, not 123.4545
How to get rid of this extraneous value?
Ah, but are you sure about the value of x? :)
#include <stdio.h>int main(){ float x = 123.4545f; double y = x; printf("%f\n%f\n", x, y); return 0;}
outputs:
123.454498123.454498
It so happens that many, many numbers that have an exact finite representation in base 10 don't have one in the binary system. 123.4545 happens to be one of them; you just gotta live with the fact. It isn't about float vs. double; all floats are also exactly representable as doubles (the same doesn't hold in the other direction, obviously), at least in the most popular floating-point format.
Quote:
To convince yourself, just display the actual value of "x" on the output, before the cast to a double. You'll see (with 6 digits) it values 123.454498
Mmm... no.
It is 123.4545.
float x = 123.4545f;Console.WriteLine( x.ToString() );
Quote:Original post by budiproThough you may be surprised to hear it, the value of x was 123.454498291016 BEFORE it was assigned to y.float x = 123.4545f;double y = x;
The value of y will be 123.454498291016, not 123.4545
How to get rid of this extraneous value?
Not even a value as simple as 0.9 can be stored accurately in a float, OR a double. It's only ever an approximation when it comes down to representing base 10 fractions in binary.
1/2^-1 + 1/2^-2 + 1/2^-3 + 1/2^-6 + ... =
0.5 + 0.25 + 0.125 + 0.015625 + ... =
What Every Computer Scientist Should Know About Floating-Point Arithmetic
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement