Article View: microsoft.public.USASalesInfo.Developer.VisualC++
Article #4Re: Error in Microsoft Visual C++ v5, Borland C++ v5.01, JDK v1.1.5 when using "double" as type.
From: Paul
Date: Mon, 02 Mar 1998 00:00
Date: Mon, 02 Mar 1998 00:00
70 lines
3086 bytes
3086 bytes
IEEE 8-byte floating point numbers, AKA doubles, may not have an exact
representation in binary, therefore you get the slight error.
Remember the *old* calculator "bug"? This is an example of the above:
1 / 3 * 3 = 0.9999999. (Should be 1.0)
If you need financial-like calculations where you can't afford the round
off problem, you'll need to get a financial math library. Rogue Wave has
one called Money.h or something similar to that. Or if you want to pick
up something free from the Web, you may need to do a search on "BCD" or
"Binary Coded Decimal".
I had a similar problem when I worked for an insurance company. We had
to scrap using "doubles" in our calculations and use a financial math
library. Just 1/2 a penny off gave us errors that mutiplied into
thousands of dollars difference!
BTW, the only language that guarantees correct calculations is COBOL,
since they do not use "doubles", but probably a form of BCD.
Regards,
Paul
Azri Nielsen-Ferreira wrote:
> There seems to be an error when using a the type "double" in C++ or
> Java. I have tested the program on PowerBuilder and Lotus Notes and
> both have the same error as Borland C++ v5.01, Microsoft Visual C++
> v5. I have tried the program with JDK v1.1.5 and Borland JBuilder too
> and they also have the same problem.
>
> Look at the following program and test it with your compiler
>
> C++ example:
>
> #include <iostream.h>
> void main(void)
> {
> for (double d=-1.0;d<1.0;)
> {
> cout << d << endl;
> d=d+0.1;
> }
> }
>
> Java example:
>
> public class doubleTest
> {
> public static void main(String args[])
> {
> for (double d=-1.0;d<1.0;)
> {
> System.out.println(d);
> d=d+0.1;
> }
> }
>
> }
>
> Best regards, Azri Nielsen-Ferreira
> E-mail: dkdacanf@ibmmail.com
--
To reply, remove "nospam." from my address.
Technical Details
Message-ID:
<34FB3280.6BA3D9F7@ix.netcom.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<34fed1be.3599015@news.uni-c.dk>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f8d38,982e935b1f8b3f3e X-Google-Attributes: gidf8d38,public X-Google-Thread: 10a146,982e935b1f8b3f3e X-Google-Attributes: gid10a146,public X-Google-Thread: 109b8e,982e935b1f8b3f3e X-Google-Attributes: gid109b8e,public X-Google-Thread: f5d71,982e935b1f8b3f3e X-Google-Attributes: gidf5d71,public X-Google-Thread: 109fba,982e935b1f8b3f3e X-Google-Attributes: gid109fba,public From: Paul <paulmck@ix.netcom.com> Subject: Re: Error in Microsoft Visual C++ v5, Borland C++ v5.01, JDK v1.1.5 when using "double" as type. Date: 1998/03/02 Message-ID: <34FB3280.6BA3D9F7@ix.netcom.com> X-Deja-AN: 330165045 Content-Transfer-Encoding: 7bit References: <34fed1be.3599015@news.uni-c.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: Netcom X-NETCOM-Date: Mon Mar 02 2:30:45 PM PST 1998 Newsgroups: microsoft.public.USASalesInfo.Developer.VisualC++,comp.lang.java.programmer,comp.lang.java,comp.lang.c++,borland.public.cpp.language