Skip to main content
gamedev.net
🔒 Locked

modulous

Started by SteveCRM Dec 27, 2002 at 5:43 PM 9 replies 850+ views
Original Post
SteveCRM
SteveCRM
Why can''t I use modulous? rquad=(rquad)%(circ); rquad and circ are both declared as GLfloat but I get these 2 errors : error C2296: ''%'' : illegal, left operand has type ''float'' : error C2297: ''%'' : illegal, right operand has type ''float''
Yann L
Yann L
Modulo is an integer only operation. It gives you the remainder of an integer division. There is no remainder for floating point divisions, only an error term. That''s why the % operator will not work on floating point types.
SteveCRM
SteveCRM
thanks
BlackLight
BlackLight
float fModulous = (int) fA % (int) fB;
Yann L
Yann L
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]
Taharez
Taharez
Well, I''ve used this in my code, and it works just fine
Yann L
Yann L
fmod is not really the same as integer modulus. fmod(x,y) returns x-(a*y), where a is the largest integer, so that the result has a smaller magnitude, though the same sign as x. In some cases it can simulate an % operator, in some not.
chronos
chronos
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]
chronos
chronos
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?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.