Re: Convert float to double - weird failure



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
.



Relevant Pages

  • Re: Types
    ... (there are machines with "typed RAM", ... mulf rResult, rInput1, rInput2 ... could treat the two inputs as 32-bit floating-point numbers, ... meaningful at all, for example adding two addresses, or multiplying two character strings. ...
    (comp.lang.c)
  • Re: Implementation of getbits() / setbits()
    ... This includes all machines using IEEE ... Some non-IEEE floating-point systems support multiple ...
    (comp.lang.c)
  • Re: Non Portable C
    ... different bit patterns on machines with different floating-point ... may be slightly under on some machines and slightly over on others. ... might not even have twos complement representation for negative numbers. ... individual members differ, but the padding and layout in memory may differ. ...
    (comp.lang.c)
  • Re: fixed point numbers
    ... bob wrote: ... Fixed point is usually used when floating-point is unavailable, ... limits it to 32-bit machines. ...
    (comp.games.development.programming.misc)