Re: Possreps and numeric types

From: Matthias Klaey <mpky_at_hotmail.com>
Date: Wed, 28 Mar 2007 01:10:10 +0200
Message-ID: <6l7j03di6tdmf1k68s7oa2d6fto0lv9ko3_at_4ax.com>


"V.J. Kumar" <vjkmail_at_gmail.com> wrote:

>"David Cressey" <cressey73_at_verizon.net> wrote in
>news:KD2Oh.1604$Rp2.1491_at_trndny04:
>
>>
>> "V.J. Kumar" <vjkmail_at_gmail.com> wrote in message
>> news:Xns98FFEB79C9D6Bvdghher_at_217.22.228.20...
>>> Matthias Klaey <mpky_at_hotmail.com> wrote in
>>> news:gqsg031cmfnr5p5fbiokksmaf8psios4hh_at_4ax.com:
>>>
>>> > I used to teach that if you calculate "2 + 2" on a computer, the
>>> > result would be somewhere between 3 and 5,
>>>
>>> You are kidding, right ? If not, you might want to fix your
>>> computer for the sake of your poor students at least !
>>
>> I think you missed the irony in the original teaching. If you are
>> working with integers,
>> there is only one answer that is somewhere between 3 and 5.
>
>I may be missing the irony, but how can adding integer 2 and 2 can
>produce anything but 4 ? "Somewhere" implies vagueness or ambiguity, at
>least in the English I am accustomed to. Is it some sort of new post-
>modern kind of math, or English, I am unfamiliar with ? It seems that
>the Swiss secondary education system is in even worse shape than the
>American !
>
>> Everything works. If you are working with floating point numbers, and
>> approximations, you can get some nasty surprises.
>
>Not when adding floating 2.0 and 2.0, it is still 4.0 surprisingly
>enough for some !

[...]

Here is a very simple "real world" example such as an interest calculation on a capital (capital 21200$, interest 2.5%, calculate capital + interest):

  21200 * 1.025 = 21730

As you can verify easily with hand calculation, this is exact.

Now evaluate this same calculation with any computer language that implements the IEEE 854 standard (such as C, C++, Java, the .NET languages, Visual Basic, Delphi and numerous others, also MS SQL Server, Oracle; basically *all* languages that rely on the Intel Floating Point architecture).

The result you will get is that

  21200 * 1.025 *is not equal to* 21730

because 21200 * 1.025 is repersented as 40D5387FFFFFFFFF and 21730 is represented as 40D5388000000000

The "Swiss" part is that I am using dramatic oversimplification to teach you about a computational phenomenon that is going to bite you very quickly into your a*** if you don't understand the mathematical theory of floating point calculation.

This same Swiss humour goes even farther:

  "Never trust a computer calculation before you have verified    the computer result by hand"

Greetings
Matthias Kläy

-- 
www.kcc.ch
Received on Wed Mar 28 2007 - 01:10:10 CEST

Original text of this message