-
Notifications
You must be signed in to change notification settings - Fork 374
/
number.go
71 lines (58 loc) · 2.01 KB
/
number.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aggregations
import (
"math"
"math/big"
)
// SumNumbers accumulate numbers and returns the result of summation.
// The result has the same type as the input, except when the result
// cannot be presented accurately. Then int32 is converted to int64,
// and int64 is converted to float64. It ignores non-number values.
// For empty `vs`, it returns int32(0).
// This should only be used for aggregation, aggregation does not return
// error on overflow.
func SumNumbers(vs ...any) any {
// use big.Int to accumulate values larger than math.MaxInt64.
intSum := big.NewInt(0)
// handle accumulation of doubles close to max precision.
// TODO https://github.com/FerretDB/FerretDB/issues/2300
var floatSum float64
var hasFloat64, hasInt64 bool
for _, v := range vs {
switch v := v.(type) {
case float64:
hasFloat64 = true
floatSum = floatSum + v
case int32:
intSum.Add(intSum, big.NewInt(int64(v)))
case int64:
hasInt64 = true
intSum.Add(intSum, big.NewInt(v))
default:
// ignore non-number
}
}
if hasFloat64 || !intSum.IsInt64() {
// ignore accuracy because there is no rounding from int64.
intAsFloat, _ := new(big.Float).SetInt(intSum).Float64()
return intAsFloat + floatSum
}
integer := intSum.Int64()
if !hasInt64 && integer <= math.MaxInt32 && integer >= math.MinInt32 {
// convert to int32 if input has no int64 and can be represented in int32.
return int32(integer)
}
return integer
}