MOD function does not work! So be warned!

Ian Summerfield iansummerfield at btconnect.com
Sun Mar 31 16:13:01 EST 2002


It seems I've opened a can of worms with this one.  Strangely enough I've
been working with Hypercard since it was in beta, in fact Wildcard 0.7 was
where I started.   Anyway, I just thought I'd try the same test in
Hypercard;  if Hypercard got it right then Revolution has no excuse,
fortunately Hypercard got it wrong too!   I can't believe that with all the
projects I've been involved in over the years that I've not been caught out
before!   Well not for many years anyway.   Here's the Hypercard code
tested:

on mouseUp
  put empty into field "res"
  put 235 into startPoint
  put 255 into endPoint
  put 0.1 into increment
  put startPoint into loop
  repeat while loop<=endPoint
    put loop && loop mod 5 & return after field "res"
    add increment to loop
  end repeat
end mouseup

I thought it would work fine,  but Hypercard suffers the same precision
glitch,  so Metacard/Revolution is forgiven!

I'd like to see a change though, something that allows me to define a
precision to n decimal places.  Instead you have to program your own such
logic.

I first came across this sort of problem in AppleSoft Basic on my Apple II,
so for those working with currency, do what I used to do,  work internally
with each input number multiplied by 100 - that way you're dealing with
integers only!   The code above would work if rewritten as:

on mouseUp
  global gDecimalPlaces
  
  put 2 into gDecimalPlaces -- do once on openstack maybe
  
  put (10 ^ gDecimalPlaces) into adjustment
  
  put empty into field "res"
  
  -- make each number an integer, if you know your input
  -- might be to more decimal places than desired then you might want to
  -- round the result to 0 decimal places.
  
  put 235 * adjustment into startPoint
  put 255 * adjustment into endPoint
  put .1 * adjustment into increment
  
  put startPoint into loop
  
  repeat while loop<=endPoint
    -- on outputing any results always divide by the adjustment
    put (loop / adjustment) && ((loop mod 500)/adjustment) & return after
field "res"
    add increment to loop
  end repeat
end mouseup


The problem is completely avoided.   I'd just forgotten this quirk of
floating point arithmetic.

Someone on the list implied they were teaching programming?  When I started
out we first started learning binary before even touching a system,  then
base 8, 16, etc.   Then we could understand how a computer stored values,
we also understood the precision problems with representing floating point
numbers,  this was all done before being allowed to touch a system!   Mind
you, maybe times have changed now seeing as there's so much more to learn.
I started learning at age 14 on a DEC PDP 10.   So if I knew this then why
did I have a problem now?   Well I simply forgot,  Revolution distances you
from the low level world,  well that's what I thought it was designed to do,
so you forget these things.  Clearly it's closer to a "real" development
system than I thought.  Anyway, if you're teaching I think it's a good
starting point,  so many "techies" I've employed over the years don't get
taught the basics any more,  it makes me wonder how they can appreciate just
how much large a 1mb, 1gb, etc. really is!

Thank you all for jogging my ageing memory!

-i-






More information about the use-livecode mailing list