Skip to content

Commit

Permalink
[bug] Fix misbehaviour and assertion error on ti.math.sign
Browse files Browse the repository at this point in the history
  • Loading branch information
listerily committed May 26, 2023
1 parent d56f7b1 commit 5c77628
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion python/taichi/math/mathimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def sign(x):
>>> ti.math.sign(x)
[-1.000000, 0.000000, 1.000000]
"""
return ops.cast((x >= 0.0) - (x <= 0.0), float)
return ops.cast((x >= 0.0), float) - ops.cast((x <= 0.0), float)


@func
Expand Down
11 changes: 11 additions & 0 deletions tests/python/test_unary_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,14 @@ def test_u32(x: ti.uint32) -> ti.int32:
assert test_u32(100) == 3
assert test_u32(1000) == 6
assert test_u32(10000) == 5


@test_utils.test()
def test_sign():
@ti.kernel
def foo(val: ti.f32) -> ti.f32:
return ti.math.sign(val)

assert foo(0.5) == 1.0
assert foo(-0.5) == -1.0
assert foo(0.0) == 0.0

0 comments on commit 5c77628

Please sign in to comment.