Modulos not working with floats..?
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
Anthracks
"the operands of % must have integral type"
The C++ Programming Language 2nd Edition, Bjarne Stroustrup.
Page 502.
The C++ Programming Language 2nd Edition, Bjarne Stroustrup.
Page 502.
Wellll... it worked on this calculator i had. :D
Thanks guys.
Thanks guys.
-Shane Parker
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.
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.
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
Popular Topics
Advertisement
Recommended Tutorials
Advertisement