fmod problem

This is a discussion on fmod problem within the C Programming forums, part of the General Programming Boards category; How to use fmod inside a if function? Code: double List () { double Div = N; while (--Div > ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    16

    Question 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);
       }
    }

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    16

    Re: fmod problem

    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);
    
       }
    }
    But I got overflow, please help

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    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)
    Your choice.

    What's it supposed to do?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21