Jump to content
  • Advertisement
Sign in to follow this  
budipro

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.454498
123.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 this post


Link to post
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 this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!