-3

Does R have a known problem with uniroot and handling floating points?

>str(uniroot(function(x) x*(x^2-1) + .5, lower = -2, upper = 2,
+                  tol = 0.0001))

List of 4
$ root      : num -1.19
$ f.root    : num -2.55e-07
$ iter      : int 7
$ estim.prec: num 5e-05
> -1.19 * ( 1.19 ^ 2 - 1 ) + 0.5 
[1] 0.004841

Clearly the value of f.root is not equal to the value of the function calculated by hand.

gkb0986
  • 3,099
  • 1
  • 24
  • 22

1 Answers1

3

Your value of root is shown to only 2 s.f., but it estimates the precision at 5e-5. That suggests it "knows" other digits in the answer that you are not looking at. Try to print out root-1.19 and you will see what I mean.

In particular, str is intended as a quick way to view the structure of an R object, so it intentionally prints a limited number of digits: the default value of digits.d is 3 (see ?str). Just printing the results (as in uniroot(...) or u1 <- uniroot(...); u1 would have shown you more digits and perhaps have avoided the confusion.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Floris
  • 45,857
  • 6
  • 70
  • 122
  • 1
    You're welcome. If this solves your question please "accept" the answer. That, rather than a comment, tells the community "this answer solves this question". – Floris Jan 31 '13 at 14:30
  • I want to give a quick shout-out to @benbolker who edited the answer and made it more complete! – Floris Jan 31 '13 at 16:25