Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet113/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   109   110   111   112   113   114   115   116   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Rounding

The math module also has a couple of handy functions that convert floating point numbers

to integral floating point numbers, i.e. round up or down to the nearest whole number, but

give  a  result  that  is  still  floating  point.  The  function  floor(x)  converts  x  to  the  largest

whole number (integral) value less than or equal to x, and similarly ceil(x) converts x  to

the smallest integral value greater than or equal to x:

math.floor(5.25) # 5.0

math.ceil(5.25) # 6.0

Even though these functions remove any fractional part, after the decimal point, in both

cases  the  data  type  returned  is  floating  point.  Hence  these  could  be  converted  to  actual

Python integers using the int() function:

int(math.floor(5.25)) # 5

int(math.ceil(5.25)) # 6

Note that int(x) is the integer part of x and so int(math.floor(x)) is not the same as int(x)

when x is negative:

int(math.floor(-5.25)) # -6; the integer less than the value

int(-5.25) # -5; the integer part of the value

The more usual kind of rounding, to the nearest whole number or decimal place, would

be done with the inbuilt round() function; this is not in the math module.

round(8.49) # 8.0 ; rounded down

round(8.51) # 9.0 ; rounded up



round(3.141592, 1) # 3.1 ; to one decimal place

round(3.141592, 3) # 3.142 ; to three decimal places

round(9621, -2) # 9600.0 ; to nearest hundred

As  illustrated  above,  the  second  argument  in  round()  specifies  how  many  decimal

places the rounding is done to, and this number can be negative to round off at positions

above 1.0; to the nearest ten, hundred, thousand etc. Note that even rounded numbers will

still be subject to floating point errors.

In  Python  3  the  behaviour  of  round()  changed  slightly.  Consider  the  case  when  the

second argument takes its default value, 0. In Python 2, the function rounds to the number

with the larger magnitude if the fractional part of the argument is 0.5, but in Python 3 this

only  happens  if  that  number  is  even,  otherwise  it  rounds  to  the  number  with  the  smaller

magnitude.

round(7.5) # 8.0 in both Python 2 and Python 3

round(8.5) # 9.0 in Python 2, 8.0 in Python 3

Thus in Python 3 you always get an even number in this circumstance.


Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   109   110   111   112   113   114   115   116   ...   514




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish