fmod problem

• 08-01-2002
yusiye
fmod problem
How to use fmod inside a if function?
Code:

```double List () {   double Div = N;   while (--Div > 0){     if((N % Div) == 0 )     printf("%.lf +", Div);   } }```
• 08-01-2002
yusiye
Re: fmod problem
Quote:

Originally posted by yusiye
How to use fmod inside a if function?
Code:

```double List () {   double Div = N;   while (--Div > 0){     if((N % Div) == 0 )     printf("%.lf +", Div);   } }```

I changed to
Code:

```double List () {   double Div = N;   d = fmod(Div, N);   while (--Div > 0){     if( d == 0 )     printf("%.lf +", Div);   } }```
• 08-01-2002
gabulldog
fmod returns the remainder of a divisor and dividend presented by the code. It is a very useful tool, but you need to remember that floats and doubles are not always exact, so you might not get the desired result.

The code you have presented
Code:

``` double List () {   double Div = N;   while (--Div > 0){     if((N % Div) == 0 )     printf("%.lf +", Div);   } }```
appears to attempt to use modulo vice fmod. You could either use fmod:
Code:

` if((fmod(N,Div))==0)`
or cast your doubles into ints and use modulo:
Code:

`if(((int)N % (int)Div)) == 0)`