• Subject: Re: Floating point inaccuracies
• From: Tony Finch <dot@...>
• Date: Mon, 29 Nov 2010 10:36:55 +0000

```On Mon, 29 Nov 2010, Luiz Henrique de Figueiredo wrote:

> > True, but most people understand why you can't add 1/3 repeatedly and
> > wind up with 1.
>
> You do. The code below prints true.
> 	a=1/3
> 	b=a+a+a print(b==1)

But,

\$ lua -e 'q=1/3; a=0; i=0; while a == i*q do a = a+q; i=i+1; end; print(i)'
6

Actually if you play around with the value of q, 6 turns out to be quite a
popular result!

\$ lua -e 'for d=3,99,2 do q=1/d; a=0; i=0; while a == i*q do a = a+q; i=i+1; end; print(d,i); end'
3       6
5       6
7       6
9       6
11      6
13      10
15      6
17      6
19      14
21      14
23      10
25      6
27      18
29      6
31      10
33      10
35      7
37      22
39      6
41      6
43      10
45      13
47      13
49      6
51      10
53      6
55      6
57      30
59      6
61      6
63      34
65      34
67      11
69      7
71      7
73      38
75      21
77      6
79      11
81      42
83      6
85      14
87      14
89      26
91      13
93      6
95      7
97      6
99      6

Tony.
--
f.anthony.n.finch  <dot@dotat.at>  http://dotat.at/
HUMBER THAMES DOVER WIGHT PORTLAND: NORTH BACKING WEST OR NORTHWEST, 5 TO 7,
DECREASING 4 OR 5, OCCASIONALLY 6 LATER IN HUMBER AND THAMES. MODERATE OR
ROUGH. RAIN THEN FAIR. GOOD.

```