How to Properly Round-Up Half Float Numbers

How to properly round-up half float numbers?

The Numeric Types section documents this behaviour explicitly:

round(x[, n])
x rounded to n digits, rounding half to even. If n is omitted, it defaults to 0.

Note the rounding half to even. This is also called bankers rounding; instead of always rounding up or down (compounding rounding errors), by rounding to the nearest even number you average out rounding errors.

If you need more control over the rounding behaviour, use the decimal module, which lets you specify exactly what rounding strategy should be used.

For example, to round up from half:

>>> from decimal import localcontext, Decimal, ROUND_HALF_UP
>>> with localcontext() as ctx:
... ctx.rounding = ROUND_HALF_UP
... for i in range(1, 15, 2):
... n = Decimal(i) / 2
... print(n, '=>', n.to_integral_value())
...
0.5 => 1
1.5 => 2
2.5 => 3
3.5 => 4
4.5 => 5
5.5 => 6
6.5 => 7

How to round float 0.5 up to 1.0, while still rounding 0.45 to 0.0, as the usual school rounding?

It is actually currently considered proper to NOT blindly round *.5 up. Rather, it is proper to round *.5 to the nearest even number. Python 3 implements this "proper" form of "banker rounding", but a lot of other languages don't (yet). Blindly rounding *.5 up produces a slight bias, but "banker rounding" helps to balance it it out. See this thread for more info. So...

Method 1

You could conditionally use aceil(...) function (from the math module for the rounding up aspect. You'll have to do it conditionally in order to also maintain the regular rounding behavior for values less than 0.5. Try something like the following (note that this isn't extremely robust in that it only works on positive values...it should be able to be easily adapted to work with both positive and negative values though):

import math

val = 1.5
x = 0

if (float(val) % 1) >= 0.5:
x = math.ceil(val)
else:
x = round(val)

Note that a ceil(...) function will return an integer, not a float. This shouldn't be a major issue, but now you are aware.

Method 2

From the post I linked to above, it looks like another option is to use the decimal module to emulate the "old" way of rounding's behavior. I'm kind of copy & pasting from there, but here you go:

import decimal

x = decimal.Decimal('1.5').quantize(decimal.Decimal('1'),
rounding=decimal.ROUND_HALF_UP)

Supposedly the decimal.ROUND_HALF_UP form of rounding is what you are looking for. This way you don't have to use a ceil(...) function conditionally.

I'm guessing that this was marked as a duplicate of another because a little digging would have given you more than enough info on this topic. (I didn't mark it as a duplicate, I'm just assuming that is why someone else did.)

Python 3.x rounding half up

Rounding is surprisingly hard to do right, because you have to handle floating-point calculations very carefully. If you are looking for an elegant solution (short, easy to understand), what you have like like a good starting point. To be correct, you should replace decimal.Decimal(str(number)) with creating the decimal from the number itself, which will give you a decimal version of its exact representation:

d = Decimal(number).quantize(...)...

Decimal(str(number)) effectively rounds twice, as formatting the float into the string representation performs its own rounding. This is because str(float value) won't try to print the full decimal representation of the float, it will only print enough digits to ensure that you get the same float back if you pass those exact digits to the float constructor.

If you want to retain correct rounding, but avoid depending on the big and complex decimal module, you can certainly do it, but you'll still need some way to implement the exact arithmetics needed for correct rounding. For example, you can use fractions:

import fractions, math

def round_half_up(number, dec_places=0):
sign = math.copysign(1, number)
number_exact = abs(fractions.Fraction(number))
shifted = number_exact * 10**dec_places
shifted_trunc = int(shifted)
if shifted - shifted_trunc >= fractions.Fraction(1, 2):
result = (shifted_trunc + 1) / 10**dec_places
else:
result = shifted_trunc / 10**dec_places
return sign * float(result)

assert round_half_up(1.49) == 1
assert round_half_up(1.5) == 2
assert round_half_up(1.51) == 2
assert round_half_up(2.49) == 2
assert round_half_up(2.5) == 3
assert round_half_up(2.51) == 3

Note that the only tricky part in the above code is the precise conversion of a floating-point to a fraction, and that can be off-loaded to the as_integer_ratio() float method, which is what both decimals and fractions do internally. So if you really want to remove the dependency on fractions, you can reduce the fractional arithmetic to pure integer arithmetic; you stay within the same line count at the expense of some legibility:

def round_half_up(number, dec_places=0):
sign = math.copysign(1, number)
exact = abs(number).as_integer_ratio()
shifted = (exact[0] * 10**dec_places), exact[1]
shifted_trunc = shifted[0] // shifted[1]
difference = (shifted[0] - shifted_trunc * shifted[1]), shifted[1]
if difference[0] * 2 >= difference[1]: # difference >= 1/2
shifted_trunc += 1
return sign * (shifted_trunc / 10**dec_places)

