Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Remainder of float division.
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
RandomLetters




PostPosted: Wed May 25, 2011 9:25 pm   Post subject: Remainder of float division.

Is there a way to get a remainder for float division?

So for example 1.3 mod 0.9 = 0.4.
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Wed May 25, 2011 9:28 pm   Post subject: RE:Remainder of float division.

What is 130 mod 9?
RandomLetters




PostPosted: Wed May 25, 2011 9:31 pm   Post subject: RE:Remainder of float division.

1?
Insectoid




PostPosted: Wed May 25, 2011 9:32 pm   Post subject: RE:Remainder of float division.

No, try again.
RandomLetters




PostPosted: Wed May 25, 2011 9:33 pm   Post subject: RE:Remainder of float division.

oh fail. 4.

However, the floats can be much larger than integers.
apython1992




PostPosted: Wed May 25, 2011 9:36 pm   Post subject: RE:Remainder of float division.

If there's no way to do it in the language already, you can write it in yourself.
apython1992




PostPosted: Wed May 25, 2011 9:38 pm   Post subject: RE:Remainder of float division.

Two seconds of googling tells me that Java's mod operator (%) works with integers, floats, and doubles.
RandomLetters




PostPosted: Wed May 25, 2011 9:42 pm   Post subject: RE:Remainder of float division.

Wow, how did I miss that, lol. Thanks Razz
Sponsor
Sponsor
Sponsor
sponsor
apython1992




PostPosted: Wed May 25, 2011 9:43 pm   Post subject: RE:Remainder of float division.

No problem Smile Google is your friend!
Insectoid




PostPosted: Wed May 25, 2011 11:15 pm   Post subject: RE:Remainder of float division.

Even if Java's mod operator didn't work on floats, you could easily write a function (does Java allow operator overloading?) to find the number of significant digits of both parameters, select the greater number, n, and multiply both numbers by 10^n, do your modulus, and divide the result by n.

I'm sure there's a more efficient way to do this (using math, and not the mod operator at all), though float division is notoriously slow, so it might not make a difference.
apython1992




PostPosted: Thu May 26, 2011 7:01 am   Post subject: RE:Remainder of float division.

I don't think Java supports operator overloading, so that would have to be written as a method.

The better way I think would be to take the integer result from the division (the quotient floored to the nearest integer), which is the number of times the divisor goes completely into the dividend. Multiply the floored integer by the divisor, and subtract that result from the dividend to get your remainder. Example:

4.6 / 1.3 = 3.54 -> floored to 3
1.3 * 3 = 3.9
4.6 - 3.9 = 0.7 -> the remainder
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: