Advertisement

Modulos not working with floats..?

Started by January 01, 2001 12:11 PM
5 comments, last by MrShaneParker 24 years ago
Is it just me, or does modulus not work with floating point numbers..? The expression: float mynum = 5.5 % 2.2; Gives an error, saying: 'illegal, left operand has to be constant double'... Is this just a rule of c++ i didn't know about? Thanks! - Shane Edited by - MrShaneParker on January 1, 2001 1:13:00 PM
-Shane Parker
Just a random suggestion (I can''t test it now), but maybe make mynum a double rather than float? Since it says it must be constant double...also, does modulus even work on decimals? Usually the whole point is to return the remainder of the division, which is an integer. Seems like it''s defeating the purpose of modulus almost .

Anthracks
Advertisement
"the operands of % must have integral type"

The C++ Programming Language 2nd Edition, Bjarne Stroustrup.
Page 502.
Wellll... it worked on this calculator i had. :D

Thanks guys.
-Shane Parker
Use the library function fmod();

David
Ah! That worked. Thanks! :D
-Shane Parker
Advertisement
Or use a bitwise AND to get the result. So:

5 % 2 = 1

a % b = a && (b - 1)

101 && 001 = 001 = 1

hmm, lemme try that on some bigger numbers, it doesn''t seem right.

23 % 10 = 310111 %  0101010111 && 0100100001= 1 


no, that doesn''t seem right (unless I stuffed up before... but I can''t be bothered checking). Forget I ever spoke. Or maybe there is a proper way to do that?

Trying is the first step towards failure.
Trying is the first step towards failure.

This topic is closed to new replies.

Advertisement