3

I basically want to know whether the result of my equation (which is a simple one like this x / y) was rounded up or down. The reason is that I have two simple statements after the rounding line like this:

if h % 2 != 0: h = h + 1
if h % 4 != 0: h = h + 2

and based on the direction of the rounding I would choose the + or - operator, so if the result was rounded up and h % 2 != 0 then it would be h = h + 1 and if it was rounded down then h = h - 1.

Does round() give that kind of information?

Also, is my math correct? (I want the result to be dividable by 4)

Derp
  • 33
  • 4
  • Do you control the rounding part or have access to the unrounded result? – user2390182 Oct 21 '20 at 16:25
  • So `h = round(x/y)`? (And no, the result is a plain `int` or `float` value with no memory of where it came from.) – chepner Oct 21 '20 at 16:26
  • `round()` does not report that information. But you can compare the unrounded result to the rounded result to see which is bigger, which will tell you. – John Gordon Oct 21 '20 at 16:27
  • @schwobaseggl Yes and yes. – Derp Oct 21 '20 at 16:30
  • @JohnGordon I thought about another way initially, but didn't know how to implement it. It was something like this: Postpone the rounding and based on the resulting float decide which operator to use (i.e. if less than 5 then `-` else `+`) and then round it. – Derp Oct 21 '20 at 16:33
  • Are you trying to round to the nearest multiple of 2 and 4? – Barmar Oct 21 '20 at 16:34
  • @Barmar Yes, nearest multiple of 4 (which would be a multiple of 2 as well). – Derp Oct 22 '20 at 21:16

4 Answers4

1

Using round() if the number after the given decimal is:

  • >=5 that + 1 will be added to the final value.
  • <5 that the final value will return as is to the mentioned decimals.

But you can use ceil or floor from the math package where it always rounds up or down, respectively.

import math

>>> math.ceil(5.2)
6
>>> math.floor(5.9)
5
feserr
  • 97
  • 1
  • 7
Lorena Gil
  • 341
  • 1
  • 6
  • That way I'd have to always check the result manually which would be counter-intuitive in my case. – Derp Oct 22 '20 at 21:18
1

Try this to round to 4 directly :

import math

h = 53.75
rounded = math.round(h / 4) * 4

if (rounded > h):
  print("Rounded up by " + str(rounded - h))
else:
   print("Rounded down by " + str(h - rounded))
dspr
  • 2,383
  • 2
  • 15
  • 19
  • `math.floor()` always rounds down. – Barmar Oct 21 '20 at 16:35
  • Yes, yes it was a mistake, it's just edited. Thanks – dspr Oct 21 '20 at 16:37
  • Can you please explain your math a bit? Will it always give me the nearest multiple of four? – Derp Oct 22 '20 at 21:42
  • Let's say that you want to round 23 to a multiple of 5 : `23 / 5.0 => 4.6`, then `math.round(4.6) => 5` and finally `5 * 5 => 25` – dspr Oct 23 '20 at 07:56
  • @dspr I got that part, just wanted to know the theory behind it. Anyway I changed my code based on your code above and now I only got 1 statement instead of 2: `if h % 4 != 0: h = D(h / 4).quantize(D('1'), rounding=decimal.ROUND_HALF_UP) * 4` Thanks for the tip. – Derp Oct 23 '20 at 15:56
  • **A**. h/4 give you the position of h in terms of "blocks of 4" stating from 0. **B**. You're averaging to the nearest block beginning using round. This will always correspond to the nearest multiple of 4, although it's not in the right scale yet. **C**. You're going back to the original number scale by multiplying by the value of each block. – JeanOlivier Jan 16 '23 at 18:47
0

Let's say you want to know whether 3.9 and 4.4 were rounded. You can do something like this:

def is_rounded_down(val, ndigits=None):
    return round(val, ndigits) < val

Then you can simply call the function to find out

>>> is_rounded_down(3.9)
False
>>> is_rounded_down(4.4)
True

By default round() doesn't give that information, so you need to check yourself.

Chrispresso
  • 3,660
  • 2
  • 19
  • 31
0

For Python 2.X integer division returns an integer and always rounds down.

add@LM1756:~$ python
Python 2.7.13 (default, Sep 26 2018, 18:42:22)
>>> print 8/3
2
>>> print type(5/2)
<type 'int'>

For Python 3.X integer division returns a float, and so there is no rounding.

add@LM1756:~$ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
>>> print(8/3)
2.6666666666666665
>>> type(8/3)
<class 'float'>
>>>
Alan Dyke
  • 855
  • 6
  • 14