MCPcopy
hub / github.com/shopspring/decimal / roundShortest

Function roundShortest

rounding.go:25–160  ·  view source on GitHub ↗

roundShortest rounds d (= mant * 2^exp) to the shortest number of digits that will let the original floating point value be precisely reconstructed.

(d *decimal, mant uint64, exp int, flt *floatInfo)

Source from the content-addressed store, hash-verified

23// roundShortest rounds d (= mant * 2^exp) to the shortest number of digits
24// that will let the original floating point value be precisely reconstructed.
25func roundShortest(d *decimal, mant uint64, exp int, flt *floatInfo) {
26 // If mantissa is zero, the number is zero; stop now.
27 if mant == 0 {
28 d.nd = 0
29 return
30 }
31
32 // Compute upper and lower such that any decimal number
33 // between upper and lower (possibly inclusive)
34 // will round to the original floating point number.
35
36 // We may see at once that the number is already shortest.
37 //
38 // Suppose d is not denormal, so that 2^exp <= d < 10^dp.
39 // The closest shorter number is at least 10^(dp-nd) away.
40 // The lower/upper bounds computed below are at distance
41 // at most 2^(exp-mantbits).
42 //
43 // So the number is already shortest if 10^(dp-nd) > 2^(exp-mantbits),
44 // or equivalently log2(10)*(dp-nd) > exp-mantbits.
45 // It is true if 332/100*(dp-nd) >= exp-mantbits (log2(10) > 3.32).
46 minexp := flt.bias + 1 // minimum possible exponent
47 if exp > minexp && 332*(d.dp-d.nd) >= 100*(exp-int(flt.mantbits)) {
48 // The number is already shortest.
49 return
50 }
51
52 // d = mant << (exp - mantbits)
53 // Next highest floating point number is mant+1 << exp-mantbits.
54 // Our upper bound is halfway between, mant*2+1 << exp-mantbits-1.
55 upper := new(decimal)
56 upper.Assign(mant*2 + 1)
57 upper.Shift(exp - int(flt.mantbits) - 1)
58
59 // d = mant << (exp - mantbits)
60 // Next lowest floating point number is mant-1 << exp-mantbits,
61 // unless mant-1 drops the significant bit and exp is not the minimum exp,
62 // in which case the next lowest is mant*2-1 << exp-mantbits-1.
63 // Either way, call it mantlo << explo-mantbits.
64 // Our lower bound is halfway between, mantlo*2+1 << explo-mantbits-1.
65 var mantlo uint64
66 var explo int
67 if mant > 1<<flt.mantbits || exp == minexp {
68 mantlo = mant - 1
69 explo = exp
70 } else {
71 mantlo = mant*2 - 1
72 explo = exp - 1
73 }
74 lower := new(decimal)
75 lower.Assign(mantlo*2 + 1)
76 lower.Shift(explo - int(flt.mantbits) - 1)
77
78 // The upper and lower bounds are possible outputs only if
79 // the original mantissa is even, so that IEEE round-to-even
80 // would round to the original mantissa and not the neighbors.
81 inclusive := mant%2 == 0
82

Callers 1

newFromFloatFunction · 0.85

Calls 5

AssignMethod · 0.80
ShiftMethod · 0.45
RoundMethod · 0.45
RoundDownMethod · 0.45
RoundUpMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…