1. Modular Division problem

I was working with floating point numbers and integers, and I came across a case where I needed just the part of the floating point number that comes after the decimal. So for example, if I had

Code:
`float x = 3.5;`
what I wanted was just the ".5" part of x, not the 3 in front of it. So I decided to use modular division to get it, like this:

Code:
```float x = 3.5;
float partOfX = x % 1;```
So what that would do would divide 3.5 by 1, giving me a remainder of .5, and storing that into partOfX. Yet the compiler for some reason does not allow me to perform modular division on floating point numbers. I tried casting things every which way, got nothing useful. Apparently you can only use modular division with integers.

So to fix this I just I did some casting to integer and subtraction to get the .5 that I wanted. But I thought it was weird that I couldn't perform modular division on floating point numbers. The calculator program that comes with Windows lets me but I guess Visual C++ 5.0 does not. Perhaps all C/C++ compilers don't.

Anyways, anyone know if its all C/C++ compilers that don't allow modular division with floating point numbers? Or is my compiler just goofy?

2. Nope, the modulus operator is only defined for the various integer types.

I suppose you could overload it for floating-point types if you wanted.

3. you could iterate it something fierce:
Code:
```while (num >= 1 || num <= -1)
num -= num / fabs(num);```
i'm pretty sure that would work

4. Code:
```double Number = 34.69823;
int Temp = (int)Number;
double Answer = Number - (double)Temp;

5. In Java you can take the modulus of a floating point number. With out using any "tricks"

6. Originally posted by Mister C
In Java you can take the modulus of a floating point number. With out using any "tricks"
How does this help? This is a C/C++ forum, not Java

7. #include <cmath> and use fmod( ).

8. The reason (presumably) that the modulus operator was not implemented for floating point types in C++ is that it is a relation in number theory dealing with the divisibility of integers. Since divisibility has no useful meaning if real or rational numbers are considered, it is not defined for them.

After all that, however, Knuth has a pretty good definition of the modulus operator for all real numbers x, and y:

x mod y =
x - y * [x / y] -- if y != 0
x -- if y = 0

Where [a] is the least integer function.

Alright, probably more than anyone cared to hear, sorry about that.

Cheers