How to use fmod inside a if function?
Code:double List ()
{
double Div = N;
while (--Div > 0){
if((N % Div) == 0 )
printf("%.lf +", Div);
}
}
Printable View
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 toQuote:
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);
}
}
But I got overflow, please help:confused: :confused:Code:double List ()
{
double Div = N;
d = fmod(Div, N);
while (--Div > 0){
if( d == 0 )
printf("%.lf +", Div);
}
}
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 presentedappears to attempt to use modulo vice fmod. You could either use fmod:Code:double List ()
{
double Div = N;
while (--Div > 0){
if((N % Div) == 0 )
printf("%.lf +", Div);
}
}
or cast your doubles into ints and use modulo:Code:if((fmod(N,Div))==0)
Your choice.Code:if(((int)N % (int)Div)) == 0)
What's it supposed to do?
When comparing floating-point values to an exact value such as zero, you need to remember that floating-point is an approximation and it's best to test for "close enough" instead of exactly the same.
C provides the FLT_EPSILON, DBL_EPSILON, and LDBL_EPSILON macros in float.h for such use.
-Prelude