math: fix error for math.abs(0.0)/math.abs(0) (related #14165) (#14191)

yuyi 2022-04-27 18:23:37 +08:00 committed by Jef Roosens
parent 8b085a32a5
commit 2d9fa62941
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 11 additions and 1 deletions

View File

@ -408,6 +408,16 @@ fn test_abs() {
} }
} }
fn test_abs_zero() {
ret1 := abs(0)
println(ret1)
assert '$ret1' == '0'
ret2 := abs(0.0)
println(ret2)
assert '$ret2' == '0'
}
fn test_floor() { fn test_floor() {
for i := 0; i < math.vf_.len; i++ { for i := 0; i < math.vf_.len; i++ {
f := floor(math.vf_[i]) f := floor(math.vf_[i])

View File

@ -18,5 +18,5 @@ pub fn max<T>(a T, b T) T {
// abs returns the absolute value of `a` // abs returns the absolute value of `a`
[inline] [inline]
pub fn abs<T>(a T) T { pub fn abs<T>(a T) T {
return if a > 0 { a } else { -a } return if a < 0 { -a } else { a }
} }