Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
building regular floating point vectors

Subject: building regular floating point vectors

From: Blue Wizard

Date: 16 Jun, 2010 16:29:04

Message: 1 of 3

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(n-1) + 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.

Subject: building regular floating point vectors

From: Steven Lord

Date: 16 Jun, 2010 16:46:50

Message: 2 of 3


"Blue Wizard" <hdh6@cornell.edu> wrote in message
news:c4fc258a-e911-466f-b736-f431f4704112@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(n-1) + delta
> 2) vec(n) = vec(1) + (n - 1) * delta

Three other options:

3) vec = lowerLimit:delta:upperLimit
4) vec = linspace(lowerLimit, upperLimit, numberOfPoints)
5) Scale vec(1) and delta so that they are exactly representable as binary
numbers, construct your vector, then scale the vector back so the first
element and spacing match your original values.

for appropriate values of lowerLimit, upperLimit, and numberOfPoints.

> 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.

What happens if for one of the methods, vec(k) is more accurate than
vec(k+1) for some intermediate value k (by the metric you described) but the
reverse is true for the other methods? Would the magnitude of the error in
each of those elements lead you to choose one over the other?

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: building regular floating point vectors

From: Roger Stafford

Date: 16 Jun, 2010 17:08:05

Message: 3 of 3

Blue Wizard <hdh6@cornell.edu> wrote in message <c4fc258a-e911-466f-b736-f431f4704112@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(n-1) + 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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us