Thread: Rounding function not working

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    9

    Rounding function not working

    I can't figure out why the roundf function is not successfully rounding my numbers.


    Code:
    #include <stdio.h>
    #include <math.h>
    
    //function declarations
    
    void CelToFah (int ctemp, int ftemp);
    void FahToCel (int cel, int fah);
    float roundf (float ftemp);
    float roundf (float cel);
    I use the roundf function in one of my other functions

    Code:
    void FahToCel (int fah, int cel)
    {
    //statements
    printf("Please enter the temperature in Fahrenheit degrees.\n");
    scanf("%d", &fah);
    (cel=(((float)fah-32.0)*5.0/9.0));
    roundf((float)cel);
    printf("The temperature in degrees Celsius is %d.\n", cel);
    return ;
    //FahToCel
    }
    it produces a correct number when called, but it is not a rounded number, the roundf((float)cel) does nothing. I get the same number with and without it.

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    roundf() returns something. You're never assigned that something that's returned to anything.

    Also, you don't need all those casts.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    9
    Code:
    void FahToCel (int fah, int cel)
    {
    //statements
    printf("Please enter the temperature in Fahrenheit degrees.\n");
    scanf("%d", &fah);
    (cel=(((float)fah-32.0)*5.0/9.0));
    cel=roundf((float)cel);
    printf("The temperature in degrees Celsius is %d.\n", cel);
    return ;
    //FahToCel
    }
    is that what you mean by assign something? When I do that and try to run it I get an error saying "undefined reference to `roundf'"

  4. #4
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by harmonypig View Post
    is that what you mean by assign something? When I do that and try to run it I get an error saying "undefined reference to `roundf'"
    Yes that's what I meant.

    You need to link with the math library. Usually add -lm to your compiler command line or in the makefile, but this may vary depending on which compiler you're using.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    9
    Thank you! That was definitely a step I was missing!
    However, I still don't get a rounded number, for example if i enter 46 degrees C I get an output of 114, even though the real answer is 114.8 and it should round up to 115.

  6. #6
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    The problem is that on line 6, you're assigning the result to cal (which is an int) so the truncation happens there. The floatf() function call is doing nothing.

    Try deleting line 6 and replacing line 7 with

    Code:
    cel = roundf((fah-32.0)*5.0/9.0);

  7. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    9
    Thank you so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rounding function in C
    By harmonypig in forum C Programming
    Replies: 1
    Last Post: 10-09-2013, 03:04 PM
  2. How to use ceil function (rounding up of numbers)
    By Jeffery1 in forum C Programming
    Replies: 2
    Last Post: 05-01-2013, 11:29 AM
  3. if function not working (its inside anot if function)
    By ahahsiol in forum C Programming
    Replies: 2
    Last Post: 03-08-2013, 08:55 AM
  4. Function not working! :*(
    By mramazing in forum C Programming
    Replies: 7
    Last Post: 01-07-2007, 02:14 AM
  5. Rounding function
    By sand_man in forum C Programming
    Replies: 7
    Last Post: 07-13-2005, 05:42 AM