improve math.digit and add math.count_digit

pull/13729/head
Kyle Pritchard 2022-03-13 16:16:58 +00:00 committed by Delyan Angelov
parent ca40cf3eb6
commit 1e607656ed
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 7 additions and 1 deletions

View File

@ -79,7 +79,7 @@ pub fn digits(_n i64, _b ...int) []int {
// count_digits return the number of digits in the number passed.
// Number argument accepts any integer type (i8|i16|int|isize|i64) and will be cast to i64
pub fn count_digits(n i64) int {
return int(math.ceil(math.log(f64(n) + 1 ) / math.log(10.0)))
return int(math.ceil(math.log(math.abs(f64(_n) + 1 )) / math.log(10.0)))
}
// minmax returns the minimum and maximum value of the two provided.

View File

@ -982,3 +982,9 @@ fn test_powi() {
assert powi(0, -2) == -1 // div by 0
assert powi(2, -1) == 0
}
fn test_count_digits() {
assert count_digits(12345) == 5
assert count_digits(i64(1234567890)) == 10
assert count_digits(-67345) == 5
}