[PD] Some more float weirdness/fun

Charles Henry czhenry at gmail.com
Sat Mar 10 23:31:28 CET 2012


On 3/10/12, Lorenzo Sutton <lorenzofsutton at gmail.com> wrote:
> On 09/03/12 19:18, Quim Llimona wrote:
>> It's well-known that floats can't be treated the same way as integers...
>> but since PD is aimed at non-engineers and non-scientists I think it
>> would be a good idea to implement the "good" comparison algorithms (i.e.
>> checking against a threshold, etc) inside [==] and so, just to make
>> patching easier. Maybe it's already supposed to behave this way...
>
> No, no...I don't agree (and hope initiating the thread didn't suggest
> this idea). [==] should be what it says exact comparison. Actually in
> the patch I was making it would have been simple to put in a [>=]
> instead which basically *is* a threshold... I just thought it would be
> nice to point out some float fun given all the discussions :)
>
> Lorenzo.

How about an abstraction that uses <= against 2xepsilon*|input|?  That
would be a reliable automatic way to ignore single-bit rounding
errors.  I'm just using this page as a reference:
http://en.wikipedia.org/wiki/Machine_epsilon
There's two inputs, so I'd choose the largest of them and use that in
calculating the threshold.  The test patch below just takes a vertical
slider from -1e-7 to 1e-7 and adds the value to 0.999 to be compared
against.  This patch needs a little improvement--it seems to be
obeying a threshold which is a little closer to 8.9e-8 (which is
greater than the 32-bit machine epsilon.... so it still does what is
wanted with a little extra room)

nearly_equal.pd
--------------------------------------------------------
#N canvas 86 491 682 612 10;
#X obj 91 39 inlet;
#X obj 137 39 inlet;
#X obj 182 39 \$1;
#X obj 216 39 loadbang;
#X obj 94 140 abs;
#X obj 98 345 <=;
#X obj 139 110 abs;
#X obj 185 110 abs;
#X obj 93 74 t f f;
#X obj 138 74 t f f;
#X obj 140 212 >=;
#X obj 189 209 <;
#X obj 139 145 f;
#X obj 186 144 t b f;
#X obj 202 244 *;
#X obj 140 172 t f f;
#X obj 140 244 *;
#X obj 94 108 -;
#X obj 140 276 +;
#X obj 140 308 * 1.192e-07;
#X obj 97 391 outlet;
#X connect 0 0 8 0;
#X connect 1 0 9 0;
#X connect 2 0 17 1;
#X connect 3 0 2 0;
#X connect 4 0 5 0;
#X connect 5 0 20 0;
#X connect 6 0 12 0;
#X connect 7 0 13 0;
#X connect 8 0 17 0;
#X connect 8 1 6 0;
#X connect 9 0 17 1;
#X connect 9 1 7 0;
#X connect 10 0 16 0;
#X connect 11 0 14 0;
#X connect 12 0 15 0;
#X connect 13 0 12 0;
#X connect 13 1 10 1;
#X connect 13 1 11 1;
#X connect 13 1 14 1;
#X connect 14 0 18 1;
#X connect 15 0 10 0;
#X connect 15 1 11 0;
#X connect 15 1 16 1;
#X connect 16 0 18 0;
#X connect 17 0 4 0;
#X connect 18 0 19 0;
#X connect 19 0 5 1;


nearly_equal_test.pd
--------------------------------------------------------
#N canvas 0 25 264 184 10;
#X obj 118 79 nearly_equal 0.999;
#X obj 38 27 vsl 15 128 -1e-07 1e-07 0 0 empty empty empty 0 -9 0 10
-262144 -1 -1 0 1;
#X floatatom 115 135 5 0 0 0 - - -;
#X obj 116 18 + 0.999;
#X connect 0 0 2 0;
#X connect 1 0 3 0;
#X connect 3 0 0 0;



More information about the Pd-list mailing list