🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: microsoft.public.USASalesInfo.Developer.VisualC++
Article #7

Re: Error in Microsoft Visual C++ v5, Borland C++ v5.01, JDK v1.1.5 when using "double" as type.

#7
From: Michael Furman
Date: Mon, 02 Mar 1998 00:00
42 lines
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
---------------------------------------------------------------
Technical Details
Message-ID: <34FAEE28.1D31461@nospam.gssi.mv.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <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: f5d71,982e935b1f8b3f3e
X-Google-Attributes: gidf5d71,public
X-Google-Thread: 10a146,982e935b1f8b3f3e
X-Google-Attributes: gid10a146,public
X-Google-Thread: 109b8e,982e935b1f8b3f3e
X-Google-Attributes: gid109b8e,public
X-Google-Thread: 109fba,982e935b1f8b3f3e
X-Google-Attributes: gid109fba,public
From: Michael Furman <engr@nospam.gssi.mv.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: <34FAEE28.1D31461@nospam.gssi.mv.com>
X-Deja-AN: 330066718
Content-Transfer-Encoding: 7bit
References: <34fed1be.3599015@news.uni-c.dk>
To: dkdacanf@ibmmail.com
Content-Type: text/plain; charset=koi8-r
Organization: GSSI
Mime-Version: 1.0
Newsgroups: microsoft.public.USASalesInfo.Developer.VisualC++,comp.lang.java.programmer,comp.lang.java,comp.lang.c++,borland.public.cpp.language