0

I need to know the price of all the offices that a company hires, knowing that you pay per office and there can be room for two workers per room, the problem is that I cannot use libraries or if...

monthlyPrice = oficePrice * (workers/2);
monthlyPrice = 100 * (7 / 2) = 350$

I need to either round up or find me another mathematical formula.

it could be done with a realtointegrer, i don´t now... :(

Thanks. Pablo.

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • Use floating point types instead of integers? Perhaps like `workers / 2.0`? – Some programmer dude Oct 01 '21 at 08:02
  • 8
    Divide x by 2 and round up? `(x+1)/2`. – n. m. could be an AI Oct 01 '21 at 08:05
  • duplicates: [Dividing two integers and rounding up the result, without using floating point](https://stackoverflow.com/q/17005364/995714), [Fast ceiling of an integer division](https://stackoverflow.com/q/2745074/995714), [What's the right way to implement integer division-rounding-up?](https://stackoverflow.com/q/41870852/995714). And in case rounding to nearest is needed: [Rounding integer division (instead of truncating)](https://stackoverflow.com/q/2422712/995714) – phuclv Oct 01 '21 at 08:18
  • Your title asks about “round up,” but your example seems to show you want 350 for 100•(7/2), which is not rounding up but simply an exact calculation using ordinary arithmetic. Edit the question to clarify the problem: Do you want to **round** results to a nearby integer, or do you want to **calculate with fractions**? If you want to round, what rounding rule do you want to use—to nearest, to next lower integer, to next higher integer—include how ties should be handled. If you want to calculate with fractions, there may be more questions about the accuracy needed. – Eric Postpischil Oct 01 '21 at 11:58

3 Answers3

1

The simpliest way to round a floating point number x is as follows:

return (int)(x + 0.5);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • this one rounds to "nearest" for positive values and **not** round up as the OP wants. Besides it's much slower than the integer solution because of the 2 cross-domain data movements – phuclv Oct 01 '21 at 09:12
0

I would suggest that you use the / and % operators.

/ will give you the integer section (left side of decimal point) for a division operation.

% will give you the remainder section (right side of the decimal point) of a division operation.

e.g. 7/2 gives 3

7%2 gives 1

You can then decide whether you wish to round up the result depending on the value returned by using %.

Ah I see that you're not allowed to use the if ... else ... construct. What about ... ? ... : ...?

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
ChrisBD
  • 9,104
  • 3
  • 22
  • 35
  • _What about ... ? ... : ...?_ I don't think it's a good idea to use a ternary when the task doesn't allow the use of `if`s. – David Ranieri Oct 01 '21 at 08:17
0

For money I would use fixed point.

//one cent is minimum we care
#define MULT    (100)  

#define MAKE_FIXED(d)  ((int32_t)(d * MULT))
#define MAKE_REAL(f)   (((double)(f)) / MULT)

int32_t mulf(int32_t a, int32_t b)
{
    int64_t part = (int64_t)a * b;
    return part/MULT;
}

int32_t divf(int32_t a, int32_t b)
{
    int64_t part = ((int64_t)a * MULT) / b;
    return part;
}


int main(void)
{
    int32_t officeprice = MAKE_FIXED(100);
    int32_t workers = MAKE_FIXED(7);

    
    printf("%f\n", MAKE_REAL(mulf(officeprice, divf(workers,MAKE_FIXED(2)))));

}
0___________
  • 60,014
  • 4
  • 34
  • 74