-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into kafka-heartbeats
- Loading branch information
Showing
6 changed files
with
101 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package datatypes | ||
|
||
import ( | ||
"math" | ||
"math/big" | ||
) | ||
|
||
var tenInt = big.NewInt(10) | ||
|
||
func CountDigits(bi *big.Int) int { | ||
if bi.IsInt64() { | ||
i64 := bi.Int64() | ||
// restrict fast path to integers with exact conversion to float64 | ||
if i64 <= (1<<53) && i64 >= -(1<<53) { | ||
if i64 == 0 { | ||
return 1 | ||
} | ||
return int(math.Log10(math.Abs(float64(i64)))) + 1 | ||
} | ||
} | ||
|
||
estimatedNumDigits := int(float64(bi.BitLen()) / math.Log2(10)) | ||
|
||
// estimatedNumDigits (lg10) may be off by 1, need to verify | ||
digitsBigInt := big.NewInt(int64(estimatedNumDigits)) | ||
errorCorrectionUnit := digitsBigInt.Exp(tenInt, digitsBigInt, nil) | ||
|
||
if bi.CmpAbs(errorCorrectionUnit) >= 0 { | ||
return estimatedNumDigits + 1 | ||
} | ||
|
||
return estimatedNumDigits | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package datatypes | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
) | ||
|
||
func TestCountDigits(t *testing.T) { | ||
bi := big.NewInt(-0) | ||
expected := 1 | ||
result := CountDigits(bi) | ||
if result != expected { | ||
t.Errorf("Unexpected result. Expected: %v, but got: %v", expected, result) | ||
} | ||
|
||
bi = big.NewInt(0) | ||
expected = 1 | ||
result = CountDigits(bi) | ||
if result != expected { | ||
t.Errorf("Unexpected result. Expected: %v, but got: %v", expected, result) | ||
} | ||
|
||
// 18 nines | ||
bi = big.NewInt(999999999999999999) | ||
result = CountDigits(bi) | ||
expected = 18 | ||
if result != expected { | ||
t.Errorf("Unexpected result. Expected: %v, but got: %v", expected, result) | ||
} | ||
|
||
// 18 nines | ||
bi = big.NewInt(-999999999999999999) | ||
result = CountDigits(bi) | ||
expected = 18 | ||
if result != expected { | ||
t.Errorf("Unexpected result. Expected: %v, but got: %v", expected, result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters