Blue Wizard <hdh6@cornell.edu> wrote in message <c4fc258ae911466fb736f431f4704112@d8g2000yqf.googlegroups.com>...
> I wish to build a vector of increasing floating point number, all
> separated by the same amount. I can do this either of the following
> two ways:
>
> delta is a floating point number, not necessarily whole
> vec is a vector of floating point numbers, not necessarily whole
> n is a floating point number but whole.
>
> for the nth value
>
> 1) vec(n) = vec(n1) + delta
> 2) vec(n) = vec(1) + (n  1) * delta
>
> Question: The numbers delta and vec(1) might not be representable
> exactly as binary numbers. Which method is more accurate in that
> vec(n) is closest to the true value when the computation is performed
> with floating point arithmetic. Thanks in advance.
      
The answer is that 2) is more accurate on average but not by much for large n. In a rough statistical sense, you can split up these errors into three kinds. First there is the error due to vec(1) being not exactly representable in binary. That tends to be a constant and is the same for 1) and 2). Next there is the error due to delta not being exactly representable in binary which tends to increase in direct proportion to n. That too is statistically the same for 1) and 2). Finally there is the error due to repeated addition round offs in 1) and the single multiplication round off in 2). That tends to increase as the square root of n for 1) but remains constant for 2). Consequently 2) has less error on average for that reason but only for that reason.
For large n it is evident that the error due to the original error in delta will come to predominate over all other errors since it increases in proportion to n, while the repeated addition round offs increase only as the square root of n, statistically speaking, so the differences between 1) and 2) becomes proportionately much less than the errors in either one.
To put all this in proper perspective, if you are using double precision binary floating point numbers, these have significands (mantissas) of 53 bits in length, so their errors in representing numbers are roughly one part in 2^53 (equals one part in 10^16.) These are very small errors.
Be aware that matlab can build such vectors for you with its colon operator and indirectly with its linspace function.
Roger Stafford
