Codebase list golang-github-vbauerster-mpb / 6606af9
fix math.Round Vladimir Bauer 8 years ago
2 changed file(s) with 51 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
269269 }
270270
271271 var str string
272 timeRemaining := time.Duration(st.Total-st.Current) * time.Duration(math.Round(s.Value()))
272 timeRemaining := time.Duration(st.Total-st.Current) * time.Duration(round(s.Value()))
273273 hours := int64((timeRemaining / time.Hour) % 60)
274274 minutes := int64((timeRemaining / time.Minute) % 60)
275275 seconds := int64((timeRemaining / time.Second) % 60)
353353 }
354354
355355 p := float64(width) * float64(current) / float64(total)
356 return int64(math.Round(p))
356 return int64(round(p))
357357 }
358358
359359 // SpeedNoUnit returns raw I/O operation speed decorator.
0 package decor
1
2 import "math"
3
4 const (
5 uvone = 0x3FF0000000000000
6 mask = 0x7FF
7 shift = 64 - 11 - 1
8 bias = 1023
9 signMask = 1 << 63
10 fracMask = 1<<shift - 1
11 )
12
13 // round returns the nearest integer, rounding half away from zero.
14 //
15 // Special cases are:
16 // Round(±0) = ±0
17 // Round(±Inf) = ±Inf
18 // Round(NaN) = NaN
19 func round(x float64) float64 {
20 // Round is a faster implementation of:
21 //
22 // func Round(x float64) float64 {
23 // t := Trunc(x)
24 // if Abs(x-t) >= 0.5 {
25 // return t + Copysign(1, x)
26 // }
27 // return t
28 // }
29 bits := math.Float64bits(x)
30 e := uint(bits>>shift) & mask
31 if e < bias {
32 // Round abs(x) < 1 including denormals.
33 bits &= signMask // +-0
34 if e == bias-1 {
35 bits |= uvone // +-1
36 }
37 } else if e < bias+shift {
38 // Round any abs(x) >= 1 containing a fractional component [0,1).
39 //
40 // Numbers with larger exponents are returned unchanged since they
41 // must be either an integer, infinity, or NaN.
42 const half = 1 << (shift - 1)
43 e -= bias
44 bits += half >> e
45 bits &^= fracMask >> e
46 }
47 return math.Float64frombits(bits)
48 }