Strange math behaviour... could someone explain this ?

Richard Gaskin ambassador at fourthworld.com
Fri Oct 7 21:41:54 EDT 2005


Alex Tweedly wrote:
> Lynch, Jonathan wrote:
> 
>> But still...
>>
>> Answer trunc((36-34.2)*100) should return 180, not 179.
> 
> No it shouldn't.
> 
>> I mean, the
>> underlying code should work to return an accurate value.
> 
> It does.
> 
>> Perhaps it is just a matter of opinion, but to me, if the software 
>> returns a wrong value in a calculation, it is a bug.
>>
> It's not a matter of opinion, and it's not a wrong answer - it's a 
> correct answer. The binary double precision representation of 34.2 is 
> inexact, and hence the binary double precision representation of 36-34.2 
> is similarly inexact - so instead of exactly 180, it's about 1x2**-40 
> less than that.
> And then when you use trunc() it does what you ask.
> 
> It's NOT a Rev bug - it's an artifact of double precision binary 
> arithmetic (or, if you like, an artifact of the IEEE format used by 
> Intel (and everyone else)).

One man's "artifact" is another man's "bug". :)

The original poster never specified that it was a Rev bug, and you've 
shown that the anomaly lies outside of Rev.

Computers are the servants of humans, not the other way around.

In human logic, this:

   trunc((36-34.2)*100)

...is evaluated in three steps:

1. 36-34.2 = 1.8
2. 1.8 * 100 = 180
3. truncate 180 = 180

Math is a human tool devised for the benefit of humans.  Parentheses 
have a specific meaning which breaks down operations into an explicit 
order of operations.

If a computer system is designed in such a way that it no longer serves 
its masters faithfully by honoring things like the explicit order of 
exectution, it's commonly called a "bug", and it's usually up to another 
human to come in an fix it.

Whether the fault lies with Rev or the microprocessor, the anwer of 179 
is logically incorrect.  It may be understandable, but understanding why 
it's giving an incorrect answer doesn't change the answer. :)

--
  Richard Gaskin
  Managing Editor, revJournal
  _______________________________________________________
  Rev tips, tutorials and more: http://www.revJournal.com



More information about the use-livecode mailing list