in reply to RE (tilly) 3: The Big Test
in thread The Big Test

One I used to see regularly when in my second Basic class (*sigh*) was (in perl idiom)  if ($num/2 == int($num/2)) {print "$num even!\n":}

That still gives me the willies, though it is techically correct(ish) and is more generally applicable than terminal digit inspection =) Most people these days never got taught what the mod function really does. Basic math logic skills are passed off till college.

One thing I remember from Basic was that true was -1 and false was 0, and that all logical ops returned one of those two values. We used to do this (sorta) in graphics programs to get smoothly, randomly, changing colors and positions:

my @color= (127,127,127); #256x256x256 color palette my @location= (50,50); #200x200 screen for(;;) { $color[0] += (int(rand*3)==1) - (int(rand*3)==1); $color[1] += (int(rand*2)==1) - (int(rand*4)==1); $color[2] += (int(rand*4)==1) - (int(rand*2)==1); $location[0] += (int(rand*5)) - 2; $location[1] += (int(rand*5)) - 2; @color = map {$_ % 256} @color; @location = map {$_ % 200} @location; DrawPixel(@location,@color); }

We did megacomplex things with math logic because it was blazingly fast vs. if (){}; =) Seems like a forgotten era now that I do web stuff.

--
$you = new YOU;
honk() if $you->love(perl)

Replies are listed 'Best First'.
RE: RE: RE (tilly) 3: The Big Test
by myocom (Deacon) on Oct 18, 2000 at 05:20 UTC

    You say this is "correct(ish)", but I can't for the life of me think of a case where it's not correct...so why the ish? :-)

      What happens if someone tosses a
      use integer;
      in at the beginning of the file? Same effect if you take that habit to many other languages.

      Just because it works doesn't mean that it isn't a really bad idea.

      Because some math illiterate always tries it with 2.5 to see if a number is divisible evenly with it... ala: if ($num/2.5 == int($num/2.5)) {print "$num even!\n":} Evil!

      If they didn't understand it when they did that, how do you explain to them why they are wrong? I usually resorted to "Mr. G? Kelly has a question about her code..." =)

      --
      $you = new YOU;
      honk() if $you->love(perl)