IRC channel logs
2025-08-06.log
back to list of logs
<lechner>Hi, is multiplying by -1 the best way to change the sign of an integer in Guile? <ieure>lechner, Generally the best way in any language. <identity>(- 3) is -3 and (- -3) is 3; / with one argument also works similarly, (/ 3) is 1/3 and (/ 1/3) is 3 <lechner>identity / thanks! very interesting and usefule <old>lechner: ,optimize (define (negate-number x) (- x)) <old>=> (define (negate-number x) (- 0 x)) <old>the constant folding optimizer is very clever sometime :-/ <old>,optimize (define (foo x) (+ x 1 1)) => (define (foo x) (+ (+ x 1) 1)) <old>,optimize (define (foo x) (+ 1 1 x)) => (define (foo x) (+ 2 x)) <old>s/is very/is not very <old>lechner: FYI, I think that there are further optimizations done in C to check if one operand is 0 when doing +/- <old>so it's probably faster than multiplying by -1 <old>maybe that optimization is also done for multiplication and 1/-1, not sure <dsmith>I saw a beautiful (to me) branchless signum in C the other day: (x > 0) - (x < 0) <dsmith>Works because '>' and '<' eval to 0 or 1