MCPcopy Index your code
hub / github.com/pquerna/ffjson / adjustLastDigit

Function adjustLastDigit

fflib/v1/extfloat.go:645–668  ·  view source on GitHub ↗

adjustLastDigit modifies d = x-currentDiff*ε, to get closest to d = x-targetDiff*ε, without becoming smaller than x-maxDiff*ε. It assumes that a decimal digit is worth ulpDecimal*ε, and that all data is known with a error estimate of ulpBinary*ε.

(d *decimalSlice, currentDiff, targetDiff, maxDiff, ulpDecimal, ulpBinary uint64)

Source from the content-addressed store, hash-verified

643// It assumes that a decimal digit is worth ulpDecimal*ε, and that
644// all data is known with a error estimate of ulpBinary*ε.
645func adjustLastDigit(d *decimalSlice, currentDiff, targetDiff, maxDiff, ulpDecimal, ulpBinary uint64) bool {
646 if ulpDecimal < 2*ulpBinary {
647 // Approximation is too wide.
648 return false
649 }
650 for currentDiff+ulpDecimal/2+ulpBinary < targetDiff {
651 d.d[d.nd-1]--
652 currentDiff += ulpDecimal
653 }
654 if currentDiff+ulpDecimal <= targetDiff+ulpDecimal/2+ulpBinary {
655 // we have two choices, and don't know what to do.
656 return false
657 }
658 if currentDiff < ulpBinary || currentDiff > maxDiff-ulpBinary {
659 // we went too far
660 return false
661 }
662 if d.nd == 1 && d.d[0] == '0' {
663 // the number has actually reached zero.
664 d.nd = 0
665 d.dp = 0
666 }
667 return true
668}

Callers 1

ShortestDecimalMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…