Re: Convert float to double - weird failure
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Fri, 26 Oct 2007 04:48:33 -0400
tugboat90 wrote:
I am trying to cast a float to a double (in C++) and am using the
static cast functionality. However, the value returned from the
static cast is not correct.
Example:
// Define a float
float f = 1.20000;
// Cast into a double - after the cast, the value of "d" is
// is "1.20000498297"
double d = static_cast<double> (f);
Is there some reason why the last few places are filled with garbage
now? I would have thought it would have been filled with 0's.
Yes. floats (and doubles) are based on binary representations, and
in your system they cannot exactly represent the value of 1.2.
Thus the value of f is set to the closest float approximation to
1.2 available. Now d is set to that value (not 1.2) whatever it
is.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com
.
- References:
- Convert float to double - weird failure
- From: tugboat90
- Convert float to double - weird failure
- Prev by Date: Re: College Advice
- Next by Date: Re: Different CS Degrees
- Previous by thread: Re: Convert float to double - weird failure
- Next by thread: Re: Convert float to double - weird failure
- Index(es):
Relevant Pages
|