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

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question















  • 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











  • You can also cast as a string and then count the length.
    – Acccumulation
    Jan 18 at 18:39
















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.







share|improve this question















  • 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











  • You can also cast as a string and then count the length.
    – Acccumulation
    Jan 18 at 18:39












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.







share|improve this question











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.









share|improve this question










share|improve this question




share|improve this question









asked Jan 18 at 6:38









Shaun Luttin

22618




22618







  • 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











  • You can also cast as a string and then count the length.
    – Acccumulation
    Jan 18 at 18:39












  • 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











  • 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










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.






share|improve this answer























    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    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






























    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.






    share|improve this answer



























      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.






      share|improve this answer

























        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.






        share|improve this answer















        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.







        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jan 18 at 16:10









        Shaun Luttin

        22618




        22618











        answered Jan 18 at 7:18









        Zeta

        14.3k23267




        14.3k23267






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            Python Lists

            Aion

            JavaScript Array Iteration Methods