Can i use long double to compute integers in c language











up vote
-1
down vote

favorite












I try to write a factorial function to compute a large number(factorial(105)),its result have 168 digit, so use long double, but it seems to be a error, can't it use like this?



#include <stdio.h>

long double factorial(long double n,long double base = 1){
if (n <= 1){
return 1*base;
}else{
return factorial(n-1,n * base);
}
}
int main(){
printf("%.0Lfn",factorial(25)); // this result is correct

printf("%.0Lfn",factorial(26));
//correct result is 403291461126605635584000000,but it return 403291461126605635592388608
return 0;
}









share|improve this question




















  • 1




    <s>Nope. ceil(log2(108!)) = 559 bits.</s> EDIT: Nevermind, you're using a double. But IIRC, the exponent still doesn't have enough bits to represent this.
    – Mateen Ulhaq
    Nov 20 at 6:16








  • 3




    Strongly suggest using the header file: bignum.h which if it is not already on your computer can be downloaded from: bignum.h
    – user3629249
    Nov 20 at 6:31






  • 6




    @user3629249: How are you going to "use a header file"? Header files are useless without the actual library these header files belong to. There's no point in "downloading it" if it is not "on your computer". It will achieve absolutely nothing.
    – AnT
    Nov 20 at 6:37












  • @AnT, Naturally, there is a library file associated with this header file. However, I figure the OP should know that relationship by now
    – user3629249
    Nov 20 at 7:21















up vote
-1
down vote

favorite












I try to write a factorial function to compute a large number(factorial(105)),its result have 168 digit, so use long double, but it seems to be a error, can't it use like this?



#include <stdio.h>

long double factorial(long double n,long double base = 1){
if (n <= 1){
return 1*base;
}else{
return factorial(n-1,n * base);
}
}
int main(){
printf("%.0Lfn",factorial(25)); // this result is correct

printf("%.0Lfn",factorial(26));
//correct result is 403291461126605635584000000,but it return 403291461126605635592388608
return 0;
}









share|improve this question




















  • 1




    <s>Nope. ceil(log2(108!)) = 559 bits.</s> EDIT: Nevermind, you're using a double. But IIRC, the exponent still doesn't have enough bits to represent this.
    – Mateen Ulhaq
    Nov 20 at 6:16








  • 3




    Strongly suggest using the header file: bignum.h which if it is not already on your computer can be downloaded from: bignum.h
    – user3629249
    Nov 20 at 6:31






  • 6




    @user3629249: How are you going to "use a header file"? Header files are useless without the actual library these header files belong to. There's no point in "downloading it" if it is not "on your computer". It will achieve absolutely nothing.
    – AnT
    Nov 20 at 6:37












  • @AnT, Naturally, there is a library file associated with this header file. However, I figure the OP should know that relationship by now
    – user3629249
    Nov 20 at 7:21













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I try to write a factorial function to compute a large number(factorial(105)),its result have 168 digit, so use long double, but it seems to be a error, can't it use like this?



#include <stdio.h>

long double factorial(long double n,long double base = 1){
if (n <= 1){
return 1*base;
}else{
return factorial(n-1,n * base);
}
}
int main(){
printf("%.0Lfn",factorial(25)); // this result is correct

printf("%.0Lfn",factorial(26));
//correct result is 403291461126605635584000000,but it return 403291461126605635592388608
return 0;
}









share|improve this question















I try to write a factorial function to compute a large number(factorial(105)),its result have 168 digit, so use long double, but it seems to be a error, can't it use like this?



#include <stdio.h>

long double factorial(long double n,long double base = 1){
if (n <= 1){
return 1*base;
}else{
return factorial(n-1,n * base);
}
}
int main(){
printf("%.0Lfn",factorial(25)); // this result is correct

printf("%.0Lfn",factorial(26));
//correct result is 403291461126605635584000000,but it return 403291461126605635592388608
return 0;
}






c factorial long-double






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 6:31









StoryTeller

91.8k12182249




91.8k12182249










asked Nov 20 at 6:11









archi

42




