rounding function python

Rounding down operation is referred Floor, represented by the mathematical symbol ⌊⌋; ceil operation referred Ceiling, represented by the mathematical symbol ⌈⌉. E.g:

⌊59/60⌋=0

⌈59/60⌉=1

⌊-59/60⌋=-1

 ⌈-59/60⌉=0

 Rounding down up to only the number of function numbers after the decimal point operation number is not zero,

If it is an integer, it returns to the integer itself
operands fractional non-zero:
given 4.9
calling function with the whole is rounded down to give 4
called with rounding up function is obtained 5

 

  • Rounding up: the smallest integer greater than their own;
  • Rounding down: smaller than their largest integer;
  • Round off: closer to their integer;

 Syria little nonsense, directly on the code:

First, I had to remind an easily overlooked or confused the issue - in general, this end is 0.5 decimal 5, rounded to the nearest whole should carry. This carry mean: -0.5 → -1; 0.5 → 1. i.e., different positive and negative cases, both directed away from zero, so that the greater the absolute value of the carry direction

Roundup: Math.ceil ()
Import Math

Math.ceil (-0.5)
>>> 0

Math.ceil (-0.9)
>>> 0

Math.ceil (0.3)
>>>. 1
as seen in code, math. ceil () rounds up strictly follow, all fractional values are larger toward the direction of rounding, whether positive and negative numbers are so

Rounding: round ()

round (-2.5)
>>> -2

round (-1.5)
>>> -2

round (-0.5)
>>> 0

round (0.5)
>>>. 1

round (for 1.5)
>>> 2

round (2.5)
> 2 >>
 as shown code, round () is not passed when the second parameter default rounding, rounding off is in accordance with the specific. However, it is worth mentioning here that the end of the handling of the decimal 5: When a front end 5 is odd: rounding (such -1.5,1.5 processing result) to a larger absolute value of direction; and when the end of the 5 the former is an even number: rounding to the end (processing results, such as -2.5, -0.5, 0.5 and 2.5).

Rounding down: math.floor ()

Math.floor (-0.3)
>>> -1

Math.floor (0.9)
>>> 0
easily and faithfully rounding down, will not be discussed

Two interesting and special Python rounding: int (), divisible "//"

int()

int (-0.5)
>>> 0

int (-0.9)
>>> 0

int (0.5)
>>> 0

int (0.9)
>>> 0
stating: int () function is a "round toward zero", rounding direction always makes the results less than the absolute value of the fractional

"//"

(-1) -0.5 // # 2
>>> -1

(-3) -1.5 // # 2
>>> -2

1 @ 2 # 0.5
>>> 0

. 3 1.5 # @ 2
>>> 1
stating: "divisible" symbol calculation result faithfully rounding down, and Math.floor () as the processing result

 

in conclusion:

Roundup: Math.ceil ()
rounds down: Math.floor (), divisible by "//"
rounding: round () - odd rounding away from 0, to the end of the even rounding; or words: Odd carry, to the end of the even
rounding to 0: int ()
due to the recently doing arithmetic problems, many algorithms questions to be involved (0-1) / 2 such boundary is calculated, this time we want this to -0.5 rounding 0, and you want the result (4-1) / 2 rounded 1.5 to 1, i.e. the number of positive rounding down, rounding up negative number, all in all is rounded to 0, we can use this time int ()

https://blog.csdn.net/weixin_41712499/article/details/85208928

Guess you like

Origin www.cnblogs.com/abels0025/p/11278562.html