Search code examples
pythonsympysymbolic-integration

While integrating sin and cos using sympy the output must be same, but integration of cos gives a value of order -16


While integrating sin and cos using sympy the output must be same, but integration of cos gives a value of order -16 which is essentially 0 upon rounding. Any reason why it gives such a low value for cos and a direct 0 for sin?

i/p: f=(sp.integrate(sp.cos(x),(x,-np.pi,np.pi)))
o/p: 2.44929359829471e-16


i/p: f=(sp.integrate(sp.sin(x),(x,-np.pi,np.pi)))
o/p: 0

Solution

  • Floating point numbers (basically decimal numbers) have limited precision which leads to some strange things from time to time.

    For example 0.1 + 0.2 will give 0.30000000000000004

    In your example, the e-16 is very small and is basically zero as you said, but somewhere in the calculation there was probably a rounding error for the cosine values but it just so happened not to be the case for the sine values. You can try a different range for the integral of sin and probably find the same thing happening.