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
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.

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.


Welcome!

It looks like you're new here. Sign in or register to get started.

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


Welcome!

It looks like you're new here. Sign in or register to get started.
Advertisement