How to cast float to double without random value addition?

Started by
11 comments, last by budipro 18 years, 8 months ago
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.
Advertisement
float x = 1.0;
double y = x;

...

or am I misunderstanding you?
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]
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.
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 budipro
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


Just tried it :P :(
Quote:Original post by budipro
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?


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 budipro
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?
Though you may be surprised to hear it, the value of x was 123.454498291016 BEFORE it was assigned to y.
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
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement