Author |
Topic: Calculating Modulus (Read 1731 times) |
|
Grimbal
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 7527
|
|
Re: Calculating Modulus
« Reply #1 on: May 4th, 2011, 8:06am » |
Quote 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 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..
|
|
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: Calculating Modulus
« Reply #3 on: Jun 26th, 2011, 12:14pm » |
Quote 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:
Posts: 7527
|
|
Re: Calculating Modulus
« Reply #4 on: Jun 27th, 2011, 1:03am » |
Quote 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:
Posts: 13730
|
|
Re: Calculating Modulus
« Reply #5 on: Jun 27th, 2011, 8:39am » |
Quote 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:
Posts: 7527
|
|
Re: Calculating Modulus
« Reply #6 on: Jun 27th, 2011, 10:11am » |
Quote 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 Modify
|
Yeah got it wrong 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 |
|
|
|
|