How do I use modulus for float/double?
Posted
by ShrimpCrackers
on Stack Overflow
See other posts from Stack Overflow
or by ShrimpCrackers
Published on 2010-06-01T02:53:18Z
Indexed on
2010/06/01
3:03 UTC
Read the original article
Hit count: 318
java
I'm creating an RPN calculator for a school project. I'm having trouble with the modulus operator. Since we're using the double data type, modulus won't work on floating point numbers. For example, 0.5 % 0.3 should return 0.2 but I'm getting a division by zero exception.
The instruction says to use fmod(). I've looked everywhere for fmod(), including javadocs but I can't find it. I'm starting to think it's a method I'm going to have to create?
edit: hmm, strange. I just plugged in those numbers again and it seems to be working fine...but just in case. Do I need to watch out using the mod operator in Java when using floating types? I know something like this can't be done in C++ (I think).
© Stack Overflow or respective owner