Re: Convert float to double - weird failure
- From: Ben Pfaff <blp@xxxxxxxxxxxxxxx>
- Date: Thu, 25 Oct 2007 21:26:00 -0700
tugboat90 <bmshipe@xxxxxxxxx> writes:
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.
There is no need for any kind of cast to convert from a float to
a double. Simple assignment will have the same effect.
Beyond that, please read the C FAQ. Here are some relevant Q&A:
14.1: When I set a float variable to, say, 3.1, why is printf printing
it as 3.0999999?
A: Most computers use base 2 for floating-point numbers as well as
for integers. In base 2, one divided by ten is an infinitely-
repeating fraction (0.0001100110011...), so fractions such as
3.1 (which look like they can be exactly represented in decimal)
cannot be represented exactly in binary. Depending on how
carefully your compiler's binary/decimal conversion routines
(such as those used by printf) have been written, you may see
discrepancies when numbers (especially low-precision floats) not
exactly representable in base 2 are assigned or read in and then
printed (i.e. converted from base 10 to base 2 and back again).
See also question 14.6.
14.4: My floating-point calculations are acting strangely and giving
me different answers on different machines.
A: First, see question 14.2 above.
If the problem isn't that simple, recall that digital computers
usually use floating-point formats which provide a close but by
no means exact simulation of real number arithmetic. Underflow,
cumulative precision loss, and other anomalies are often
troublesome.
Don't assume that floating-point results will be exact, and
especially don't assume that floating-point values can be
compared for equality. (Don't throw haphazard "fuzz factors"
in, either; see question 14.5.)
These problems are no worse for C than they are for any other
computer language. Certain aspects of floating-point are
usually defined as "however the processor does them" (see also
question 11.34), otherwise a compiler for a machine without the
"right" model would have to do prohibitively expensive
emulations.
This article cannot begin to list the pitfalls associated with,
and workarounds appropriate for, floating-point work. A good
numerical programming text should cover the basics; see also the
references below.
References: Kernighan and Plauger, _The Elements of Programming
Style_ Sec. 6 pp. 115-8; Knuth, Volume 2 chapter 4; David
Goldberg, "What Every Computer Scientist Should Know about
Floating-Point Arithmetic".
--
"Now I have to go wash my mind out with soap."
--Derick Siddoway
.
- References:
- Convert float to double - weird failure
- From: tugboat90
- Convert float to double - weird failure
- Prev by Date: Convert float to double - weird failure
- Next by Date: Re: NewsMaestro Usenet Supertool v. 4.0.2 for Windows/Linux/Unix
- Previous by thread: Convert float to double - weird failure
- Next by thread: Re: Convert float to double - weird failure
- Index(es):
Relevant Pages
|