Page 1 of 1 1
Topic Options
#185117 - 2008-02-06 09:54 PM "Rounding" bug incrementing a variable by 0.1
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
 Code:
break on

@kix ? ?

$dbl = 0.0

while $dbl < 15
    $dbl = 0.1 + $dbl
    $dbl ? 
loop

get $


This one shows a whacky rounding behaviour both in 4.53 and 4.60 (both kix32 and wkix32) when it reaches 5.9 and adds 0.1 (no matter if $dbl is 0 or 0.0 or (not) implicitly/explicitly declared before the loop)

Output on Windows XP Professional SP2 (German standard regional settings, therefore the comma in the output) :


4.60

0,1
0,2
0,3
0,4
0,5
0,6
0,7
0,8
0,9
1
1,1
1,2

... normal output, until:

5,7
5,8
5,9
5,99999999999999
6,09999999999999
6,19999999999999
6,29999999999999
6,39999999999999
6,49999999999999
6,59999999999999
6,69999999999999
6,79999999999999

... the bug persists until it suddenly goes back to normal at 10

9,79999999999998
9,89999999999998
9,99999999999998
10,1
10,2
10,3
10,4
10,5



_________________________



Top
#185126 - 2008-02-07 05:58 AM Re: "Rounding" bug incrementing a variable by 0.1 [Re: Jochen]
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Seen those things before, was typically due to an internal conversion of the number to floting point
_________________________
There are two types of vessels, submarines and targets.

Top
#185128 - 2008-02-07 09:27 AM Re: "Rounding" bug incrementing a variable by 0.1 [Re: Sealeopard]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Originally Posted By: Sealeopard
Seen those things before, was typically due to an internal conversion of the number to floting point

Agreed, it is not a bug.

You will find this annoyance in common in all (most?) programming languages, and it is caused (as SL says) by the way that floating point numbers are stored, which is an approximation of the real value.

There is a pretty good article here if you want more detail.

I learned this the hard way back in the days i wrote 'C' code, but it applies equally to languages which are further away from the machine code.

It's why when testing a floating point for zero you can never say
 Code:
If $fpNumber = 0


Instead you have to use something like
 Code:
If $fpNumber < 0.001 AND $fpNumber > -0.001


The values that you use for comparison will depend on the scale of your calculations.

Top
#185169 - 2008-02-08 03:05 PM Re: "Rounding" bug incrementing a variable by 0.1 [Re: Richard H.]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
I would still call it a bug, not a KiXtart bug, but anyway a bug.

Well, there might be a good explanation of things caused by approximation of things. I will read the article you linked Richard, and I will do this with great interest \:\)

But it still seems to be laziness to leave bugs, that may be unavoidable for complex math, as they are when it comes to simple additions. I mean, 5.9 + 0.1 is at any time 6.0 and not 5.99999999.

Another strange fact is that there is no problem doing this increment with 0.2 or any higher number. Oh well, not quite true: As soon as the last digit of the floating point number is a 1 there will be sooner or later a rounding bug that "heals" itself shortly thereafter

I see that there might be no easy fixing from Ruuds side (or any fixing at all), but still it smells fishy like the sloppy coding started in the sixties of last century that gave IT the great boost before the new millennium


Edited by Jochen (2008-02-08 03:08 PM)
_________________________



Top
Page 1 of 1 1


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

Who's Online
0 registered and 1169 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

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