wu :: forums
« wu :: forums - Calculating Modulus »

Welcome, Guest. Please Login or Register.
May 14th, 2024, 9:21pm

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: Grimbal, ThudnBlunder, towr, SMQ, Eigenray, william wu, Icarus)
   Calculating Modulus
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: Calculating Modulus  (Read 1731 times)
Skandh
Junior Member
**





   


Gender: male
Posts: 77
Calculating Modulus  
« on: May 3rd, 2011, 7:17am »
Quote Quote Modify Modify

Given : X - integer > 1
    Y - floating point number with fixed digits after decimal point and Y>=0
 
To find :  XY%X
 
If  Y=0 then XY%X=1
If 0<Y<1 then XY%X=XY
If Y=1 then XY%X=0
but how can we calculate result for Y>1. Is there any efficient method other than calculating XY and then taking modulus.
IP Logged

I wanna pull by legs!!!
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Calculating Modulus  
« Reply #1 on: May 4th, 2011, 8:06am »
Quote Quote Modify Modify

Since Y is a float, I don't see a better way to compute XY than by using the logs.  XY = exp(Y·log X).
 
The problem with %X is that it amplifies errors, so if you want an accurate result, depending on the size of XY, you might have to compute with a very high precision before taking the modulo.
 
XY%X = X·frac(XY-1)  (frac is the fractional part, after the decimal point).  So the precision you need to use for XY is the number of digits in XY-1 plus the number of significant digits you want for your result.
IP Logged
wiley
Newbie
*





   


Posts: 12
Re: Calculating Modulus  
« Reply #2 on: Jun 26th, 2011, 7:13am »
Quote Quote Modify Modify

Even if Y > 1, say 1.52, the XY mod X should be X.52, i.e. fractional part of Y.
 
Hence calculating XY-floor(Y) is sufficient..
Correct me if I made some silly mistake in understanding the problem..  Smiley
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Calculating Modulus  
« Reply #3 on: Jun 26th, 2011, 12:14pm »
Quote Quote Modify Modify

21.52 ~= 2.8679105  ==> 21.52 % 2 ~= 0.8679105
20.52 ~= 1.43395525  ==> 20.52 % 2 ~= 1.43395525
therefor 21.52 % 2 != 20.52 % 2
 
So as a general rule it fails.
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Calculating Modulus  
« Reply #4 on: Jun 27th, 2011, 1:03am »
Quote Quote Modify Modify

It is not even true with integers:
22 mod 2 = 0
22-floor(2) mod 2= 20 mod 2 = 1.
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Calculating Modulus  
« Reply #5 on: Jun 27th, 2011, 8:39am »
Quote Quote Modify Modify

Yeah, but for positive integers it's true if instead of floor(X) you taken ceil(X)-1. Which for positive non-integer reals would be the same.
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Calculating Modulus  
« Reply #6 on: Jun 27th, 2011, 10:11am »
Quote Quote Modify Modify

So it pretty much never works the way it was stated.
 
It would work for X=1, or 0<Y<1, but that was explicitly excluded...
IP Logged
wiley
Newbie
*





   


Posts: 12
Re: Calculating Modulus  
« Reply #7 on: Sep 3rd, 2011, 9:25am »
Quote Quote Modify Modify

Yeah got it wrong Sad
 
We may need not compute XY by taking modulus on each multiplication with X:
Something like:
To compute 24.52%2,
 
2.52%2 = 1.4339
1.4339 * 2 = 2.8679 - 2 = .8679
0.8679 * 2 = 1.7358
1.7358 * 2 = 3.4716 - 2 = 1.4716
1.4716 * 2 = .9432
IP Logged
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board