Debian Codegraph @

common/math/math.go

raw
1 // Copyright 2018 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package math
15
16 import (
17 "errors"
18 "reflect"
19 )
20
21 // DoArithmetic performs arithmetic operations (+,-,*,/) using reflection to
22 // determine the type of the two terms.
23 func DoArithmetic(a, b any, op rune) (any, error) {
24 av := reflect.ValueOf(a)
25 bv := reflect.ValueOf(b)
26 var ai, bi int64
27 var af, bf float64
28 var au, bu uint64
29 var isInt, isFloat, isUint bool
30 switch av.Kind() {
31 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
32 ai = av.Int()
33 switch bv.Kind() {
34 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
35 isInt = true
36 bi = bv.Int()
37 case reflect.Float32, reflect.Float64:
38 isFloat = true
39 af = float64(ai) // may overflow
40 bf = bv.Float()
41 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
42 bu = bv.Uint()
43 if ai >= 0 {
44 isUint = true
45 au = uint64(ai)
46 } else {
47 isInt = true
48 bi = int64(bu) // may overflow
49 }
50 default:
51 return nil, errors.New("can't apply the operator to the values")
52 }
53 case reflect.Float32, reflect.Float64:
54 isFloat = true
55 af = av.Float()
56 switch bv.Kind() {
57 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
58 bf = float64(bv.Int()) // may overflow
59 case reflect.Float32, reflect.Float64:
60 bf = bv.Float()
61 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
62 bf = float64(bv.Uint()) // may overflow
63 default:
64 return nil, errors.New("can't apply the operator to the values")
65 }
66 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
67 au = av.Uint()
68 switch bv.Kind() {
69 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
70 bi = bv.Int()
71 if bi >= 0 {
72 isUint = true
73 bu = uint64(bi)
74 } else {
75 isInt = true
76 ai = int64(au) // may overflow
77 }
78 case reflect.Float32, reflect.Float64:
79 isFloat = true
80 af = float64(au) // may overflow
81 bf = bv.Float()
82 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
83 isUint = true
84 bu = bv.Uint()
85 default:
86 return nil, errors.New("can't apply the operator to the values")
87 }
88 case reflect.String:
89 as := av.String()
90 if bv.Kind() == reflect.String && op == '+' {
91 bs := bv.String()
92 return as + bs, nil
93 }
94 return nil, errors.New("can't apply the operator to the values")
95 default:
96 return nil, errors.New("can't apply the operator to the values")
97 }
98
99 switch op {
100 case '+':
101 if isInt {
102 return ai + bi, nil
103 } else if isFloat {
104 return af + bf, nil
105 }
106 return au + bu, nil
107 case '-':
108 if isInt {
109 return ai - bi, nil
110 } else if isFloat {
111 return af - bf, nil
112 }
113 return au - bu, nil
114 case '*':
115 if isInt {
116 return ai * bi, nil
117 } else if isFloat {
118 return af * bf, nil
119 }
120 return au * bu, nil
121 case '/':
122 if isInt && bi != 0 {
123 return ai / bi, nil
124 } else if isFloat && bf != 0 {
125 return af / bf, nil
126 } else if isUint && bu != 0 {
127 return au / bu, nil
128 }
129 return nil, errors.New("can't divide the value by 0")
130 default:
131 return nil, errors.New("there is no such an operation")
132 }
133 }