🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: double division rounding error

#8544
From: Ron Reynolds
Date: Tue, 13 Mar 2001 14:17
38 lines
2389 bytes
Tor Iver Wilhelmsen <tor.wilhelmsen@sds.no> wrote:
>If precision is important to you, do all computations with scaled
>ints/longs or java.math.BigDecimal.

i tried the BigDecimal approach - it produced stranger results:
40.99999999999999417132912071792848633216439957879871.

i understand that decimals, because of their representations, are
subject to weird "sums of negative powers of two" problems, but
i didn't expect that to appear in numbers so low in sig-figs.

the actual problem i'm trying to solve is to determine if d1 is
a perfect multiple of d2 (basically does d1%d2==0?).  the problem
is that d1%d2 is often very close (but not equal to) 0.0 or very
close (but not equal to) d2, so i could use some form of sigma
value to put tolerances around what i consider 0 and d2, but the
current approach i'm using is this:

 boolean isMultiple(double d1, double d2) {
   float multi = (float)(d1/d2);
   return Math.floor(multi) == Math.ceil(multi);
 }

using doubles is a requirement, but most values we work with are
currency values (or stock prices) and the docs indicate that
float is good for up to 6 significant decimal digits, so i think
i'm safe on the precision loss.  however, this method seems like 
it might be doing more work than it needs to calculating floor()
and ceil() this way, but it produces (so far) consistent results
and i can't imagine floor() and ceil() being all that 
expensive.  (i hope)

i'd really like to know how would one of you guys would have
solved this same problem.

thx.
.............ron.
Technical Details
Message-ID: <3aae2be1$1_2@dnews>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!newsfeed.wirehub.nl!hub.org!hub.org!PeerFeed.KWeb.on.ca!bornews.borland.com!dnews!207.105.83.65
References: <3aad499f$1_2@dnews> <usnkioyup.fsf@sds.no>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,1db527391e14d00c
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-03-13 06:26:34 PST
From: "Ron Reynolds" <ron@ronreynolds.com>
Sender: "Ron Reynolds" <ron@ronreynolds.com>
Reply-To: "Ron Reynolds" <ron@ronreynolds.com>
Subject: Re: double division rounding error
Newsgroups: borland.public.jbuilder.java.language
X-User-Info: 63.85.63.20 63.85.63.20    
References: <3aad499f$1_2@dnews>  <usnkioyup.fsf@sds.no> 
NNTP-Posting-Host: 207.105.83.65
Message-ID: <3aae2be1$1_2@dnews>
Date: 13 Mar 2001 06:17:05 -0800
X-Trace: 13 Mar 2001 06:17:05 -0800, 207.105.83.65
Lines: 38
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!newsfeed.wirehub.nl!hub.org!hub.org!PeerFeed.KWeb.on.ca!bornews.borland.com!dnews!207.105.83.65