42








  • 1




    <s>Nope. ceil(log2(108!)) = 559 bits.</s> EDIT: Nevermind, you're using a double. But IIRC, the exponent still doesn't have enough bits to represent this.
    – Mateen Ulhaq
    Nov 20 at 6:16








  • 3




    Strongly suggest using the header file: bignum.h which if it is not already on your computer can be downloaded from: bignum.h
    – user3629249
    Nov 20 at 6:31






  • 6




    @user3629249: How are you going to "use a header file"? Header files are useless without the actual library these header files belong to. There's no point in "downloading it" if it is not "on your computer". It will achieve absolutely nothing.
    – AnT
    Nov 20 at 6:37












  • @AnT, Naturally, there is a library file associated with this header file. However, I figure the OP should know that relationship by now
    – user3629249
    Nov 20 at 7:21














  • 1




    <s>Nope. ceil(log2(108!)) = 559 bits.</s> EDIT: Nevermind, you're using a double. But IIRC, the exponent still doesn't have enough bits to represent this.
    – Mateen Ulhaq
    Nov 20 at 6:16








  • 3




    Strongly suggest using the header file: bignum.h which if it is not already on your computer can be downloaded from: bignum.h
    – user3629249
    Nov 20 at 6:31






  • 6




    @user3629249: How are you going to "use a header file"? Header files are useless without the actual library these header files belong to. There's no point in "downloading it" if it is not "on your computer". It will achieve absolutely nothing.
    – AnT
    Nov 20 at 6:37












  • @AnT, Naturally, there is a library file associated with this header file. However, I figure the OP should know that relationship by now
    – user3629249
    Nov 20 at 7:21








1




1




<s>Nope. ceil(log2(108!)) = 559 bits.</s> EDIT: Nevermind, you're using a double. But IIRC, the exponent still doesn't have enough bits to represent this.
– Mateen Ulhaq
Nov 20 at 6:16






<s>Nope. ceil(log2(108!)) = 559 bits.</s> EDIT: Nevermind, you're using a double. But IIRC, the exponent still doesn't have enough bits to represent this.
– Mateen Ulhaq
Nov 20 at 6:16






3




3




Strongly suggest using the header file: bignum.h which if it is not already on your computer can be downloaded from: bignum.h
– user3629249
Nov 20 at 6:31




Strongly suggest using the header file: bignum.h which if it is not already on your computer can be downloaded from: bignum.h
– user3629249
Nov 20 at 6:31




6




6




@user3629249: How are you going to "use a header file"? Header files are useless without the actual library these header files belong to. There's no point in "downloading it" if it is not "on your computer". It will achieve absolutely nothing.
– AnT
Nov 20 at 6:37






@user3629249: How are you going to "use a header file"? Header files are useless without the actual library these header files belong to. There's no point in "downloading it" if it is not "on your computer". It will achieve absolutely nothing.
– AnT
Nov 20 at 6:37














@AnT, Naturally, there is a library file associated with this header file. However, I figure the OP should know that relationship by now
– user3629249
Nov 20 at 7:21




@AnT, Naturally, there is a library file associated with this header file. However, I figure the OP should know that relationship by now
– user3629249
Nov 20 at 7:21












3 Answers
3






active

oldest

votes

















up vote
5
down vote



accepted










Back of the envelope calculation: 25! is slightly more than 1025; three orders of magnitude is roughly 10 bits, so you would need roughly 83 bits of mantissa even just to represent precisely the result.



Given that a long double, on platforms that support it, is generally 80 bits for the whole value (not just the mantissa!), apparently there's no way you have enough mantissa to perform that calculations of that order of magnitude with integer precision.



However: factorial here is a bit magic, as many of the factors contain powers of two, which just add binary zeroes to the right, which do not require mantissa (they end up in the exponent). In particular:



25! = 2   4   2   8   2    4    2    16    2    4     2    8    = 2²² · m
3 5 3 7 9 5 11 3 13 7 15 17 9 19 5 21 11 23 3 25


(m being the product of all non-2 factors, namely m = 310 · 56 · 73 · 112 · 13 · 17 · 19 · 23, so effectively the data we have to store in the mantissa)



Hence, our initial estimate exceeds the actual requirements by 22 bits.



It turns out that



log2(f) = 10·log23 + 6·log25 + 3·log27 + 2·log211 + log213 + log217 + log219 + log223 = 61.68



which is indeed just below the size of the mantissa of 80 bit long double (64 bit). But when you multiply it by 26 (so, excluding the factor 2, which ends up in the exponent, by 13), you are adding log2(13) = 3.7 bits. 61.7+3.7 is 65.4, so from 26! onwards you no longer have the precision to perform your calculation exactly.






