-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.go
60 lines (54 loc) · 997 Bytes
/
calculator.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
package calculator
import (
"errors"
"math"
)
func AddMany(inputs ...float64) float64 {
if len(inputs) == 0 {
return 0
}
var result float64 = 0
for _, value := range inputs {
result += value
}
return result
}
func SubstractMany(inputs ...float64) float64 {
if len(inputs) == 0 {
return 0
}
result := inputs[0]
for _, value := range inputs[1:] {
result -= value
}
return result
}
func MultiplyMany(inputs ...float64) float64 {
if len(inputs) == 0 {
return 0
}
result := inputs[0]
for _, value := range inputs[1:] {
result *= value
}
return result
}
func DivideMany(inputs ...float64) (float64, error) {
if len(inputs) == 0 {
return 0, nil
}
result := inputs[0]
for _, value := range inputs[1:] {
if value == 0 {
return 0, errors.New("division by zero not allowed")
}
result /= value
}
return result, nil
}
func Sqrt(a float64) (float64, error) {
if a < 0 {
return 0, errors.New("negative number not allowed")
}
return math.Sqrt(a), nil
}