Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - Problems with floating point and modulus

 
Thread Tools Search this Thread
Old 04-18-2006, 12:30 PM   #1
Default Problems with floating point and modulus


Hi,

I try to get a 'next value' (depending on 'nextNumber') for a double with
this method.

public static double nextNearest(double value, double nextNumber)
{
final double remainder = value % nextNumber;
return value - remainder + (remainder > 0 ? nextNumber : 0);
}

But the 'remainder' variable does not get the exact remainder after the
modulus. Is there another way to realise this method so the return result
is the exact double that i want? I'm aware of floating point inaccuracies,
that's why I ask this question ..

Thanks,

J


Tasperian Jigs
  Reply With Quote
Old 04-18-2006, 01:14 PM   #2
Tasperian Jigs
 
Posts: n/a
Default Re: Problems with floating point and modulus
> But the 'remainder' variable does not get the exact remainder after the
> modulus.


With the example values of 0.7 and 0.01. The outcome is in that case
0.7000000000000001


Tasperian Jigs
  Reply With Quote
Old 04-18-2006, 03:13 PM   #3
Patricia Shanahan
 
Posts: n/a
Default Re: Problems with floating point and modulus
Tasperian Jigs wrote:
> Hi,
>
> I try to get a 'next value' (depending on 'nextNumber') for a double with
> this method.
>
> public static double nextNearest(double value, double nextNumber)
> {
> final double remainder = value % nextNumber;
> return value - remainder + (remainder > 0 ? nextNumber : 0);
> }
>
> But the 'remainder' variable does not get the exact remainder after the
> modulus. Is there another way to realise this method so the return result
> is the exact double that i want? I'm aware of floating point inaccuracies,
> that's why I ask this question ..
>
> Thanks,
>
> J


Can you explain a bit more of the context and requirements?

There are generally a couple of ways of dealing with rounding error when
you need to do tests:

1. Use a small fudge factor, traditionally called "epsilon". To apply
this, you need to find a value for epsilon that is smaller than any
non-zero number you would get if you were doing exact real arithmetic,
but larger than the rounding error.

Something around 1e-12 times the larger input value might work.

Your test would become remainder>epsilon rather than remainder>0.

2. Do the controlling stuff in some form of exact arithmetic, such as
scaled integer. If all interesting numbers are representable as decimal
fractions with a bounded number of digits, consider BigDecimal.

Patricia


Patricia Shanahan
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46