🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #8534

double division rounding error

#8534
From: Ron Reynolds
Date: Mon, 12 Mar 2001 22:11
13 lines
1261 bytes
this i don't get, especially since the numbers are so simple.
the following code:
  double d1 = 2.05, d2 = 0.05;
  System.out.println("d1/d2 = " + d1/d2);
produces:
  d1/d2 = 40.99999999999999
if i cast the result to "float" i can get 41.0, which is nice,
but my question is why this sort of rounding error pops up with
such low-precision numbers and what's the accepted way of
handling this sort of problem?
thanks.
.............ron.
Technical Details
Message-ID: <3aad499f$1_2@dnews>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!bornews.borland.com!dnews!207.105.83.65

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,1db527391e14d00c,start
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-03-12 14:14:08 PST
From: "Ron Reynolds" <ron@ronreynolds.com>
Sender: "Ron Reynolds" <ron@ronreynolds.com>
Reply-To: "Ron Reynolds" <ron@ronreynolds.com>
Subject: double division rounding error
Newsgroups: borland.public.jbuilder.java.language
X-User-Info: 63.85.63.20 63.85.63.20    
NNTP-Posting-Host: 207.105.83.65
Message-ID: <3aad499f$1_2@dnews>
Date: 12 Mar 2001 14:11:43 -0800
X-Trace: 12 Mar 2001 14:11:43 -0800, 207.105.83.65
Lines: 13
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!bornews.borland.com!dnews!207.105.83.65