From 19f3fa7f4bd58b03b6080183e61d8dbd54df7c7d Mon Sep 17 00:00:00 2001 From: itchyny Date: Fri, 1 Mar 2024 23:00:20 +0900 Subject: [PATCH] fix nearbyint and rint to round ties to even --- cli/test.yaml | 57 +++++++++++++++++++++++++++------------------------ func.go | 4 ++-- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/cli/test.yaml b/cli/test.yaml index 3e96cdcd..560d64ca 100644 --- a/cli/test.yaml +++ b/cli/test.yaml @@ -3953,7 +3953,7 @@ - name: trigonometric functions args: - -c - - 'map(sin),map(cos),map(tan) | map(. * 1000000000 | floor / 1000000000)' + - 'map(sin), map(cos), map(tan) | map(. * 1000000000 | floor / 1000000000)' input: '[0, 1, 2]' expected: | [0,0.841470984,0.909297426] @@ -3963,7 +3963,7 @@ - name: inverse trigonometric functions args: - -c - - 'map(asin),map(acos),map(atan) | map(. * 1000000000 | floor / 1000000000)' + - 'map(asin), map(acos), map(atan) | map(. * 1000000000 | floor / 1000000000)' input: '[0, 1, 2]' expected: | [0,1.570796326,null] @@ -3973,7 +3973,7 @@ - name: hyperbolic functions args: - -c - - 'map(sinh),map(cosh),map(tanh) | map(. * 1000000000 | floor / 1000000000)' + - 'map(sinh), map(cosh), map(tanh) | map(. * 1000000000 | floor / 1000000000)' input: '[0, 1, 2]' expected: | [0,1.175201193,3.626860407] @@ -3983,39 +3983,42 @@ - name: inverse hyperbolic functions args: - -c - - 'map(asinh),map(acosh),map(atanh) | map(. * 1000000000 | floor / 1000000000)' + - 'map(asinh), map(acosh), map(atanh) | map(. * 1000000000 | floor / 1000000000)' input: '[0, 1, 2]' expected: | [0,0.881373587,1.443635475] [null,0,1.316957896] [0,1.7976931348623157e+308,null] -- name: floor, round, nearbyint, rint, ceil, trunc, fabs, sqrt and cbrt functions +- name: floor, ceil, trunc, fabs functions args: - -c - - 'range(.) | sin | (fabs,sqrt,cbrt,sin) * 100000 | [(floor,round,nearbyint,rint,ceil,trunc) / 100000]' - input: '5' + - 'map(floor), map(ceil), map(trunc), map(fabs)' + input: '[-3.7, -3.3, -2.5, -1.7, -1.0, -0.7, -0.3, 0.0, 0.3, 0.5, 0.7, 1.0, 1.7, 2.5, 3.3, 3.7]' expected: | - [0,0,0,0,0,0] - [0,0,0,0,0,0] - [0,0,0,0,0,0] - [0,0,0,0,0,0] - [0.84147,0.84147,0.84147,0.84147,0.84148,0.84147] - [0.91731,0.91732,0.91732,0.91732,0.91732,0.91731] - [0.94408,0.94409,0.94409,0.94409,0.94409,0.94408] - [0.74562,0.74562,0.74562,0.74562,0.74563,0.74562] - [0.90929,0.9093,0.9093,0.9093,0.9093,0.90929] - [0.95357,0.95357,0.95357,0.95357,0.95358,0.95357] - [0.9688,0.9688,0.9688,0.9688,0.96881,0.9688] - [0.78907,0.78907,0.78907,0.78907,0.78908,0.78907] - [0.14112,0.14112,0.14112,0.14112,0.14113,0.14112] - [0.37565,0.37566,0.37566,0.37566,0.37566,0.37565] - [0.52063,0.52063,0.52063,0.52063,0.52064,0.52063] - [0.14065,0.14065,0.14065,0.14065,0.14066,0.14065] - [0.7568,0.7568,0.7568,0.7568,0.75681,0.7568] - [null,null,null,null,null,null] - [-0.9113,-0.9113,-0.9113,-0.9113,-0.91129,-0.91129] - [-0.68661,-0.6866,-0.6866,-0.6866,-0.6866,-0.6866] + [-4,-4,-3,-2,-1,-1,-1,0,0,0,0,1,1,2,3,3] + [-3,-3,-2,-1,-1,-0,-0,0,1,1,1,1,2,3,4,4] + [-3,-3,-2,-1,-1,-0,-0,0,0,0,0,1,1,2,3,3] + [3.7,3.3,2.5,1.7,1,0.7,0.3,0,0.3,0.5,0.7,1,1.7,2.5,3.3,3.7] + +- name: round, nearbyint, rint functions + args: + - -c + - 'map(round), map(nearbyint), map(rint)' + input: '[-2.5, -1.7, -1.5, -1.2, -0.7, -0.5, -0.3, 0.0, 0.3, 0.5, 0.7, 1.2, 1.5, 1.7, 2.5]' + expected: | + [-3,-2,-2,-1,-1,-1,-0,0,0,1,1,1,2,2,3] + [-2,-2,-2,-1,-1,-0,-0,0,0,0,1,1,2,2,2] + [-2,-2,-2,-1,-1,-0,-0,0,0,0,1,1,2,2,2] + +- name: sqrt and cbrt functions + args: + - -c + - 'map(sqrt), map(cbrt) | map(. * 1000000000 | floor / 1000000000)' + input: '[-8, -3.375, 0, 0.125, 1, 4, 8, 40.96, 64]' + expected: | + [null,null,0,0.35355339,1,2,2.828427124,6.4,8] + [-2,-1.5,0,0.5,1,1.587401051,2,3.447095504,4] - name: significand function args: diff --git a/func.go b/func.go index a8635b92..c6936f16 100644 --- a/func.go +++ b/func.go @@ -128,8 +128,8 @@ func init() { "atanh": mathFunc("atanh", math.Atanh), "floor": mathFunc("floor", math.Floor), "round": mathFunc("round", math.Round), - "nearbyint": mathFunc("nearbyint", math.Round), - "rint": mathFunc("rint", math.Round), + "nearbyint": mathFunc("nearbyint", math.RoundToEven), + "rint": mathFunc("rint", math.RoundToEven), "ceil": mathFunc("ceil", math.Ceil), "trunc": mathFunc("trunc", math.Trunc), "significand": mathFunc("significand", funcSignificand),