Count the number of digits in an integer using F# and log10

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
This code counts the number of digits in an integer.
let digitCount number = (int) (log10 ((float)number)) + 1;
For instance, it tells us 123456789 has 9 digits.
let input = 123456789;
printfn "%i has %i digits" input (digitCount input);
https://dotnetfiddle.net/nxuoah
A weakness of the code is that it uses two casts. Can we use math to count the number of digits in an integer without having to cast twice? The reason to cast to a float is to make the type compatible with a the log10 function. The reason to cast back in to an integer is to represent a "count" of digits instead of a "measure" of digits.
A second, related weakness of the code is the number of parentheses, which makes the code more difficult to read.
functional-programming f#
add a comment |Â
up vote
0
down vote
favorite
This code counts the number of digits in an integer.
let digitCount number = (int) (log10 ((float)number)) + 1;
For instance, it tells us 123456789 has 9 digits.
let input = 123456789;
printfn "%i has %i digits" input (digitCount input);
https://dotnetfiddle.net/nxuoah
A weakness of the code is that it uses two casts. Can we use math to count the number of digits in an integer without having to cast twice? The reason to cast to a float is to make the type compatible with a the log10 function. The reason to cast back in to an integer is to represent a "count" of digits instead of a "measure" of digits.
A second, related weakness of the code is the number of parentheses, which makes the code more difficult to read.
functional-programming f#
1
Usinglog10seems like overkill. You can do this recursively by dividing (well, integer division) the number by 10 repeatedly.
â Dair
Jan 18 at 6:50
You can also cast as a string and then count the length.
â Acccumulation
Jan 18 at 18:39
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This code counts the number of digits in an integer.
let digitCount number = (int) (log10 ((float)number)) + 1;
For instance, it tells us 123456789 has 9 digits.
let input = 123456789;
printfn "%i has %i digits" input (digitCount input);
https://dotnetfiddle.net/nxuoah
A weakness of the code is that it uses two casts. Can we use math to count the number of digits in an integer without having to cast twice? The reason to cast to a float is to make the type compatible with a the log10 function. The reason to cast back in to an integer is to represent a "count" of digits instead of a "measure" of digits.
A second, related weakness of the code is the number of parentheses, which makes the code more difficult to read.
functional-programming f#
This code counts the number of digits in an integer.
let digitCount number = (int) (log10 ((float)number)) + 1;
For instance, it tells us 123456789 has 9 digits.
let input = 123456789;
printfn "%i has %i digits" input (digitCount input);
https://dotnetfiddle.net/nxuoah
A weakness of the code is that it uses two casts. Can we use math to count the number of digits in an integer without having to cast twice? The reason to cast to a float is to make the type compatible with a the log10 function. The reason to cast back in to an integer is to represent a "count" of digits instead of a "measure" of digits.
A second, related weakness of the code is the number of parentheses, which makes the code more difficult to read.
functional-programming f#
asked Jan 18 at 6:38
Shaun Luttin
22618
22618
1
Usinglog10seems like overkill. You can do this recursively by dividing (well, integer division) the number by 10 repeatedly.
â Dair
Jan 18 at 6:50
You can also cast as a string and then count the length.
â Acccumulation
Jan 18 at 18:39
add a comment |Â
1
Usinglog10seems like overkill. You can do this recursively by dividing (well, integer division) the number by 10 repeatedly.
â Dair
Jan 18 at 6:50
You can also cast as a string and then count the length.
â Acccumulation
Jan 18 at 18:39
1
1
Using
log10 seems like overkill. You can do this recursively by dividing (well, integer division) the number by 10 repeatedly.â Dair
Jan 18 at 6:50
Using
log10 seems like overkill. You can do this recursively by dividing (well, integer division) the number by 10 repeatedly.â Dair
Jan 18 at 6:50
You can also cast as a string and then count the length.
â Acccumulation
Jan 18 at 18:39
You can also cast as a string and then count the length.
â Acccumulation
Jan 18 at 18:39
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Your code contains a bug. What happens if number is 0? log10 0 should return -Infinity, so we end up with the smallest int plus one. Therefore, we need to check whether input is 0 first:
let digitCount number = if number == 0 then 1 else (int) (log10 ((float)number)) + 1;
At that point we're not really succinct anymore, so there's no harm in using a recursive function instead:
let rec digitCount number = if number < 10 then 1 else 1 + digitCount (number / 10)
However, that's not tail recursive, so we would want to rewrite it using a loop that's tail recursive. That's left as an exercise, though.
That being said, one can optimize counting digit a lot.
Andrei Alexandrescu held a talk about optimizing digits_base_10 in C++ several years ago. The original talk is offline, unfortunately, but the slides are still there.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Your code contains a bug. What happens if number is 0? log10 0 should return -Infinity, so we end up with the smallest int plus one. Therefore, we need to check whether input is 0 first:
let digitCount number = if number == 0 then 1 else (int) (log10 ((float)number)) + 1;
At that point we're not really succinct anymore, so there's no harm in using a recursive function instead:
let rec digitCount number = if number < 10 then 1 else 1 + digitCount (number / 10)
However, that's not tail recursive, so we would want to rewrite it using a loop that's tail recursive. That's left as an exercise, though.
That being said, one can optimize counting digit a lot.
Andrei Alexandrescu held a talk about optimizing digits_base_10 in C++ several years ago. The original talk is offline, unfortunately, but the slides are still there.
add a comment |Â
up vote
1
down vote
accepted
Your code contains a bug. What happens if number is 0? log10 0 should return -Infinity, so we end up with the smallest int plus one. Therefore, we need to check whether input is 0 first:
let digitCount number = if number == 0 then 1 else (int) (log10 ((float)number)) + 1;
At that point we're not really succinct anymore, so there's no harm in using a recursive function instead:
let rec digitCount number = if number < 10 then 1 else 1 + digitCount (number / 10)
However, that's not tail recursive, so we would want to rewrite it using a loop that's tail recursive. That's left as an exercise, though.
That being said, one can optimize counting digit a lot.
Andrei Alexandrescu held a talk about optimizing digits_base_10 in C++ several years ago. The original talk is offline, unfortunately, but the slides are still there.
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Your code contains a bug. What happens if number is 0? log10 0 should return -Infinity, so we end up with the smallest int plus one. Therefore, we need to check whether input is 0 first:
let digitCount number = if number == 0 then 1 else (int) (log10 ((float)number)) + 1;
At that point we're not really succinct anymore, so there's no harm in using a recursive function instead:
let rec digitCount number = if number < 10 then 1 else 1 + digitCount (number / 10)
However, that's not tail recursive, so we would want to rewrite it using a loop that's tail recursive. That's left as an exercise, though.
That being said, one can optimize counting digit a lot.
Andrei Alexandrescu held a talk about optimizing digits_base_10 in C++ several years ago. The original talk is offline, unfortunately, but the slides are still there.
Your code contains a bug. What happens if number is 0? log10 0 should return -Infinity, so we end up with the smallest int plus one. Therefore, we need to check whether input is 0 first:
let digitCount number = if number == 0 then 1 else (int) (log10 ((float)number)) + 1;
At that point we're not really succinct anymore, so there's no harm in using a recursive function instead:
let rec digitCount number = if number < 10 then 1 else 1 + digitCount (number / 10)
However, that's not tail recursive, so we would want to rewrite it using a loop that's tail recursive. That's left as an exercise, though.
That being said, one can optimize counting digit a lot.
Andrei Alexandrescu held a talk about optimizing digits_base_10 in C++ several years ago. The original talk is offline, unfortunately, but the slides are still there.
edited Jan 18 at 16:10
Shaun Luttin
22618
22618
answered Jan 18 at 7:18
Zeta
14.3k23267
14.3k23267
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185369%2fcount-the-number-of-digits-in-an-integer-using-f-and-log10%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Using
log10seems like overkill. You can do this recursively by dividing (well, integer division) the number by 10 repeatedly.â Dair
Jan 18 at 6:50
You can also cast as a string and then count the length.
â Acccumulation
Jan 18 at 18:39