-
Notifications
You must be signed in to change notification settings - Fork 0
/
DivideNumbers.go
56 lines (51 loc) · 1.51 KB
/
DivideNumbers.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
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package dfl
import (
"fmt"
"reflect"
"github.com/pkg/errors"
)
// DivideNumbers divides the first number by the second number and returns the results.
// The parameters can be an int, int64, or float64.
// The parameters will be cast as applicable.
// For example you can divide two integers with
// total := DivideNumbers(1, 2)
// or you could divide an int with a float64.
// total := DivideNumbers(4, 3.2)
func DivideNumbers(a interface{}, b interface{}) (interface{}, error) {
switch a.(type) {
case int:
switch b.(type) {
case int:
return a.(int) / b.(int), nil
case int64:
return int64(a.(int)) / b.(int64), nil
case float64:
return float64(a.(int)) / b.(float64), nil
}
case int64:
switch b.(type) {
case int:
return a.(int64) / int64(b.(int)), nil
case int64:
return a.(int64) / b.(int64), nil
case float64:
return float64(a.(int64)) / b.(float64), nil
}
case float64:
switch b.(type) {
case int:
return a.(float64) / float64(b.(int)), nil
case int64:
return a.(float64) / float64(b.(int64)), nil
case float64:
return a.(float64) / b.(float64), nil
}
}
return 0, errors.New(fmt.Sprintf("Error dividing %#v (%v) - %#v (%v)", a, reflect.TypeOf(a).String(), b, reflect.TypeOf(b).String()))
}