diff --git a/vlib/math/math_test.v b/vlib/math/math_test.v index 816bb9fe01..652cd4c7f0 100644 --- a/vlib/math/math_test.v +++ b/vlib/math/math_test.v @@ -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() { for i := 0; i < math.vf_.len; i++ { f := floor(math.vf_[i]) diff --git a/vlib/math/mathutil.v b/vlib/math/mathutil.v index f3f5c4101d..86c599c251 100644 --- a/vlib/math/mathutil.v +++ b/vlib/math/mathutil.v @@ -18,5 +18,5 @@ pub fn max(a T, b T) T { // abs returns the absolute value of `a` [inline] pub fn abs(a T) T { - return if a > 0 { a } else { -a } + return if a < 0 { -a } else { a } }