-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods.go
69 lines (56 loc) · 1.9 KB
/
methods.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
package main
import (
"fmt"
"math"
)
func calculateStraightlineDepreciation(initValue int, scrapValue int, period int) ([]int, error) {
if period <= 0 {
return nil, fmt.Errorf("period must be greater than 0")
}
if initValue <= scrapValue {
return nil, fmt.Errorf("initial value must be greater than scrap value")
}
amountDepreciation := initValue - scrapValue
singleDepreciation := math.Floor(float64(amountDepreciation) / float64(period))
depreciations := make([]int, period)
for i := 0; i < period-1; i++ {
depreciations[i] = int(singleDepreciation)
}
// Adjust the last depreciation to match the scrap value
depreciations[period-1] = initValue - scrapValue - int(singleDepreciation)*(period-1)
return depreciations, nil
}
func calculateDoubleDecliningBalance(initValue int, scrapValue int, period int) ([]int, error) {
if period <= 0 {
return nil, fmt.Errorf("period must be greater than 0")
}
if initValue <= scrapValue {
return nil, fmt.Errorf("initial value must be greater than scrap value")
}
depreciations := make([]int, period)
currentValue := float64(initValue)
for i := 0; i < period; i++ {
depreciation := 2.0 / float64(period) * currentValue
if currentValue-depreciation < float64(scrapValue) {
depreciation = currentValue - float64(scrapValue)
}
depreciations[i] = int(depreciation)
currentValue -= depreciation
}
return depreciations, nil
}
func calculateSumOfTheYearsDigits(initValue int, scrapValue int, period int) ([]int, error) {
if period <= 0 {
return nil, fmt.Errorf("period must be greater than 0")
}
if initValue <= scrapValue {
return nil, fmt.Errorf("initial value must be greater than scrap value")
}
amountDepreciation := initValue - scrapValue
depreciations := make([]int, period)
sumYears := period * (period + 1) / 2
for i := 0; i < period; i++ {
depreciations[i] = (period - i) * amountDepreciation / sumYears
}
return depreciations, nil
}