GnuTux wrote:
I don't think coders would think to check to see if that zero was actually zero. I know I wouldn't. If it shows zero on the dialog, one would assume it's zero.
Hate to be bearer of bad news but.. methinks you are expecting a bit too much of the cosseted environment provided by Script-Fu. This is floating-point arithmetic and it is, therefore, imprecise by nature, as this simple exercise on the Script-Fu Console demonstrates:
> (define x 0)
x
> (define n 1000)
n
> (while (> n 0) (set! x (+ x 0.1)) (set! n (- n 1)))
()
> x
100.0
> (= x 100.0)
#f
> (set! x (- x 100.0))
-1.406874617e-12
The while loop just adds 0.1 to a variable ("x") 1000 times over, starting from zero. At the end of the loop, I have displayed the value of x, and sure enough it shows as "100.0" which is what you would expect. However, if we then use (= ) to test whether the variable is actually equal to 100.0, we get the answer false! And if we subtract 100.0 from the variable it now shows a very small (and previously invisible) residual value, a remnant from all that floating-point addition.
If you try the same exercise but adding 0.125 (ie. 1/8) each time, you get no such residual:
> (define x 0)
x
> (define n 1000)
n
> (while (> n 0) (set! x (+ x 0.125)) (set! n (- n 1)))
()
> x
125.0
> (= x 125.0)
#t
> (set! x (- x 125.0))
0.0
The reason for this discrepancy is that 0.1 is not a precise
binary fraction ..it is a precise
decimal fraction.
The precise binary fractions are 1/2, 1/4, 1/8, 1/16, etc. Those numbers (or any multiple or combination thereof) can be represented
precisely in floating-point format and can be added, subtracted and/or multiplied indefinitely without accumulating errors.
Decimal fractions cannot be represented precisely and so
every arithmetic operation accumulates a small error (depending upon the floating-point format used and the value in question).
In this case, the floating-point format is that used by the underlying C99 (in which TinyScheme, Script-Fu, libgimp and GTK are all coded) which is IEEE-754:binary64 (see
http://en.wikipedia.org/wiki/Double-pre ... int_format) and so errors are of the order e-15 or smaller. (In the first exercise above, after 1000 additions, we have accumulated a total error of order e-12. But the Script-Fu Console does not display that residual error when the base value is anything other than zero.)
------------------
I have not yet got to the bottom of quite what is going on with the GTK spinbuttons, but this inherent imprecision of
decimal fractions in floating-point format is certainly one factor involved: each time you click on an up/down arrow on a GTK spinbutton, GTK adds/subtracts the specified
step-value configured for the spinbutton to/from the current value retained by the spinbutton; if the step-value is not a precise binary fraction (such as 0.125 decimal), then errors will, necessarily, accumulate. (This does not, of course, happen when you enter a value manually or push the slider to either end-stop - hence the difference.) So, in this respect GTK is not at fault, nor are the Script-Fu plugin or libgimp ..it is just in the nature of floating-point arithmetic and I'm afraid it is the responsibility of the script-coder to make his/her script robust against such issues.
Where GTK may still be at fault is in the end-stop behaviour: if you use the up/down buttons to step all the way to either the lower or upper end of the allowed range, GTK
should return the
precise value of that limit ..which is precisely zero in the case of your test-zero.scm ..but it appears not to do so. That I am still investigating!