Skip to content

Commit 9d2c9a7

Browse files
dmjioclaude
andcommitted
Fix signum: use gt/lt comparisons instead of negate
sign(-x) - sign(x) broke for two reasons: - Unsigned types (CBool, Word32): negate wraps (e.g. -1_u8 = 255), making sign(-x) = 0 for all positive inputs, so signum always returns 0 - Float zero: af_sign(-0.0) = 1 due to sign-bit check, giving signum(0.0) = 1 Replace with cast(gt x 0) - cast(lt x 0), which avoids negate entirely and correctly handles unsigned types and IEEE 754 negative zero. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 16d9472 commit 9d2c9a7

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

src/ArrayFire/Orphans.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ instance (Num a, AFType a) => Num (Array a) where
3939
x + y = A.add x y
4040
x * y = A.mul x y
4141
abs = A.abs
42-
signum x = A.sign (-x) - A.sign x
42+
signum x = A.cast (A.gt x 0) - A.cast (A.lt x 0)
4343
negate arr = A.scalar @a (fromInteger (-1)) `A.mul` arr
4444
x - y = A.sub x y
4545
fromInteger = A.scalar . fromIntegral

0 commit comments

Comments
 (0)