I am using python shell as a calculator, but I don't know why it can't handle -28/96 as an input for atan function:
My question is: Why is this wrong?
I am using python shell as a calculator, but I don't know why it can't handle -28/96 as an input for atan function:
My question is: Why is this wrong?
-28/96
is -1 in python 2 (integer division is the default for integers)
Turn that to -28/96.0
you'll get the result you want.
(or switch to python 3)
BTW: 3.14 is a very bad approximation for Pi
unless you like ovoïd circles :)
Use math.pi
instead.
(I just saw that you wrote 28.0/96
the first time, which was correct! Your subconscient was aware of the bug :))
Python handles it correctly. Python interprets "-28/96" as int divides int, so it returns the largest previous integer of "-28/96", namely, -1. What you need is "-28.0/96" and then python 2.7 will interpret the expression as float division and handle it as your wish.
Also, the same expression will return a double precise float in python 3, namely, "-0.2916666666666667".
Looking at the result of -28/96
:
>>> -28/92
-1
>>> math.atan(-1)
-0.7853981633974483
So python uses integer division for integers, not float division.
>>> math.atan(-28.7/96)
-0.2905008632137447
gives the correct result