Re: how long is double
From: Jack Klein (jackklein_at_spamcop.net)
Date: 01/04/04
- Next message: Thomas Mang: "Re: how long is double"
- Previous message: Andrew Koenig: "Re: how long is double"
- In reply to: f: "how long is double"
- Next in thread: P.J. Plauger: "Re: how long is double"
- Reply: P.J. Plauger: "Re: how long is double"
- Reply: Servé Lau: "Re: how long is double"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 4 Jan 2004 06:17:06 -0500
On 3 Jan 2004 21:26:11 -0500, ffunus@yahoo.com (f) wrote in
comp.lang.c++:
> I have this
>
> double sum, a, b, c;
> sum = a + b + c;
> printf("%.20f = %.20f, %.20f, %.20f", sum, a, b, c);
>
> I found that the debug version and release version of the same code
> give me different result. I am using VC++ 6.0.
>
> In debug version, the print out is:
> -12.25938471938358500000 = -11.43596388399630500000,
> -0.07591666113607631300, -0.74750417425120252000,
>
> In the release version, the print out is:
> -12.25938471938358300000 = -11.43596388399630500000,
> -0.07591666113607631300, -0.74750417425120252000,
>
> The above sum = a + b + c is just a part of my computation. I found
> that my whole computation crushed in the debug version because some
> number became zero and another number divide this number. But this did
> not happened in the release version.
>
> Why?
>
> Thanks,
>
> ff
If you value accuracy in floating point calculations, do not use
Visual C++ under any circumstances. When Microsoft changed from
16-bit to 32-bit operating systems and compilers, they made changes to
their floating point code for the purpose of providing compatibility
on all the different processors for which they would provide Windows
NT, most of which never happened.
In particular, they made these two changes:
1. In their 16-bit compilers, the long double type used the full 80
bit extended precision of the Intel floating point hardware. In their
32-bit compilers, long double is the same as double and uses the 64
bit double precision mode of the floating point hardware. There is no
way in Visual C++ at all to utilize the higher precision mode build
into the hardware.
2. In most cases, their math code sets the floating point hardware
control bits to limit precision to 64 bits, instead of using the 80
bit format normally used internally by the FPU. That means that you
lose precision on calculations entirely inside the FPU, and not just
when you store values to RAM.
Microsoft made the decision years ago that programmers were not
trustworthy to decide for themselves whether they were better off with
the highest precision the Intel FPU can provide, or they should
sacrifice performance and accuracy for compatible floating point
results on other processors that nobody actually bought Windows NT on.
They made the decision for you, and took away your control over the
precision of your results.
If you want the maximum precision and accuracy that the Intel FPU is
capable of providing, you have to give up on Visual C++ and switch to
another compiler, such as Borland or GNU, that gives you extended
precision long double and doesn't truncate the floating point control
bits.
-- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
- Next message: Thomas Mang: "Re: how long is double"
- Previous message: Andrew Koenig: "Re: how long is double"
- In reply to: f: "how long is double"
- Next in thread: P.J. Plauger: "Re: how long is double"
- Reply: P.J. Plauger: "Re: how long is double"
- Reply: Servé Lau: "Re: how long is double"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|