0

To calculated the value of the derivative of a given function Func at a given point x, to get a good precision one would think this:

a = Fun( x - Number.MIN_VALUE)
b = Func( x + Number.MIN_VALUE)
return (b-a)/(2*Number.MIN_VALUE)

Now for any x + Number.MIN_VALUE (or x - Number.MIN_VALUE) both return x in javascript.

I tried of different values, and 1 + 1e-15 returns 1.000000000000001. Trying to get more precision, 1 + 1e-16 returns 1. So I'll have to use 1e-15 instead of Number.MIN_VALUE which is 5e-324.

Is there a way to get a better precision in this case in javascript?

Brent Worden
  • 10,624
  • 7
  • 52
  • 57
Swair
  • 1,503
  • 2
  • 15
  • 27
  • `1 - 1e-15` returns: `0.999999999999999`; `1 - 1e-16` returns: `0.9999999999999999`; I don't see the problem here. – Cerbrus Dec 12 '12 at 12:53
  • oops..edited. `1 + 1e-15` and `1 + 1e-16`. – Swair Dec 12 '12 at 12:56
  • There's only a certain "range" of precision you can work with, this has to do with the way [Floating Point](http://en.wikipedia.org/wiki/Floating_point) numbers work. This is why you can't really calculate with `1.0` and `1e-17` or smaller, while the system _can_ represent those smaller numbers. – Cerbrus Dec 12 '12 at 13:06
  • 1
    I've had [what every computer scientist should know about floating point arithmetic](http://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CDcQFjAA&url=http%3A%2F%2Fperso.ens-lyon.fr%2Fjean-michel.muller%2Fgoldberg.pdf&ei=d4LIUOkYycqsB-iSgKAK&usg=AFQjCNEOKaGOeToQrLFAmG-_fEK7E9zNrg&sig2=etaIxo7CWAGMeslA_pjnVA&bvm=bv.1354675689,d.bmk) in my reading list for a while now. I guess this is the time. – Swair Dec 12 '12 at 13:13

1 Answers1

3

This is not at all about javascript, actually.

Reducing the separation between the points will not increase your precision of the derivative. The values of the function in the close points will be computed with some error in the last digits, and finally, your error will be much larger than the point separation. Then you divide very small difference which has a huge relative error by a very small number, and you most likely get complete rubbish.

The best way to get a better value of the derivative is to calculate function in multiple points (with not very small separation) and then construct an approximation polynomial and differentiate it in the point of your interest.

pavi2410
  • 1,220
  • 2
  • 12
  • 16
begemotv2718
  • 868
  • 6
  • 14
  • Ok that sounds good. Can you point me to some library which does this (preferably a higher language library..better if python or ruby). I am digging sympy source code. Haven't found anything useful yet. – Swair Dec 12 '12 at 13:02
  • Please have a look at the http://en.wikipedia.org/wiki/Automatic_differentiation for some robust methods and some references to the libraries. – begemotv2718 Dec 12 '12 at 13:33