Can the modulus operator work on double (data type) operands?
If not, can anyone refer me to some website that gives c code which does the modulus operation on double operands?
You may also suggest at least a pseudocode on doing this.
Can the modulus operator work on double (data type) operands?
If not, can anyone refer me to some website that gives c code which does the modulus operation on double operands?
You may also suggest at least a pseudocode on doing this.
This isn't really a place to 'make demands'. Try it and see if it works yourself.
> You may also suggest at least a pseudocode on doing this.
Okay, since you asked,
Code:A MODULUS B
I don't think that's a demand.
I'm asking if anyone knows whether % works with double operands 'coz it's causing compile-time errors in my program.
Also, I wouldn't ask that question if I haven't tried it.
If you can't answer my question, then don't.
You don't have to post stupid pseudocodes.
Last edited by chickenandfries; 03-28-2008 at 07:02 AM.
Compilation of the program failed.
The compiler indicated that modulus operation is not allowed with int and double as operands.
This code will work:
This code will not:Code:int main(void) { int a = 55; int b = 11; int c; c = a % b; printf("Answer is %d.\n", (int)c); fflush(stdout); return 0; }
Since it throws an error on c = a % b, I think you have your answer. I would not doubt, however, that there is a standard library function to do this. Google search for a modulus function that takes in doubles, or write your own.Code:int main(void) { double a = 55; double b = 11; double c; c = a % b; printf("Answer is %d.\n", (int) c); fflush(stdout); return 0; }
This is correct, you can not use % on non-integers.
There is a library function called fmod(): http://www.hmug.org/man/3/fmod.php
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
fmod() worked!
Thanks matsp!!!