Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4015610
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:38:24+00:00 2026-05-20T09:38:24+00:00

Is there an easy way to determine the number of digits a GMP integer

  • 0

Is there an easy way to determine the number of digits a GMP integer has? I know you can determine it through a log, but I was wondering if there was something built into the library that I’m missing. The only thing I’ve found in the manual is:

_mp_size The number of limbs, or the negative of that when representing a negative integer.
Zero is represented by _mp_size set to zero, in which case the _mp_d data is unused.

But I’m under the impression that is quite different than what I’m looking for.

i.e

124839 = 6 digits.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:38:25+00:00Added an answer on May 20, 2026 at 9:38 am

    You can use size_t mpz_sizeinbase (mpz_t op, int base) to get the number of characters to output the number as a string in a specific base.

    size_t mpz_sizeinbase (mpz_t op, int base)

    Return the size of op measured in number of digits in the given base. base can vary from 2 to 62. The sign of op is ignored, just the absolute value is used. The result will be either exact or 1 too big. If base is a power of 2, the result is always exact. If op is zero the return value is always 1.

    This function can be used to determine the space required when converting op to a string. The right amount of allocation is normally two more than the value returned by mpz_sizeinbase, one extra for a minus sign and one for the null-terminator.

    So something along the lines of:

    size_t sz = mpz_sizeinbase (myNum, 10);
    

    should be a good start.

    If you want the exact size, you can use that value to create a big enough buffer, output the value to that buffer, then do a strlen to get the more accurate size, something like:

    size_t sz = mpz_sizeinbase (myNum, 10) + 1; // allow for sign
    char *buff = malloc (sz + 1);               // allow for `\0`
    if (buff != NULL) {
        gmp_sprintf (buff, "%Zd", myNum);
        sz = strlen (buff);
        free (buff);
    }
    

    Note that it’s not the most efficient way since it allocates a buffer every time you want to find the length, and it defaults to the safest size if the allocation fails, which could be one larger than necessary.

    Another possible way is to use the safer snprintf option, since that returns the number of bytes that would have been written, and prevents buffer overflow:

    char oneChar;
    int sz = gmp_snprintf (&oneChar, 1, "%Zd", myNum);
    

    I haven’t tested that specifically but it’s a trick I’ve used for "regular" C-style printing before.

    Note that both those "exact size" solutions include an optional sign at the front. If you want to truly count the digits rather then the characters, you should adjust for that (subtracting one from the size if the number is less than zero, for example).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an easy way to determine the sign of a floating point number?
I want to know if there is any easy/elegant way to determine the source
Is there an easy way to programmatically determine the number of lines within a
Is there an easy way to determine in a SQL Script if the ORACLE
Is there an easy way to programmatically determine the speed (or version) of a
Is there an easy way to log PHP errors to a log for a
Is there an easy way to determine which properties of my model have changed
Is there an easy way to determine if a year is a leap year?
Is there any easy way to determine how much space each table in an
Just trying to determine if there is an easy way to determine what contact

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.