Why do round() and math.ceil() give different values for negative numbers in Python 3? [duplicate]

2024/7/5 11:59:02

Why do round() and math.ceil() give different values for negative numbers in Python 3?

For example,

round(-5.5) = -6
round(-5.7) = -6
math.ceil(-5.5) = -5
math.ceil(-5.7) = -5
Answer

The functions execute different mathematical operations:

  • round() rounds a float value to the nearest integer. If either of the two integers on either side are equal distance apart, the even number is picked.

    Here, the nearest integer is always -6; for -5.5 the nearest even integer is -6, not -5.

  • math.ceil() returns the smallest integer greater than or equal to the float value.

    Here, the next integer greater than the inputs is always -5 (remember that negative numbers decrease away from zero).

I think the biggest confusion you have comes from the even rounding rule. Had you picked -4.5, you'd have seen round() return -4:

>>> import math
>>> math.ceil(-4.5)
-4
>>> round(-4.5)
-4

From the round() function documentation:

if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2).

Rounding half to the nearest integers always has to break the tie somehow, and different programming languages don't always agree on how to do this, the IEEE 754 standard defines 5 different methods. Python 3 uses the IEEE 754 default and rounds halves to even, because this method has the advantage that it is free of sign bias and in reasonable distributions of numbers will result in the same average values.

https://en.xdnf.cn/q/120576.html

Related Q&A

getting the value from text file after the colon or before the colon in python

I only need the last number 25 so i can change it to int(). I have this text file:{"members": [{"name": "John", "location": "QC", "age": 25}…

How to remove the background of an object using OpenCV (Python) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

How to move zeros to the end of a list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

My program only appends one user input to list when main is looped

This is part of my code for a guessing game. I want to count the guesses of a player, and then append their name and number of guesses to a list that is later written or appended to file. As of now, it…

How to split a python dictionary for its values on matching a key

my_dict1 = {a:1, chk:{b:2, c:3}, e:{chk:{f:5, g:6}} }I would like to loop through the dict recursively and if the key is chk, split it. Expected output:{a:1, b:2, e:{f:5}} {a:1, c:3, e:{f:5}} {a:1, b:2…

PyException: ImportError: No module named domreg

I am getting the below error while running this script ("from xml.dom import minidom") from chaquopy androi application python console. PyException: ImportError: No module named domreg

Plotting polynomial with given coefficients

Im trying to plot a polynomial with coefficients given in array:input: [an,a(n-1),...,a0] output: plot of polynomial anx^n + a(n-1)x^(n-1) + ... + a0I would like to use matplotlib polt() function so I…

grouping data using unique combinations

n my below data set, I need to find unique sequences and assign them a serial no ..DataSet :user age maritalstatus product A Young married 111 B young married 222 C young Single 111 D…

Python - Sorting in ascending order in a txt file

I had a huge document that I parsed using regex to give a txt file (json.dump) similar to the following:{"stuff": [{"name": ["frfer", "niddsi", ], "number&q…

How to make setuptools install a wheel containing multiple packages?

Suppose this wheel:M Filemode Length Date Time File - ---------- -------- ----------- -------- --------------------------------------------rw-rw-r-- 1358 26-Sep-2018 21:08…