0

So just as the question states, I'm wondering how python calculates equations such as

def indeterminate(x):
    return (x - math.sin(x)) / math.pow(x, 3)

where Limit approaches 0. I'm asking because when I input

>>> indeterminate(0.0000001)
0.17205356741102978

Where as

>>> indeterminate(0.00000001)
0.0

Shouldn't the output be approaching 1/6? And if it becomes too close, wouldn't a ZeroDivisionError message be more suitable than 0.0?

Another question I have is how do I calculate the actual number of

indeterminate(0.00000001)

The correct answer using L'hopital's rule should be 1/6 but (0.166666...) and not 0.1720...

sshashank124
  • 31,495
  • 9
  • 67
  • 76
Michael
  • 89
  • 1
  • 3
  • I just realised that '''indeterminate(0.00001)''' is more accurate than if I were to put more zeros. Could someone kindly explain to me as well? Thanks! – Michael Jan 13 '20 at 04:56
  • Here is 1 type of example of how limited precision for the representation of floating point numbers in computers can cause artifacts: https://stackoverflow.com/questions/588004/is-floating-point-math-broken – sshashank124 Jan 13 '20 at 04:57
  • 1
    If you are expecting an "actual number", then you are completely misunderstanding what floating-point numbers *are*. The only value in your code that has an exact representation as a float is that 3. – jasonharper Jan 13 '20 at 04:58
  • If you need perfect precision, consider using a library for that: https://docs.python.org/2/library/decimal.html – sshashank124 Jan 13 '20 at 04:58
  • In general, CPython is at the mercy of the underlying floating point implementation of whatever system is it is running on – Hymns For Disco Jan 13 '20 at 05:04
  • 1
    @sshashank124: `decimal` isn't exact either. It's just configurable-precision, and decimal. It won't prevent rounding error. – user2357112 Jan 13 '20 at 05:05
  • 1
    [Python arbitrary precision library](http://mpmath.org/) using 50 digits returns 0.16666666666666658333333333333336071657591591300437 as desired. Using mpmath for your formula is simple: formula becomes `return (x - mp.sin(x)) /mp.power(x, 3)` which means using mpmath sine and power functions rather than math. – DarrylG Jan 13 '20 at 05:15

0 Answers0