🔒 Locked
modulous
Advertisement
Original Post
isn''t there a "floating point" modulus?
float fModulous = (int) fA % (int) fB;
quote:
isn't there a "floating point" modulus?
Nope. The modulus operation makes no sense from a mathematical point of view, when applied to a floating point number. The modulus is defined as the remainder of a division. In a floating point division, a remainder does not exist (or is always zero). So mathematically expressed: float_1 % float_2 = 0. The result would always be zero.
The fact that: (float_1 / float_2) * float_2 != float_1 is due to floating point inaccuracies, but not to a modulus (as it would be if both value were integers).
[edited by - Yann L on December 28, 2002 8:33:02 AM]
The mod operation for floating point numbers is defined as:
x mod y = x - y * floor(x/y) ,
where floor() returns the greatest integer less than or equal to the given number.
So, if x = 1.0 and y = 0.33:
1.0 mod 0.33 = 1.0 - 0.33 * floor(1.0/0.33)
= 1.0 - 0.33 * 3.0
= 1.0 - 0.99
= 0.01
It's worth noting that x = q*y + (x mod y) , where q is an integer value (the quotient) that is equal to floor(x/y).
So, while it's true that C's % operator doesn't operate on floating point numbers, it's nevertheless possible to define the mod operation for all real numbers, as above.
(Source: The Art of Computer Programming, Vol. 1 by Donald Knuth)
[Edit: I originally said that floor() drops the number's fractional part, but that's not true when the number is negative. Also, I forgot "or equal" above. Sorry.]
[edited by - chronos on December 28, 2002 4:24:30 PM]
x mod y = x - y * floor(x/y) ,
where floor() returns the greatest integer less than or equal to the given number.
So, if x = 1.0 and y = 0.33:
1.0 mod 0.33 = 1.0 - 0.33 * floor(1.0/0.33)
= 1.0 - 0.33 * 3.0
= 1.0 - 0.99
= 0.01
It's worth noting that x = q*y + (x mod y) , where q is an integer value (the quotient) that is equal to floor(x/y).
So, while it's true that C's % operator doesn't operate on floating point numbers, it's nevertheless possible to define the mod operation for all real numbers, as above.
(Source: The Art of Computer Programming, Vol. 1 by Donald Knuth)
[Edit: I originally said that floor() drops the number's fractional part, but that's not true when the number is negative. Also, I forgot "or equal" above. Sorry.]
[edited by - chronos on December 28, 2002 4:24:30 PM]
Another thing about C''s % operator is that, as far as I know, its behavior is undefined when either operand is negative, so C''s % operator may or may not correspond to the mathematical modulo operator.
Mathematically, the result of -6 mod 5 is 4. In C, the result of -6 % 5 is either -1 or 4, depending on the implementation.
Pretty confusing, isn''t it?
Mathematically, the result of -6 mod 5 is 4. In C, the result of -6 % 5 is either -1 or 4, depending on the implementation.
Pretty confusing, isn''t it?
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement