How to cast float to double without random value addition?

This topic is 4847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites
float x = 1.0;
double y = x;

...

or am I misunderstanding you?

Share on other sites
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?

Share on other sites
float x = 123.1234;
double y = x;

y -= ( y - x );

Will this work?

ace

[Edited by - ace_lovegrove on August 10, 2005 4:10:39 AM]

Share on other sites
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.

Share on other sites
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

Share on other sites
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 :(

Share on other sites
Quote:
 Original post by budiprofloat x = 123.4545f;double y = x;The value of y will be 123.454498291016, not 123.4545How 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.

Share on other sites
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() );

Share on other sites
Quote:
 Original post by budiprofloat x = 123.4545f;double y = x;The value of y will be 123.454498291016, not 123.4545How 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

1. 1
Rutin
38
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 14
• 9
• Forum Statistics

• Total Topics
633350
• Total Posts
3011473
• Who's Online (See full list)

There are no registered users currently online

×