Mundarija kirish I. Bob pythonning o'ziga xosligi va modullar modullarni skriptlar sifatida bajarish Modul qidirish yoʻli standart modullar 1 II.


Download 47.49 Kb.
bet9/10
Sana16.06.2023
Hajmi47.49 Kb.
#1507314
1   2   3   4   5   6   7   8   9   10
Bog'liq
7. PYTHON DASTURLASH TILIDA MODULLAR

2.4 decimal moduli


Suzuvchi nuqtali sonlar bilan ishlashda hisoblash natijasining to‟g‟iri emasligiga
duch kelamiz:

number = 0.1 + 0.1 + 0.1

print(number) # 0.30000000000000004

round() funksiyasini soni yaxlitlash qo‟llash yordamida bu muammoni yechishi


mumkin. Bundan tashqari decimal ichki modulini ishlatish orqali ham bu
muammoni yechish mumkin.
Bu moduldagi Decimal sinfi asosiy sonlar bilan ishlash kompanentasi
hisoblanadi. Bu sinfni qo‟llash uchun Decimal konstruktori yordamida uning
obyektini yaratish zarur. Konstruktorga argument sifatida sonning satrdagi ifodasi
uzatiladi. (2.3.5-rasm)

from decimal import Decimal

number = Decimal("0.1")

2.3.5-rasm
Bundan so‟ng Decimal obyektini arifimetik amallarda mumkin.2.3.6-rasm

from decimal import Decimal

number = Decimal("0.1")

number = number + number + number

print(number) # 0.3

2.3.6-rasm
Decimal bilan amallarda butun sonlarni ham ishlatish mumkin. (2.3.7-rasm)

number = Decimal("0.1")

number = number + 2

2.3.7-rasm
Ammo, kasrli amalarda float va Decimal aralashtirmaslik zarur. (2.3.8-rasm)

number = Decimal("0.1")

number = number + 0.1 #bu yerda xatolik yuz beradi

2.3.8-rasm
Sonning kasr qismdagi raqamlar nechta bo‟lishini quyidagicha aniqlash mummkin. (2.3.9-rasm)

number = Decimal("0.10")

number = 3 * number

print(number) # 0.30

2.3.9-rasm
"0.10" satr sonning kasr qismida ikkita belgi bo‟lishini ko‟rsatadi, agarda oxirgi
belgilar nol bo‟lsa ham. Mos ravishda "0.100" satr sonning kasr qismida 3 belgi
bo‟lishini anglatadi.
Sonlarni yaxlitlash
Decimal obyektlari sonlarni yaxlitlaydigan quantize() metodiga ega. Bu
metodning birinchi argumenti sifatida sonning yaxlitlash formatini ko‟rsatadigan
Decimal obyekti uzatiladi. (2.3.10-rasm)

from decimal import Decimal

number = Decimal("0.444")

number = number.quantize(Decimal("1.00"))

print(number) # 0.44

number = Decimal("0.555678")

print(number.quantize(Decimal("1.00"))) # 0.56

number = Decimal("0.999")

print(number.quantize(Decimal("1.00"))) # 1.00

2.3.10-rasm
Foydalanilayotgan "1.00" satr sonning kasr qismidagi belgilar soni ikkita
belgigacha yaxlitlanishini ko‟rsatadi.
Kelishuv bo‟yicha ROUND_HALF_EVEN o‟zgarmasi soni yuqori tomonga
yaxlitlashni anglatadi, agarda son juft bo‟lmasa va keyingisi 4 dan katta bo‟lsa,
masalan. (2.3.11-rasm)

From decimal import Decimal, ROUND_HALF_EVEN

number = Decimal("10.025")

print(number.quantize(Decimal("1.00"), ROUND_HALF_EVEN)) #

10.02

number = Decimal("10.035")

print(number.quantize(Decimal("1.00"), ROUND_HALF_EVEN)) #

10.04

2.3.11-rasm
Bu erda yaxlitlash tartibi quantize() funksiyasiga ikkinchi parametr sifatida
uzatilgan. "1.00" satri sonning kasr qismini yaxlitlash ikkita belgigacha bo‟lishini
anglatadi. Lekin, birinchi holda, "10.025" kasr qismdagi ikkinchi son-2 juft,
shuning uchun, keyingi son 5 bo‟lishiga qaramasdan, ikki uchga yaxlitlanmayapti.
Ikkinchi holda, "10.025" ikkinchi son-3 juft emas, shuning uchun ham 4 ga
yaxlitlanayapti.
Bunday yaxlitlashdagi tartib har doim ham foydali bo‟lmasligi mumkin, shu
sababli ham, quyidagi o‟zgamaslardan foydalanish orqali tartibni qayta aniqlash
mumkin:

keyingi son 5 yoki 5 dan katta bo‟lsa;

  • ROUND_HALF_DOWN – soni yuqori tomonga yaxlitlaydi, agarda undan

keyingi son 5 dan katta bo‟lsa. (2.3.12-rasm)

number = Decimal("10.026")

print(number.quantize(Decimal("1.00"), ROUND_HALF_DOWN))

#10.03

number = Decimal("10.025")

print(number.quantize(Decimal("1.00"), ROUND_HALF_DOWN))

# 10.02

2.3.12-rasm
ROUND_05UP – faqat 0 ni birga yaxlitlaydi, agarda keyin 5 kelsa. (2.3.13-rasm)

number = Decimal("10.005")

print(number.quantize(Decimal("1.00"), ROUND_05UP)) # 10.01

number = Decimal("10.025")

print(number.quantize(Decimal("1.00"), ROUND_05UP)) # 10.02

2.3.13-rasm
ROUND_CEILING – keyingi son qanday bo‟lishidan qa‟tiy nazar soni
yuqori tomonga yaxlitlaydi. (2.3.14-rasm)

number = Decimal("10.021")

print(number.quantize(Decimal("1.00"), ROUND_CEILING))

# 10.03

number = Decimal("10.025")

print(number.quantize(Decimal("1.00"), ROUND_CEILING))

# 10.03

2.3.14-rasm
ROUND_FLOOR – keyingi son qanday bo‟lishdan qa‟tiy nazar soni yaxlitlamayadi. (2.3.15-rasm)

Number = Decimal("10.021")

print(number.quantize(Decimal("1.00"), ROUND_FLOOR))# 10.02

number = Decimal("10.025")

print(number.quantize(Decimal("1.00"), ROUND_FLOOR))# 10.02

2.3.15-rasm



Download 47.49 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   10




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