round up

i declare double as
1
2
3
4
5
6
double amount,rate = 0.3333;
int money = 30;

amount = rate * money;

printf("%f", amount);


so the output will be
9.999


if i want to round it up the
9.999
equal to
10
?


Use the cmath function.... there is a command in it (ceil ) which allows rounding of integers...
refer to

www.cplusplus.com/reference/clibrary/cmath/ceil/
Last edited on
you probably don't want to "always round up", but you want to "round like you learned it in scool", right? Means something like "round up after .5 and round down before .5".

One very simple way is amount = (int)(rate*money+0.5);. You can also use the function "round".

Beware: Rounding stuff is hard and complicated. There are like 10 different ways of rounding all differ in subtile things like how to round exactly 0.5 or -0.5 and some are even round different each time called. Some are inaccurate and for high-performance applications (some methods are 1000 times slower like the one here most probably just trashes your floating point pipeline).

But most of the time, the code above is just fine. ;-)

Ciao, Imi
Last edited on
Nice article, Duoas. That gives a good impression about the complexity. :-) (And it is even only about algorithms, not the different performance aspects.)


BTW: If you like to add this, there's one algorithm I encountered in the wild missing in your article. It's a bit esoteric and probably not very common, but it has its places: Rounding one decimal/bit after another.

It takes a number and starts at the least significant decimal place and round this (using any other algorithm). Then it goes on to the next place etc..

So for example, (if .5 is rounded up to 1), you would get:

0.4 -> 0
0.44444 -> 0
0.44445 -> 1
0.45 -> 1
0.5 -> 1

The place where you need this strange kind of rounding is when you need deterministic results for applying the rounding to the same number multiple times (for different precissions). So it's needed if you first round a number to 3 digits after the point and later again to 2 digits, and you have to get the same result like if you would have rounded it to 2 digits in the first place.


(To be honest, I've only seen this necessity in one statistic application, but there it was crucial ;).

Ciao, Imi.
Last edited on
I don't see how you could reasonably implement that using standard floating point...

In general computing, rounding typically only occurs at the decimal point. This is deterministic.

If you are doing something specialized, such as dealing with money, then you need to have a specialized container and methods to handle the values and round at the given powers.

I hope this makes sense...
In general computing, rounding typically only occurs at the decimal point. This is deterministic.

Yes and no. It is deterministic for the one-step rounding, but if the value may or may not have been rounded to a different precission (which is short for something like f = round(f*1000)/1000), then the "normal" rounding algorithms are not deterministic.

If you are doing something specialized, such as dealing with money, then you need to have a specialized container and methods to handle the values and round at the given powers.

IIRC it was about statistics comparing values from different sources and have to get the same result even if the intermediate steps did round their results.

But I agree. It may be too esoteric to mention in the article.

Ciao, Imi.
Yes, exactly.

It is an interesting problem you had. (I had never heard of it before.)
Topic archived. No new replies allowed.