Ask Your Question

Revision history [back]

Incorrect rounding at low precision

I'm trying to use Sage to implement a slightly modified bisection algorithm on a certain function, and at each step, displaying the interval endpoints as well as the function at these endpoints. I don't need much precision for the latter numbers; I just want an indication of the size and sign of these numbers. However, the numerical_approx() function is giving me weird responses when I specify the number of digits. For example, the following calculation (which is the function value at the greater endpoint, in the final step of the algorithm)

numerical_approx(1/2427673471984599040*(1530627*sqrt(4630419677705) - 4630419677705)*(sqrt(4630419677705) - 482051) + 482051/524288)

evaluates to

1.64597827478907e-7

which appears to be "reasonable", though I don't know for certain. However, when I write,

numerical_approx(1/2427673471984599040*(1530627*sqrt(4630419677705) - 4630419677705)*(sqrt(4630419677705) - 482051) + 482051/524288,digits=3)

it evaluates to

-0.0000160

which is clearly unreasonable: it's the wrong sign and it's too large. Further more, these endpoints come back to this same number a lot, despite how the input changes. At one point, in the middle of this string of -0.0000160s, Sage estimates 0.000, just once, and goes back to -0.0000160.

Changing the precision to 4 and 5 does seem to improve the situation (correct sign, more reasonable magnitude), but the significant digits are all different!

So, my questions are:

  1. What is going wrong here?
  2. Can I fix it?
  3. What methods could I use to be certain that these approximations have 3 correct significant figures?