I Need Help

new topic     » goto parent     » topic index » view thread      » older message » newer message

Larry D Poos writes several loops such as:
> for l=3D0  to 10000  by .1  do

There's a simple explanation for why you aren't getting the
results that you expect. The root of the problem is
that 0.1 cannot be represented in binary floating-point
form *exactly*. This means that if you add up ten 0.1's
you will *not* get 1.

Try the following statement in Euphoria:
? .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 - 1

You will not get 0. You will get -1.11e-016.
in other words a very very small number.

In QBasic you will get 1.49e-08, which is even worse.

Any language that uses IEEE floating-point is going
to have this problem. Intel hardware is based on IEEE,
so anyone who uses hardware floating-point on a PC (or most
other machines) is going to have this problem.

If you use Cobol, you will not see this problem
because Cobol uses decimal floating-point and so .1
*can* be represented exactly. So can .01. This makes
money calculations work out nicely in Cobol.

Conclusion: Don't count on floating-point calculations
to work out perfectly. Allow for a little bit of error
in your calculations. Be careful when you compare
floating-point results for equality.

Regards,
  Rob Craig
  Rapid Deployment Software

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu