Thread: Round() Function

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    6

    Round() Function

    Hi All,
    I am trying to use round() function but gcc couldn't find it...
    Can you please help me??

    Compiler: gcc 4.1.2
    Command: gcc round_func.c

    Code:
    round_func.c
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
      double a = 13.05;    
      printf("round of  %.1lf is  %.1lf\n", a, round(a));
      return 0;
    }
    Error Message:
    round_func.c:7: warning: incompatible implicit declaration of built-in function ‘round’
    /tmp/cckuuW3l.o: In function `main':
    round_func.c.text+0x24): undefined reference to `round'



    Thanks and Regrads,
    Dharak

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is no round() function in the C standard library.

    floor() (which rounds down) and ceil() (which rounds up) are both standard, and can be used to implement other styles of rounding (round nearest, round toward zero, round away from zero, ..... ).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Because there's no such a function. Use floor() or ceil() instead.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    6
    Quote Originally Posted by DRK View Post
    Because there's no such a function. Use floor() or ceil() instead.
    Code:
        #include<stdio.h>
        #include<math.h>
    
        int main()
        {
              double a, b, c ;
              a = 3.8;
              b = floor(a);  //Not Working
              c = floor(104.54) //Working
            printf ("Floor of %0lf is %01f\n", a, c);
            return 0;
        }
    Here if I pass constant value in floor function, then its working but if I pass variable then it is not working.

    any idea?

    Do I need to include something else also?

    Thanks and Regards,
    Dharak

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    6
    I also dont understand, gcc version is 4.1.2

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That wouldn't make sense since the result is the third argument to printf, i.e., c, not a, should be changed to b, if necessary.

    EDIT:
    Quote Originally Posted by Dharak Shah
    I also dont understand
    What I mean is: what is it about the results that you get that makes you think that it doesn't work? For example, in the code that you posted, the obvious reason why it appears not to work is that your input value for the variable is different from the constant.
    Last edited by laserlight; 04-15-2013 at 03:50 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    6
    Quote Originally Posted by ZuK View Post
    You should output
    Code:
    printf ("Floor of %0lf is %01f\n", b, c); // a is not rounded
    Kurt
    That's not a issue. If you remove print statement then also it won't work with variable.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    #include<stdio.h>
    #include<math.h>
     
    int main()
    {
          double a, b, c ;
          a = 3.8d;
          b = floor(a);  //Not Working
          c = floor(104.54); //Working
        printf ("Floor of %0lf is %0lf\n", a, b);
        return 0;
    }
    my output
    Code:
    Floor of 3.800000 is 3.000000

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Your posted code doesn't even compile because you are missing a semicolon on line 9.

    And we can't help you if you don't tell us what doesn't work. When I run a corrected version of your code, everything works as expected.

    So what's your problem?

    Bye, Andreas

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dharak Shah
    That's not a issue. If you remove print statement then also it won't work with variable.
    It works. It works for me, and ZuK just posted that it works. Therefore, you are either not telling the truth, or you are simply mistaken. I prefer to think the latter. So... the ball is in your court. What is the program that you compiled and ran? What output did you get, and what makes you think that it doesn't work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2013
    Posts
    6
    My Program is:
    Code:
    #include<stdio.h>
    #include<math.h>
      
    int main()
    {
          double a, b;
          a = 3.8;
          b = floor(a); 
        printf ("Floor of %0lf is %0lf\n", a, b);
        return 0;
    }
    and now output is

    Code:
    [sdharak@Quartz Final]$ gcc round_func.c
    /tmp/ccl2Wy6h.o: In function `main':
    round_func.c:(.text+0x24): undefined reference to `floor'
    collect2: ld returned 1 exit status
    gcc version is
    gcc -dumpversion
    4.1.2

    Now I dont understand what is the issue with my setup?
    It should work..


    Thanks,
    Dharak

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably need to explicitly link to the math library with the -lm option.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by laserlight View Post
    You probably need to explicitly link to the math library with the -lm option.
    I can see the problem now.
    If you just use
    Code:
    c = floor(104.54);
    it is not necessary to link the mah library. If you use a variable the math lib is needed.
    I don't have an explanation for that, other then gcc is smart enough to evaluate the floor of a literal at compile time.

    Kurt
    Last edited by ZuK; 04-15-2013 at 04:15 AM.

  15. #15
    Registered User
    Join Date
    Apr 2013
    Posts
    6
    Quote Originally Posted by laserlight View Post
    You probably need to explicitly link to the math library with the -lm option.
    Ohhhh...
    It's working... Thanks....
    But why I need to do this?
    Is there any issue with gcc version??

    -Dharak

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (Community Game) Round Robin for C++ [Round 01]
    By phantomotap in forum General Discussions
    Replies: 5
    Last Post: 04-18-2011, 01:07 PM
  2. (Community Game) Planning for Round Robin for C++ [Round 02]
    By phantomotap in forum General Discussions
    Replies: 2
    Last Post: 04-17-2011, 02:39 PM
  3. This Can't Be....How is this round function working?
    By Krak in forum C++ Programming
    Replies: 14
    Last Post: 03-13-2005, 10:14 PM
  4. round() function
    By vege^ in forum C++ Programming
    Replies: 17
    Last Post: 04-09-2003, 06:51 PM
  5. does C++ have a round () function?
    By Susan in forum C++ Programming
    Replies: 6
    Last Post: 02-09-2002, 10:27 PM

Tags for this Thread