Note that testing these functions brings to spotlight the approximations performed when creating floating-point numbers. For example, print(round_half_up(2.175, 2)) prints 2.17 because the decimal number 2.175 cannot be represented exactly in binary, so it is replaced by an approximation that happens to be slightly smaller than the 2.175 decimal. The function receives that value, finds it smaller than the actual fraction corresponding to the 2.175 decimal, and decides to round it down. This is not a quirk of the implementation; the behavior derives from properties of floating-point numbers and is also present in the round built-in of Python 3 and 2.

Pythonic way to ROUND_HALF_UP float with an arbitrary precision

As you said that doesn't work if you try to quantize with numbers greater than 1:

>>> Decimal('1.5').quantize(Decimal('10'))
Decimal('2')
>>> Decimal('1.5').quantize(Decimal('100'))
Decimal('2')

But you can simply divide, quantize and multiply:

from decimal import Decimal, ROUND_HALF_UP

def round_my(num, precision):
N_PLACES = Decimal(10) ** precision
# Round to n places
return (Decimal(num) / N_PLACES).quantize(1, ROUND_HALF_UP) * N_PLACES

However that only passes the tests if you input Decimal and compare to Decimal:

assert round_my('1.53', -1) == Decimal('1.5')
assert round_my('1.55', -1) == Decimal('1.6')
assert round_my('1.63', -1) == Decimal('1.6')
assert round_my('1.65', -1) == Decimal('1.7')
assert round_my('1.53', -2) == Decimal('1.53')
assert round_my('1.53', -3) == Decimal('1.53')
assert round_my('1.53', 0) == Decimal('2')
assert round_my('1.53', 1) == Decimal('0')
assert round_my('15.3', 1) == Decimal('20')
assert round_my('157.3', 2) == Decimal('200')

As noted in the comments it's possible to use scientific notation decimals as "working" quantize arguments, which simplifies the function:

def round_my(num, precision):
quant_level = Decimal('1e{}'.format(precision))
return Decimal(num).quantize(quant_level, ROUND_HALF_UP)

This also passes the test cases mentioned above.

How to properly round half down a decimal number?

Something like this?

import sys, signal, json, time
import random
import math
num = 0.049852124
# where 1 is in the position you want to truncate
numCoins = num - math.fmod(num, 0.001)
print numCoins # 0.049

How to round a floating point number up to a certain decimal place?

8.833333333339 (or 8.833333333333334, the result of 106.00/12) properly rounded to two decimal places is 8.83. Mathematically it sounds like what you want is a ceiling function. The one in Python's math module is named ceil:

import math

v = 8.8333333333333339
print(math.ceil(v*100)/100) # -> 8.84

Respectively, the floor and ceiling functions generally map a real number to the largest previous or smallest following integer which has zero decimal places — so to use them for 2 decimal places the number is first multiplied by 102 (or 100) to shift the decimal point and is then divided by it afterwards to compensate.

If you don't want to use the math module for some reason, you can use this (minimally tested) implementation I just wrote:

def ceiling(x):
n = int(x)
return n if n-1 < x <= n else n+1

How all this relates to the linked Loan and payment calculator problem:

screenshot of loan calculator output

From the sample output it appears that they rounded up the monthly payment, which is what many call the effect of the ceiling function. This means that each month a little more than 112 of the total amount is being paid. That made the final payment a little smaller than usual — leaving a remaining unpaid balance of only 8.76.

It would have been equally valid to use normal rounding producing a monthly payment of 8.83 and a slightly higher final payment of 8.87. However, in the real world people generally don't like to have their payments go up, so rounding up each payment is the common practice — it also returns the money to the lender more quickly.

How do you round UP a number?

The math.ceil (ceiling) function returns the smallest integer higher or equal to x.

For Python 3:

import math
print(math.ceil(4.2))

For Python 2:

import math
print(int(math.ceil(4.2)))

Python Rounding (Properly)

Python uses "banker's rounding" for x.5. Even numbers round down, odd numbers round up. You are saying it "should" round up, but that's just your definition. It's not the only definition.

If you always want rounding up, do:

result = int(x+0.5)

round() for float in C++

It's available since C++11 in cmath (according to http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf)

#include <cmath>
#include <iostream>

int main(int argc, char** argv) {
std::cout << "round(0.5):\t" << round(0.5) << std::endl;
std::cout << "round(-0.5):\t" << round(-0.5) << std::endl;
std::cout << "round(1.4):\t" << round(1.4) << std::endl;
std::cout << "round(-1.4):\t" << round(-1.4) << std::endl;
std::cout << "round(1.6):\t" << round(1.6) << std::endl;
std::cout << "round(-1.6):\t" << round(-1.6) << std::endl;
return 0;
}

Output:

round(0.5):  1
round(-0.5): -1
round(1.4): 1
round(-1.4): -1
round(1.6): 2
round(-1.6): -2


Related Topics



Leave a reply



Submit