Cleanup math

License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2019-12-10 20:53:39 +01:00
parent 0fa20f6074
commit bfdfd4023e
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA
2 changed files with 16 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package cli
import (
"fmt"
"math/big"
"github.com/filecoin-project/lotus/chain/types"
)
@ -9,11 +10,15 @@ import (
var Units = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"}
func SizeStr(size types.BigInt) string {
size = types.BigMul(size, types.NewInt(100))
i := 0
for types.BigCmp(size, types.NewInt(102400)) >= 0 && i < len(Units)-1 {
size = types.BigDiv(size, types.NewInt(1024))
r := new(big.Rat).SetInt(size.Int)
den := big.NewRat(1, 1024)
var i int
for f, _ := r.Float64(); f >= 1024 && 1 < len(Units); f, _ = r.Float64() {
i++
r = r.Mul(r, den)
}
return fmt.Sprintf("%s.%s %s", types.BigDiv(size, types.NewInt(100)), types.BigMod(size, types.NewInt(100)), Units[i])
f, _ := r.Float64()
return fmt.Sprintf("%.3f %s", f, Units[i])
}

View File

@ -6,8 +6,8 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/filecoin-project/lotus/chain/types"
"io/ioutil"
"math/big"
"math/rand"
"os"
"path/filepath"
@ -23,6 +23,7 @@ import (
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/address"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/genesis"
"github.com/filecoin-project/lotus/lib/sectorbuilder"
)
@ -361,5 +362,8 @@ func main() {
}
func bps(data uint64, d time.Duration) string {
return lcli.SizeStr(types.BigDiv(types.BigMul(types.NewInt(data), types.NewInt(uint64(time.Second))), types.NewInt(uint64(d)))) + "/s"
bdata := new(big.Int).SetUint64(data)
bdata = bdata.Mul(bdata, big.NewInt(time.Second.Nanoseconds()))
bps := bdata.Div(bdata, big.NewInt(d.Nanoseconds()))
return lcli.SizeStr(types.BigInt{bps}) + "/s"
}