0

I'm having a problem with numpy not returning zero when calculating cos(pi/2).

On my TI calculator this returns exactly zero, however numpy is returning an extremely small number instead.

Is there a way to fix this problem for sin/cos functions in numpy, or perhaps switch to sympy for these calculations so that it only return zero for well known reference angles that are suppose to return zero?

>>> import numpy
>>> numpy.cos(numpy.pi/2.0)
6.123233995736766e-17
pico
  • 1,660
  • 4
  • 22
  • 52
  • Check https://docs.python.org/3/tutorial/floatingpoint.html – yatu Apr 19 '19 at 14:27
  • One solution is to switch to sympy when calculating sin's and cos's, when to switch back to numpy using sp.N(...) function: ``` >>> import numpy as np >>> import sympy as sp >>> value = sp.N(sp.cos(sp.pi/2)) 0 ``` – pico Apr 19 '19 at 14:31
  • if this question was asked before, then where's the link? and also your link to floating point doesn't answer the question. – pico Apr 19 '19 at 14:32
  • A numeric `pi` is not exact. It's an approximation. So any function using it will also be an approximation. – hpaulj Apr 19 '19 at 16:45

0 Answers0