Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Problems with floating point and modulus

Thread view: 
Tasperian Jigs - 18 Apr 2006 12:30 GMT
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 - 18 Apr 2006 13:14 GMT
> 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
Patricia Shanahan - 18 Apr 2006 15:13 GMT
> Hi,
>
[quoted text clipped - 15 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.