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

master
yuyi 2022-04-27 18:23:37 +08:00 committed by GitHub
parent 752e105f25
commit 82ac39eca6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 }
} }