Page 1 of 1 1
Topic Options
#101157 - 2003-05-20 08:46 PM When is 0.1 - 0.1 NOT 0?
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
I was playing around with a Mandrelbot script when I noticed that For...Next Loops do not accept Double Integers, i.e.:

code:
Break On
For $x = 1.1 to 2.1 step 0.1
$x ?
Next

I was about to make post a topic in Suggestions about it, but was wise enough to run it across Jens first. The short of it is...
quote:
Numbers can only be represented digitally with an accuracy of EPS. Thus, a number x+EPS/2 canot be represented and will be either end up being x or x+EPS

His suggestion was to use a Do...Until or While...Loop loop. So, I tried this...
code:
$x = 1.2

While $x > -1.2
$x ?
$x = $x - 0.1
Loop

...which has some interesting results when 0 is expected.

Thoughts????

[ 20. May 2003, 20:47: Message edited by: Chris S. ]

Top
#101158 - 2003-05-20 08:50 PM Re: When is 0.1 - 0.1 NOT 0?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
what was it?
aps, asp, eps?
isn't it something about telephacy?
_________________________
!

download KiXnet

Top
#101159 - 2003-05-20 08:52 PM Re: When is 0.1 - 0.1 NOT 0?
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
Here is an in-depth explanation, but my head started spinning about 2 sentences into it...

http://www.mathworks.com/company/newsletter/pdf/Fall96Cleve.pdf

Top
#101160 - 2003-05-20 08:53 PM Re: When is 0.1 - 0.1 NOT 0?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
This is explained in http://www.mathworks.com/company/newsletter/pdf/Fall96Cleve.pdf and is typical for ALL digital systems. It is not something particular to KiXtart. IIRC, we've already had this discussion once on this BBS.
_________________________
There are two types of vessels, submarines and targets.

Top
#101161 - 2003-05-20 09:12 PM Re: When is 0.1 - 0.1 NOT 0?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
chris, opened your link and directly scrolled further and decided not to read a single word of it.

anyway, I even didn't think someone would like to use some odd numbers as loop orderers...
well, ESP here or there, I still think that using floating numbers there...
uhm.
_________________________
!

download KiXnet

Top
#101162 - 2003-05-20 11:30 PM Re: When is 0.1 - 0.1 NOT 0?
MightyR1 Offline
MM club member
*****

Registered: 1999-09-09
Posts: 1264
Loc: The Netherlands
Agreeing with the Lonk here...

Why one use floating???
_________________________
Greetz,
Patrick Rutten

- We'll either find a way or make one...
- Knowledge is power; knowing how to find it is more powerful...
- Problems don't exist; they are challenges...

Top
#101163 - 2003-05-21 01:44 PM Re: When is 0.1 - 0.1 NOT 0?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
The K&R 'C' manual (aka The Bible) explicitly warns about this, and advizes the use of fuzzy checks, i.e.:
code:
BAD  : If $x = 0

GOOD : If ($x>=-0.01 AND $x <=0.01)

Or whatever your margin of error is.

As the guys say, is better to stick to integer maths and convert. Your first example would become:
code:
Break ON
For $x = 11 To 21
Cdbl($x)/10 ?
Next


Top
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1046 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.059 seconds in which 0.025 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org