Thread View: microsoft.public.USASalesInfo.Developer.VisualC++
5 messages
5 total messages
Started by Azri Nielsen-Ferreira
Mon, 02 Mar 1998 00:00
Error in Microsoft Visual C++ v5, Borland C++ v5.01, JDK v1.1.5 when using "double" as type.
Author: Azri Nielsen-Ferreira
Date: Mon, 02 Mar 1998 00:00
Date: Mon, 02 Mar 1998 00:00
38 lines
1787 bytes
1787 bytes
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
Re: Error in Microsoft Visual C++ v5, Borland C++ v5.01, JDK v1.1.5 when using "double" as type.
Author: Jon Willard
Date: Mon, 02 Mar 1998 00:00
Date: Mon, 02 Mar 1998 00:00
8 lines
1346 bytes
1346 bytes
I too see this behavior, but as I suspected, when you change the cout to printf (in the C++ example) it works better. This tells me that the basic math is ok and the iostream output routines are a little whacky. I tried fooling the java version into working but still saw the problem. If the OutputStream is built on the iostream then that would explain that. Jon
Re: Error in Microsoft Visual C++ v5, Borland C++ v5.01, JDK v1.1.5 when using "double" as type.
Author: 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.
Re: Error in Microsoft Visual C++ v5, Borland C++ v5.01, JDK v1.1.5 when using "double" as type.
Author: Pete Becker
Date: Mon, 02 Mar 1998 00:00
Date: Mon, 02 Mar 1998 00:00
33 lines
1973 bytes
1973 bytes
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;
> }
> }
>
0.1 does not have an exact representation as a binary floating point
value, so the actual value that the compiler uses is slightly less than
0.1, which results in an extra trip through the loop. It's really the
same problem as in this infinite loop:
for( int i = 0; i < 10; i += 1/3 )
cout << i << endl;
-- Pete
Re: Error in Microsoft Visual C++ v5, Borland C++ v5.01, JDK v1.1.5 when using "double" as type.
Author: Michael Furman
Date: Mon, 02 Mar 1998 00:00
Date: Mon, 02 Mar 1998 00:00
42 lines
2491 bytes
2491 bytes
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.
You will probably have the same "error" using other languages.
The problem is that the representation of floating number cannot be
precise for all numbers - it is just an approximation.
So, 0.1 + 0.1 + .... + 0.2 (20 times) will not necesserely be
exactly 2.
Typical solutions:
- switch to integers:
for(int id = -10; id < 10;)
{
cout << id * .1 << endl;
id=id + 1;
}
- chage upper limit:
for (double d=-1.0;d<0.9999;)
{
cout << d << endl;
d=d+0.1;
}
--
---------------------------------------------------------------
== If you answer, please remove "nospam" from the address
Michael Furman, (603)893-1109
Geophysical Survey Systems, Inc. fax:(603)889-3984
13 Klein Drive engr @ gssi .
P.O. Box 97 mv . com
North Salem, 71543 . 1334 @
NH 03073-0097 compuserve.com
---------------------------------------------------------------
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads