[Python]#7 Rounding Methods

Clay Ryu's sound lab·2023년 8월 10일
0

Framework

목록 보기
21/49

Rounding methods dictate how we adjust a given number to a specified number of decimal places or significant digits. There are several rounding methods in use:

  1. Round-half-up(사사오입):
    In this method, we round the number to the nearest integer. If the fraction is exactly 0.5, the number is rounded up to the next higher integer.
Example: 0.5 → 1; -0.5 → 0; 3.5 → 4; 3.4 → 3; -3.5 → -3
  1. Round-half-down(오사육입):
    In this method, we round the number to the nearest integer. If the fraction is exactly 0.5, the number is rounded down to the next lower integer.
Example: 0.5 → 0; -0.5 → -1; 3.5 → 3; 3.6 → 4; -3.5 → -4
  1. Round-half-to-even(bankers' rounding, 오사오입):
    In this method, we round the number to the nearest even integer when the fractional part is exactly 0.5. This method aims to eliminate bias introduced by consistently rounding up or down and distributes the rounding errors equally across numbers.
Example: 0.5 → 0; -0.5 → 0; 3.5 → 4; 2.5 → 2; -3.5 → -4
  1. Round-half-to-odd:
    In this method, we round the number to the nearest odd integer when the fractional part is exactly 0.5.
Example: 0.5 → 1; -0.5 → -1; 3.5 → 3; 2.5 → 3; -3.5 → -3
  1. Round-up (ceil):
    In this method, we always round the number up to the next higher integer, regardless of its fractional part.
Example: 0.1 → 1; -0.1 → 0; -3.5 → -3
  1. Round-down (floor):
    In this method, we always round the number down to the next lower integer, regardless of its fractional part.
Example: 0.9 → 0; -0.9 → -1; -3.5 → -4
  1. Round-toward-zero (truncation):
    In this method, we round the number toward zero by dropping the fractional part.
Example: 0.9 → 0; -0.9 → 0; -3.5 → -3
  1. Round-away-from-zero:
    In this method, we round the number away from zero by always rounding the fractional part up for positive numbers and down for negative numbers.
Example: 0.1 → 1; -0.1 → -1; 3.5 → 4; -3.5 → -4

These rounding methods can be essential when working with floating-point numbers, especially in cases where rounding errors may accumulate and lead to significant inaccuracies in calculations.

profile
chords & code // harmony with structure

0개의 댓글