Уведомления

Группа в Telegram: @pythonsu

#1 Март 15, 2015 09:34:08

icef
Зарегистрирован: 2015-03-15
Сообщения: 1
Репутация: +  0  -
Профиль   Отправить e-mail  

Ошибка в расчётах

пишу:

pi = 3.14
s = pi + 2
l = len(str(s))
print("pi =", pi, ",", "s =", s, l)

Результат:
3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:16:31)
Python Type “help”, “copyright”, “credits” or “license” for more information.

pi = 3.14 , s = 5.140000000000001 17

Вопрос:
Откуда он взял единицу на пятнадцатом знаке после запятой?!

Офлайн

#2 Март 15, 2015 10:11:41

FishHook
От:
Зарегистрирован: 2011-01-08
Сообщения: 8312
Репутация: +  568  -
Профиль   Отправить e-mail  

Офлайн

#3 Март 15, 2015 10:14:28

TroSer
От: Харьков
Зарегистрирован: 2013-11-13
Сообщения: 65
Репутация: +  3  -
Профиль   Отправить e-mail  

Ошибка в расчётах

уже отвечали тут http://python.su/forum/topic/26534/

It’s important to realize that this is, in a real sense, an illusion: the value in the machine is not exactly 1/10, you’re simply rounding the display of the true machine value. This fact becomes apparent as soon as you try to do arithmetic with these values

>>> 0.1 + 0.2
0.30000000000000004


ну и значение Pi можно брать из модуля math, math.pi

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version