Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C programming (rounding up)

Options
  • 19-03-2014 8:06am
    #1
    Registered Users Posts: 5,063 ✭✭✭


    Hey guys just want a little help with a problem I am having regard round a value up. It is a perimeter fencing problem. User inputs diameter of a circular field. :p
    User inputs the length of each roll
    User inputs the cost of each roll
    pi given as 3.1416
    What has work
    user inputs
    Calculation of circumference of field
    Number of roll required returns a value with decimal(need this to be rounded up)
    cost of fencing field above value * cost per roll a user input.

    using <math.h> library anything here I can use I don't want to use int here because it will round down value where the decimal value is less than 0.5 underestimating the true cost.


Comments

  • Registered Users Posts: 772 ✭✭✭maki


    The ceil function of math.h is what you want to use.
    ceil(your float here) will round it up.


  • Registered Users Posts: 5,490 ✭✭✭stefanovich


    Hey guys just want a little help with a problem I am having regard round a value up. It is a perimeter fencing problem. User inputs diameter of a circular field. :p
    User inputs the length of each roll
    User inputs the cost of each roll
    pi given as 3.1416
    What has work
    user inputs
    Calculation of circumference of field
    Number of roll required returns a value with decimal(need this to be rounded up)
    cost of fencing field above value * cost per roll a user input.

    using <math.h> library anything here I can use I don't want to use int here because it will round down value where the decimal value is less than 0.5 underestimating the true cost.
    Use a float or a double.


  • Registered Users Posts: 5,063 ✭✭✭Greenmachine


    maki wrote: »
    The ceil function of math.h is what you want to use.
    ceil(your float here) will round it up.

    Okay will give that a try so perform original calculation to achieve an answer for rolls required then something like

    ceil(rr);
    tc = (rr*rc); //tc total cost, rr rolls required, rc is roll cost
    then my normal printf statements:


  • Registered Users Posts: 772 ✭✭✭maki


    Okay will give that a try so perform original calculation to achieve an answer for rolls required then something like

    ceil(rr);
    tc = (rr*rc); //tc total cost, rr rolls required, rc is roll cost
    then my normal printf statements:

    Just note that ceil(rr) won't store the value in any variable; it'll just do the calculation in memory.
    You'll want something like:
    float roundedResult = ceil(rr);


  • Registered Users Posts: 5,063 ✭✭✭Greenmachine


    maki wrote: »
    Just note that ceil(rr) won't store the value in any variable; it'll just do the calculation in memory.
    You'll want something like:
    float roundedResult = ceil(rr);

    Brilliant will give that a go.


  • Advertisement
  • Registered Users Posts: 1,931 ✭✭✭PrzemoF


    Side note: there should be M_PI or HALF_PI defined in math.h that you can use.


  • Registered Users Posts: 5,063 ✭✭✭Greenmachine


    PrzemoF wrote: »
    Side note: there should be M_PI or HALF_PI defined in math.h that you can use.

    Not come across either of those. For the question I was looking at we were given a value for pi so did not need to call a predifined value. I will have a look into these two again. When would I use HALF_PI? The trapezoid rule perhaps?


  • Registered Users Posts: 92 ✭✭jgh_


    Not come across either of those. For the question I was looking at we were given a value for pi so did not need to call a predifined value. I will have a look into these two again. When would I use HALF_PI? The trapezoid rule perhaps?

    It's useful if you're doing geometric transformations and such. A handy way of getting a 90 degree angle ;)


Advertisement