This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Calculating problem
Type: Stage:
Components: Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, SilentGhost, heme
Priority: normal Keywords:

Created on 2014-11-22 20:19 by heme, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg231527 - (view) Author: (heme) Date: 2014-11-22 20:19
Hi guys 

I am very new to this, (just started my first lines today) so I am using
a book to learn Python. BUT there is something wrong: 

This is my program (from the book): 

# This is not quite true outside of USA
# and is based on my dim memories of my younger years
print("Firstish Grade")
print("1 + 1 =", 1 + 1)
print("2 + 4 =", 2 + 4)
print("5 - 2 =", 5 - 2)
print()
print("Thirdish Grade")
print("243 - 23 =", 243 - 23)
print("12 * 4 =", 12 * 4)
print("12 / 3 =", 12 / 3)
print("13 / 3 =", 13 // 3, "R", 13 % 3)
print()
print("Junior High")
print("123.56 - 62.12 =", 123.56 - 62.12)
print("(4 + 3) * 2 =", (4 + 3) * 2)
print("4 + 3 * 2 =", 4 + 3 * 2)
print("3 ** 2 =", 3 ** 2) 

and this is the programs answer: 

Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32
bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Firstish Grade
1 + 1 = 2
2 + 4 = 6
5 - 2 = 3

Thirdish Grade
243 - 23 = 220
12 * 4 = 48
12 / 3 = 4.0
13 / 3 = 4 R 1

Junior High
123.56 - 62.12 = 61.440000000000005
(4 + 3) * 2 = 14
4 + 3 * 2 = 10
3 ** 2 = 9
>>> 

As you can see, 

print("123.56 - 62.12 =", 123.56 - 62.12) is not = 61.440000000000005 

so my guess is that the interpreter has a malfunction. 

My pc is a compaq mini 110 running (walking;-)) windows xp srv pack 3 

in a std. config. 

brg 

Henning Mentz - Denmark
msg231528 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2014-11-22 20:33
Hi Henning,

this is not a bug. This is to do with how floating point numbers represented in computers. I'd suggest https://en.wikipedia.org/wiki/IEEE_floating_point as a starting point. Briefly, due to binary base that the computers operate on, not every number can be stored/represented precisely. In any case there are plenty of other information online that you can find useful, the easiest solution is to output correctly formatted strings corresponding to your answers.

Good luck.
msg231539 - (view) Author: (heme) Date: 2014-11-22 23:03
Hi SilentGhost 

Thanx for a quick response. 

Sorry to hear that it is not a bug, my old GW basic interpretor from
1988 has no problem with this simple calculation (123.56 - 62.12 =
61.44) but my new Python interpreter cannot give me a correct answer. 

Yes, I know about precision, I have been using C for several years, but
as I am interested in the Raspberry Pi I would like to use Python with
it. 

And as I would use it as a data collector (from instruments), I need
good precision and reliability. 

Is there anything I can do? (exept changing to another language) 

brg 

Henning 

SilentGhost skrev den 22/11/2014 21:33: 

> SilentGhost added the comment:
> 
> Hi Henning,
> 
> this is not a bug. This is to do with how floating point numbers represented in computers. I'd suggest https://en.wikipedia.org/wiki/IEEE_floating_point [1] as a starting point. Briefly, due to binary base that the computers operate on, not every number can be stored/represented precisely. In any case there are plenty of other information online that you can find useful, the easiest solution is to output correctly formatted strings corresponding to your answers.
> 
> Good luck.
> 
> ----------
> nosy: +SilentGhost
> resolution: -> not a bug
> status: open -> closed
> 
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue22917 [2]>
> _______________________________________

Links:
------
[1] https://en.wikipedia.org/wiki/IEEE_floating_point
[2] http://bugs.python.org/issue22917
msg231540 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-11-22 23:23
https://docs.python.org/3/tutorial/floatingpoint.html
msg231542 - (view) Author: (heme) Date: 2014-11-23 00:08
Thank you 

I understand that it is not always that you see what you get (GW basic
has shurely cut off the big precision, and Python doesnt, so I see the
small difference. I will take care of thinking of it next time! 

Sorry for any inconvience. 

brg 

Henning 

Mark Lawrence skrev den 23/11/2014 00:23: 

> Mark Lawrence added the comment:
> 
> https://docs.python.org/3/tutorial/floatingpoint.html [1]
> 
> ----------
> nosy: +BreamoreBoy
> 
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue22917 [2]>
> _______________________________________

Links:
------
[1] https://docs.python.org/3/tutorial/floatingpoint.html
[2] http://bugs.python.org/issue22917
History
Date User Action Args
2022-04-11 14:58:10adminsetgithub: 67106
2014-11-23 00:08:43hemesetmessages: + msg231542
2014-11-22 23:23:39BreamoreBoysetnosy: + BreamoreBoy
messages: + msg231540
2014-11-22 23:03:36hemesetmessages: + msg231539
2014-11-22 20:33:45SilentGhostsetstatus: open -> closed

nosy: + SilentGhost
messages: + msg231528

resolution: not a bug
2014-11-22 20:19:30hemecreate