What is this mathematical principle?

The

code is as follows: why does the input timestamp change and the resulting result is the same in a certain interval?

//main.go
func PrevSlot(now int64) int64 {
    // now = time.Now().Unix()
    var blockInterval = int64(10)

    result := int64((now-1)/blockInterval) * blockInterval // why result is same ?
    plog.Println("PrevSlot:", int64(result), int64((now-1)/blockInterval), now)
    return result
}


func main() {
    ticker := time.NewTicker(1 * time.Second)
    for {
        <-ticker.C

        now := time.Now().Unix()
        PrevSlot(now)
    }

}


/**
output:
2019-01-16 10:58:31.668597 I | dpos.go: PrevSlot: 1547607510 154760751 1547607511
2019-01-16 10:58:32.668649 I | dpos.go: PrevSlot: 1547607510 154760751 1547607512
2019-01-16 10:58:33.668568 I | dpos.go: PrevSlot: 1547607510 154760751 1547607513
2019-01-16 10:58:34.668572 I | dpos.go: PrevSlot: 1547607510 154760751 1547607514
2019-01-16 10:58:35.668547 I | dpos.go: PrevSlot: 1547607510 154760751 1547607515

 */
Apr.26,2022

this is a very common method of taking discrete values, and a general formula is:

[a / 10] * 10

here [] indicates rounding. That is, when an is 099, the result of [a / 10] is that when an is 1019, the result is 1, and so on, and then 10, the result of the whole expression is a series of discrete points: 0,10,20,30. .


description: / represents integer division For example, 11 / 10 = 1
11 / 10 * 10 = 10
12 / 10 * 10 = 10
13 / 10 * 10 = 10
14 / 10 * 10 = 10
15 / 10 * 10 = 10
16 / 10 * 10 = 10
17 / 10 * 10 = 10
18 / 10 * 10 = 10
19 / 10 * 10 = 10


you can take a look at this:
(101-1) / 10 * 10
(102-1) / 10 * 10
because it is int type. So in the interval of 10, the result is all the same
. The algorithm is similar to
now-now% 10

.
Menu