Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C programming (rounding up)

  • 19-03-2014 08:06AM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 774 ✭✭✭maki


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


  • Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 774 ✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 1,929 ✭✭✭PrzemoF


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


  • Registered Users, Registered Users 2 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, Registered Users 2 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