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: Trigonometric bug
Type: behavior Stage:
Components: Extension Modules, Windows Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ShubhamSingh.er, mark.dickinson, paul.moore, r.david.murray, steve.dower, steven.daprano, tim.golden, tim.peters, zach.ware
Priority: normal Keywords:

Created on 2016-07-02 13:32 by ShubhamSingh.er, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
Sin.jpg ShubhamSingh.er, 2016-07-02 13:32
Messages (4)
msg269711 - (view) Author: Shubham Singh (ShubhamSingh.er) Date: 2016-07-02 13:32
The value of sine and tangent, using math module is showing wrong.Screenshot is attached.
msg269725 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-07-02 17:54
I would imagine that this is a consequence of platform-dependent floating point math.  Not to mention working with an irrational number.  See also issue 8309.
msg269728 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2016-07-02 19:09
Python's floats are emphatically not doing symbolic arithmetic - they use the platform's binary floating point facilities, which can only represent a subset of rationals exactly.  All other values are approximated.

In particular, this shows the exact value of the approximation used for pi:

>>> import math
>>> from decimal import Decimal
>>> Decimal(math.pi)
Decimal('3.141592653589793115997963468544185161590576171875')

That isn't the mathematical pi, but is the closest approximation representable as a platform Python binary float (same as type "double" in the platform C).

Here's the difference between math.pi and a better decimal approximation to pi:

>>> Decimal(math.pi) - Decimal("3.141592653589793238462643383279502884")
Decimal('-1.224646799147353177224094238E-16')

So math.pi is a little bit smaller than the mathematical pi.

Mathematically, using the subtraction formula for sine:
 
sin(pi - e) = sin(pi)*cos(e) - sin(e)*cos(pi) =
0*cos(e) - sin(e)*-1 =
0 + sin(e) =
sin(e)

So mathematically sin(pi - e) = sin(e), and if |e| is close to 0 then sin(e) ~= e.

For that reason, it's not a coincidence that the result you got for math.sin(math.pi) = 1.22464...e-16 is approximately equal to the difference (shown above) between the mathematical pi and math.pi.  It's not due to gross inaccuracy in sine, it's due to that pi isn't exactly representable as a Python float.

Note that this has nothing to do with Python specifically.  You'll see the same kind of behaviors in any language exposing the platform floating point facilities.

If you need to do symbolic computations, then you need much fancier facilities.
msg269749 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016-07-03 02:08
I know this issue is closed, but for future reference, ShubhamSingh.er, if you submit any further bug reports, please don't submit screen shots unless necessary. Just copy and paste the text from your terminal into the issue tracker. A screen shot is more work for you, less useful to us, and impossible for the blind and visually-impaired to work with using a screen reader.
History
Date User Action Args
2022-04-11 14:58:33adminsetgithub: 71627
2016-07-03 02:08:05steven.dapranosetnosy: + steven.daprano
messages: + msg269749
2016-07-02 19:09:57tim.peterssetstatus: open -> closed

nosy: + tim.peters
messages: + msg269728

resolution: not a bug
2016-07-02 17:54:36r.david.murraysetnosy: + r.david.murray, mark.dickinson
type: resource usage -> behavior
messages: + msg269725
2016-07-02 13:32:29ShubhamSingh.ercreate