From 2d9fa62941508acc836d3c6e38978b9020ec12e2 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 27 Apr 2022 18:23:37 +0800 Subject: [PATCH] math: fix error for math.abs(0.0)/math.abs(0) (related #14165) (#14191) --- vlib/math/math_test.v | 10 ++++++++++ vlib/math/mathutil.v | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) 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 } }