Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: How to avoid wrong answers in simple arithmetic expressions.
Replies: 13   Last Post: Jan 20, 2013 7:18 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
James Tursa

Posts: 2,094
Registered: 8/5/09
Re: How to avoid wrong answers in simple arithmetic expressions.
Posted: Jan 19, 2013 6:59 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"SK " <modysk@hotmail.com> wrote in message <kdf25b$sqv$1@newscl01ah.mathworks.com>...
> dpb <none@non.net> wrote in message <kdeaav$3e3$1@speranza.aioe.org>...
> > On 1/19/2013 7:53 AM, dpb wrote:
> > ...
> >

> > >
> > > Scaling...[and other stuff elided for time being]
> > >

> >
> > What happens if you were to multiply v by 100 and do the same w/ the
> > input variable internal to the function before you start your loop?
> >
> > --

>
> Thanks for the suggestion.
>
> Yes, I did try this, and it gives the correct result although I'm not sure I understand why. My first thought was that operations on numbers which differed only in the base 10 exponent should give results of equal accuracy.
>
> However, looking up the IEEE 754 floating point floating point representation. I see that the exponent is a power-of-2 exponent rather than a power-of-10. So, for example, 0.09 and 9 have very different mantissa's. Nevertheless both 0.09 and 9 do not have exact IEEE 754 representations.


False. 9 has an exact IEEE double representation. 0.09 does not ... the closest is:

0.0899999999999999966693309261245303787291049957275390625

> More generally, most integers do not have an exact representation in this format.

False. All of the integers in the range of your particular problem can be represented exactly in IEEE double. All integers from 0 up to 2^53 have exact IEEE double representations.

> So, does Matlab treat small integers in a special way?

No. They are treated just like all other IEEE double numbers.

> Otherwise why would multiplying the numbers by 100 in the original problem guarantee a correct solution?

Multiplying by 100 (and rounding the results) puts everything into integers that can be represented exactly by IEEE double.

To get some help visualizing all of this, I suggest you download the following utility from the FEX:

http://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str

James Tursa



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.