share|improve this answer






























    up vote
    3
    down vote













    Firstly, nobody knows what long double can or cannot represent. Specific properties of the format depend on the implementation.



    Secondly, X86 extended precision floating-point format has 64-bit significand with explicit leading 1, which means that it can represent contiguous integers in ±264 range. Beyond that range representable integers are non-contiguous (i.e. they begin to "skip" with wider and wider gaps). Your factorials fall far outside that range, meaning that it is perfectly expected that they won't be represented precisely.






    share|improve this answer























    • I would have said that, as regular binary64/binary32 formats, it uses an implict leading 1... is that a typo or it is actually different?
      – Matteo Italia
      Nov 20 at 8:18






    • 1




      @MatteoItalia Yes, it is actually different. It uses explicit 1. This is rather natural: 80 bit extended precision x86 format is exactly the format used internally by FPU registers. All actual calculations are done in this format. And it is apparently more efficient to have that leading 1 explicitly present when it comes to actual calculations. Meanwhile, 32 bit and 64 bit are more like "packed" storage formats. When they get "unpacked" into FPU registers, that leading 1 becomes explicit.
      – AnT
      Nov 20 at 8:31












    • Well, TIL! Thank you!
      – Matteo Italia
      Nov 20 at 8:41


















    up vote
    3
    down vote













    Since 105! is a huge number that does not fit in a single word (or two of them), you want arbitrary precision arithmetic, also known as bignums. Use the Stirling's approximation to get an idea of how big 105! is and read the wikipage on factorials.



    Standard C (read n1570 to check) don't have bignums natively, but you'll find many libraries for that.



    I recommend GMPlib. BTW, some of its code is hand-written assembly for performance reason (when coding bignum addition, you want to take advantage of add with carry machine instructions).



    I recommend to avoid writing your own bignum operations. The existing libraries use very clever algorithms (and you'll need to make some PhD work to get something better). If you try coding your own bignum library, it probably will be much worse than competitors (unless you spend years of work).






    share|improve this answer























      Your Answer






      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: "1"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387206%2fcan-i-use-long-double-to-compute-integers-in-c-language%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      5
      down vote



      accepted










      Back of the envelope calculation: 25! is slightly more than 1025; three orders of magnitude is roughly 10 bits, so you would need roughly 83 bits of mantissa even just to represent precisely the result.



      Given that a long double, on platforms that support it, is generally 80 bits for the whole value (not just the mantissa!), apparently there's no way you have enough mantissa to perform that calculations of that order of magnitude with integer precision.



      However: factorial here is a bit magic, as many of the factors contain powers of two, which just add binary zeroes to the right, which do not require mantissa (they end up in the exponent). In particular:



      25! = 2   4   2   8   2    4    2    16    2    4     2    8    = 2²² · m
      3 5 3 7 9 5 11 3 13 7 15 17 9 19 5 21 11 23 3 25


      (m being the product of all non-2 factors, namely m = 310 · 56 · 73 · 112 · 13 · 17 · 19 · 23, so effectively the data we have to store in the mantissa)



      Hence, our initial estimate exceeds the actual requirements by 22 bits.



      It turns out that



      log2(f) = 10·log23 + 6·log25 + 3·log27 + 2·log211 + log213 + log217 + log219 + log223 = 61.68



      which is indeed just below the size of the mantissa of 80 bit long double (64 bit). But when you multiply it by 26 (so, excluding the factor 2, which ends up in the exponent, by 13), you are adding log2(13) = 3.7 bits. 61.7+3.7 is 65.4, so from 26! onwards you no longer have the precision to perform your calculation exactly.






      share|improve this answer



























        up vote
        5
        down vote



        accepted










        Back of the envelope calculation: 25! is slightly more than 1025; three orders of magnitude is roughly 10 bits, so you would need roughly 83 bits of mantissa even just to represent precisely the result.



        Given that a long double, on platforms that support it, is generally 80 bits for the whole value (not just the mantissa!), apparently there's no way you have enough mantissa to perform that calculations of that order of magnitude with integer precision.



        However: factorial here is a bit magic, as many of the factors contain powers of two, which just add binary zeroes to the right, which do not require mantissa (they end up in the exponent). In particular:



        25! = 2   4   2   8   2    4    2    16    2    4     2    8    = 2²² · m
        3 5 3 7 9 5 11 3 13 7 15 17 9 19 5 21 11 23 3 25


        (m being the product of all non-2 factors, namely m = 310 · 56 · 73 · 112 · 13 · 17 · 19 · 23, so effectively the data we have to store in the mantissa)



        Hence, our initial estimate exceeds the actual requirements by 22 bits.



        It turns out that



        log2(f) = 10·log23 + 6·log25 + 3·log27 + 2·log211 + log213 + log217 + log219 + log223 = 61.68



        which is indeed just below the size of the mantissa of 80 bit long double (64 bit). But when you multiply it by 26 (so, excluding the factor 2, which ends up in the exponent, by 13), you are adding log2(13) = 3.7 bits. 61.7+3.7 is 65.4, so from 26! onwards you no longer have the precision to perform your calculation exactly.






        share|improve this answer

























          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          Back of the envelope calculation: 25! is slightly more than 1025; three orders of magnitude is roughly 10 bits, so you would need roughly 83 bits of mantissa even just to represent precisely the result.



          Given that a long double, on platforms that support it, is generally 80 bits for the whole value (not just the mantissa!), apparently there's no way you have enough mantissa to perform that calculations of that order of magnitude with integer precision.



          However: factorial here is a bit magic, as many of the factors contain powers of two, which just add binary zeroes to the right, which do not require mantissa (they end up in the exponent). In particular:



          25! = 2   4   2   8   2    4    2    16    2    4     2    8    = 2²² · m
          3 5 3 7 9 5 11 3 13 7 15 17 9 19 5 21 11 23 3 25


          (m being the product of all non-2 factors, namely m = 310 · 56 · 73 · 112 · 13 · 17 · 19 · 23, so effectively the data we have to store in the mantissa)



          Hence, our initial estimate exceeds the actual requirements by 22 bits.



          It turns out that



          log2(f) = 10·log23 + 6·log25 + 3·log27 + 2·log211 + log213 + log217 + log219 + log223 = 61.68



          which is indeed just below the size of the mantissa of 80 bit long double (64 bit). But when you multiply it by 26 (so, excluding the factor 2, which ends up in the exponent, by 13), you are adding log2(13) = 3.7 bits. 61.7+3.7 is 65.4, so from 26! onwards you no longer have the precision to perform your calculation exactly.






          share|improve this answer














          Back of the envelope calculation: 25! is slightly more than 1025; three orders of magnitude is roughly 10 bits, so you would need roughly 83 bits of mantissa even just to represent precisely the result.



          Given that a long double, on platforms that support it, is generally 80 bits for the whole value (not just the mantissa!), apparently there's no way you have enough mantissa to perform that calculations of that order of magnitude with integer precision.



          However: factorial here is a bit magic, as many of the factors contain powers of two, which just add binary zeroes to the right, which do not require mantissa (they end up in the exponent). In particular:



          25! = 2   4   2   8   2    4    2    16    2    4     2    8    = 2²² · m
          3 5 3 7 9 5 11 3 13 7 15 17 9 19 5 21 11 23 3 25


          (m being the product of all non-2 factors, namely m = 310 · 56 · 73 · 112 · 13 · 17 · 19 · 23, so effectively the data we have to store in the mantissa)



          Hence, our initial estimate exceeds the actual requirements by 22 bits.



          It turns out that



          log2(f) = 10·log23 + 6·log25 + 3·log27 + 2·log211 + log213 + log217 + log219 + log223 = 61.68



          which is indeed just below the size of the mantissa of 80 bit long double (64 bit). But when you multiply it by 26 (so, excluding the factor 2, which ends up in the exponent, by 13), you are adding log2(13) = 3.7 bits. 61.7+3.7 is 65.4, so from 26! onwards you no longer have the precision to perform your calculation exactly.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 8:16

























          answered Nov 20 at 6:38









          Matteo Italia

          97.5k13137236




          97.5k13137236
























              up vote
              3
              down vote













              Firstly, nobody knows what long double can or cannot represent. Specific properties of the format depend on the implementation.



              Secondly, X86 extended precision floating-point format has 64-bit significand with explicit leading 1, which means that it can represent contiguous integers in ±264 range. Beyond that range representable integers are non-contiguous (i.e. they begin to "skip" with wider and wider gaps). Your factorials fall far outside that range, meaning that it is perfectly expected that they won't be represented precisely.






              share|improve this answer























              • I would have said that, as regular binary64/binary32 formats, it uses an implict leading 1... is that a typo or it is actually different?
                – Matteo Italia
                Nov 20 at 8:18






              • 1




                @MatteoItalia Yes, it is actually different. It uses explicit 1. This is rather natural: 80 bit extended precision x86 format is exactly the format used internally by FPU registers. All actual calculations are done in this format. And it is apparently more efficient to have that leading 1 explicitly present when it comes to actual calculations. Meanwhile, 32 bit and 64 bit are more like "packed" storage formats. When they get "unpacked" into FPU registers, that leading 1 becomes explicit.
                – AnT
                Nov 20 at 8:31












              • Well, TIL! Thank you!
                – Matteo Italia
                Nov 20 at 8:41















              up vote
              3
              down vote













              Firstly, nobody knows what long double can or cannot represent. Specific properties of the format depend on the implementation.



              Secondly, X86 extended precision floating-point format has 64-bit significand with explicit leading 1, which means that it can represent contiguous integers in ±264 range. Beyond that range representable integers are non-contiguous (i.e. they begin to "skip" with wider and wider gaps). Your factorials fall far outside that range, meaning that it is perfectly expected that they won't be represented precisely.






              share|improve this answer























              • I would have said that, as regular binary64/binary32 formats, it uses an implict leading 1... is that a typo or it is actually different?
                – Matteo Italia
                Nov 20 at 8:18






              • 1




                @MatteoItalia Yes, it is actually different. It uses explicit 1. This is rather natural: 80 bit extended precision x86 format is exactly the format used internally by FPU registers. All actual calculations are done in this format. And it is apparently more efficient to have that leading 1 explicitly present when it comes to actual calculations. Meanwhile, 32 bit and 64 bit are more like "packed" storage formats. When they get "unpacked" into FPU registers, that leading 1 becomes explicit.
                – AnT
                Nov 20 at 8:31












              • Well, TIL! Thank you!
                – Matteo Italia
                Nov 20 at 8:41













              up vote
              3
              down vote










              up vote
              3
              down vote









              Firstly, nobody knows what long double can or cannot represent. Specific properties of the format depend on the implementation.



              Secondly, X86 extended precision floating-point format has 64-bit significand with explicit leading 1, which means that it can represent contiguous integers in ±264 range. Beyond that range representable integers are non-contiguous (i.e. they begin to "skip" with wider and wider gaps). Your factorials fall far outside that range, meaning that it is perfectly expected that they won't be represented precisely.






              share|improve this answer














              Firstly, nobody knows what long double can or cannot represent. Specific properties of the format depend on the implementation.



              Secondly, X86 extended precision floating-point format has 64-bit significand with explicit leading 1, which means that it can represent contiguous integers in ±264 range. Beyond that range representable integers are non-contiguous (i.e. they begin to "skip" with wider and wider gaps). Your factorials fall far outside that range, meaning that it is perfectly expected that they won't be represented precisely.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 20 at 6:53

























              answered Nov 20 at 6:46









              AnT

              256k32408654




              256k32408654












              • I would have said that, as regular binary64/binary32 formats, it uses an implict leading 1... is that a typo or it is actually different?
                – Matteo Italia
                Nov 20 at 8:18






              • 1




                @MatteoItalia Yes, it is actually different. It uses explicit 1. This is rather natural: 80 bit extended precision x86 format is exactly the format used internally by FPU registers. All actual calculations are done in this format. And it is apparently more efficient to have that leading 1 explicitly present when it comes to actual calculations. Meanwhile, 32 bit and 64 bit are more like "packed" storage formats. When they get "unpacked" into FPU registers, that leading 1 becomes explicit.
                – AnT
                Nov 20 at 8:31












              • Well, TIL! Thank you!
                – Matteo Italia
                Nov 20 at 8:41


















              • I would have said that, as regular binary64/binary32 formats, it uses an implict leading 1... is that a typo or it is actually different?
                – Matteo Italia
                Nov 20 at 8:18






              • 1




                @MatteoItalia Yes, it is actually different. It uses explicit 1. This is rather natural: 80 bit extended precision x86 format is exactly the format used internally by FPU registers. All actual calculations are done in this format. And it is apparently more efficient to have that leading 1 explicitly present when it comes to actual calculations. Meanwhile, 32 bit and 64 bit are more like "packed" storage formats. When they get "unpacked" into FPU registers, that leading 1 becomes explicit.
                – AnT
                Nov 20 at 8:31












              • Well, TIL! Thank you!
                – Matteo Italia
                Nov 20 at 8:41
















              I would have said that, as regular binary64/binary32 formats, it uses an implict leading 1... is that a typo or it is actually different?
              – Matteo Italia
              Nov 20 at 8:18




              I would have said that, as regular binary64/binary32 formats, it uses an implict leading 1... is that a typo or it is actually different?
              – Matteo Italia
              Nov 20 at 8:18




              1




              1




              @MatteoItalia Yes, it is actually different. It uses explicit 1. This is rather natural: 80 bit extended precision x86 format is exactly the format used internally by FPU registers. All actual calculations are done in this format. And it is apparently more efficient to have that leading 1 explicitly present when it comes to actual calculations. Meanwhile, 32 bit and 64 bit are more like "packed" storage formats. When they get "unpacked" into FPU registers, that leading 1 becomes explicit.
              – AnT
              Nov 20 at 8:31






              @MatteoItalia Yes, it is actually different. It uses explicit 1. This is rather natural: 80 bit extended precision x86 format is exactly the format used internally by FPU registers. All actual calculations are done in this format. And it is apparently more efficient to have that leading 1 explicitly present when it comes to actual calculations. Meanwhile, 32 bit and 64 bit are more like "packed" storage formats. When they get "unpacked" into FPU registers, that leading 1 becomes explicit.
              – AnT
              Nov 20 at 8:31














              Well, TIL! Thank you!
              – Matteo Italia
              Nov 20 at 8:41




              Well, TIL! Thank you!
              – Matteo Italia
              Nov 20 at 8:41










              up vote
              3
              down vote













              Since 105! is a huge number that does not fit in a single word (or two of them), you want arbitrary precision arithmetic, also known as bignums. Use the Stirling's approximation to get an idea of how big 105! is and read the wikipage on factorials.



              Standard C (read n1570 to check) don't have bignums natively, but you'll find many libraries for that.



              I recommend GMPlib. BTW, some of its code is hand-written assembly for performance reason (when coding bignum addition, you want to take advantage of add with carry machine instructions).



              I recommend to avoid writing your own bignum operations. The existing libraries use very clever algorithms (and you'll need to make some PhD work to get something better). If you try coding your own bignum library, it probably will be much worse than competitors (unless you spend years of work).






              share|improve this answer



























                up vote
                3
                down vote













                Since 105! is a huge number that does not fit in a single word (or two of them), you want arbitrary precision arithmetic, also known as bignums. Use the Stirling's approximation to get an idea of how big 105! is and read the wikipage on factorials.



                Standard C (read n1570 to check) don't have bignums natively, but you'll find many libraries for that.



                I recommend GMPlib. BTW, some of its code is hand-written assembly for performance reason (when coding bignum addition, you want to take advantage of add with carry machine instructions).



                I recommend to avoid writing your own bignum operations. The existing libraries use very clever algorithms (and you'll need to make some PhD work to get something better). If you try coding your own bignum library, it probably will be much worse than competitors (unless you spend years of work).






                share|improve this answer

























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  Since 105! is a huge number that does not fit in a single word (or two of them), you want arbitrary precision arithmetic, also known as bignums. Use the Stirling's approximation to get an idea of how big 105! is and read the wikipage on factorials.



                  Standard C (read n1570 to check) don't have bignums natively, but you'll find many libraries for that.



                  I recommend GMPlib. BTW, some of its code is hand-written assembly for performance reason (when coding bignum addition, you want to take advantage of add with carry machine instructions).



                  I recommend to avoid writing your own bignum operations. The existing libraries use very clever algorithms (and you'll need to make some PhD work to get something better). If you try coding your own bignum library, it probably will be much worse than competitors (unless you spend years of work).






                  share|improve this answer














                  Since 105! is a huge number that does not fit in a single word (or two of them), you want arbitrary precision arithmetic, also known as bignums. Use the Stirling's approximation to get an idea of how big 105! is and read the wikipage on factorials.



                  Standard C (read n1570 to check) don't have bignums natively, but you'll find many libraries for that.



                  I recommend GMPlib. BTW, some of its code is hand-written assembly for performance reason (when coding bignum addition, you want to take advantage of add with carry machine instructions).



                  I recommend to avoid writing your own bignum operations. The existing libraries use very clever algorithms (and you'll need to make some PhD work to get something better). If you try coding your own bignum library, it probably will be much worse than competitors (unless you spend years of work).







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 at 7:01

























                  answered Nov 20 at 6:56









                  Basile Starynkevitch

                  174k13164357




                  174k13164357






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387206%2fcan-i-use-long-double-to-compute-integers-in-c-language%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      To store a contact into the json file from server.js file using a class in NodeJS

                      Redirect URL with Chrome Remote Debugging Android Devices

                      Dieringhausen