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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C programming (rounding up)

  • 19-03-2014 7: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: 772 ✭✭✭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: 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, 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,931 ✭✭✭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