Numerical Error
Using R and SAS and other softwares, we some times need to pay
attention to numerical error. A real number is represented in the
software
as
a binary, finite length (32 bit) discrete number. This lead to error,
Or rounding error. This error some times are not ignorable.
Example:
Mathematically, we have, for any x, the identity
[ sqrt(x+1) - sqrt(x) ] = 1/ [ sqrt(x+1) + sqrt(x) ]
When x is a large number, for example x=500009999998, R calculates
> x<- 500009999998
> sqrt(x+1) - sqrt(x)
[1] 7.071067e-07
> 1/( sqrt(x+1) + sqrt(x))
[1] 7.070997e-07
Try
other large x values, you get similar result (that answers are
different). You may argue that this difference is small, until you try
the following
> x <- 500009999998
> 666668888880*(sqrt(x+1) - sqrt(x))
[1] 471406
>
> 666668888880/(sqrt(x+1) + sqrt(x))
[1] 471401.4
The difference is 4.6.
Question: which answer among the two is more acurate? (closer to the true value)?
We can use the R package Rmpfr to calculate the "TRUE" value. This package let you use arbitry length to represent a number, not 32 not 64, can
be
any user defined length bit. We of course are interested when
using longer bits to represent a real number. say 120 bits.
You
will find out that the expression using dividing is more acurate, i.e.
666668888880/(sqrt(x+1)+sqrt(x)) is better.