You're replying to a comment by Eric TF Bat.

Eric TF Bat Permalink
November 30, 2009, 22:32

I couldn't figure out the inchworm-on-a-stick either (I learned Forth in my free time during university and got a High Distinction in Discrete Mathematics 1 because it enabled me to grok how stuff like two's complement arithmetic works). But the trick is that it's not the same as $x-- or --$x or $x-1: it's much weirder than that. Take a look at the output from this:

foreach my $x ((1,2,3,-1,-2,-3,0,1.5,2.5,3.1,4.9)) { 
    printf qq/%+2.2f %+25.2f\n/, $x, ~-$x;
}

+1.00                     +0.00
+2.00                     +1.00
+3.00                     +2.00
-1.00  +18446744073709551616.00
-2.00  +18446744073709551616.00
-3.00  +18446744073709551616.00
+0.00  +18446744073709551616.00
+1.50                     +0.00
+2.50                     +1.00
+3.10                     +2.00
+4.90                     +3.00

Very odd indeed, especially when you replace the second %...f with %d, and then you get this:

+1.00 +0
+2.00 +1
+3.00 +2
-1.00 -2
-2.00 -3
-3.00 -4
+0.00 -1
+1.50 +0
+2.50 +1
+3.10 +2
+4.90 +3

What I don't know is what good it would be. I mean, if $x is non-negative, then it's the same as int($x)-1, but the weird result for negative $x confuses me...

Reply To This Comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the first letter of your name: (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.