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 6702681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:04:55+00:00 2026-05-26T07:04:55+00:00

Though the representations of a number are somewhat a relative aspect, we generally use

  • 0

Though the representations of a number are somewhat a relative aspect, we
generally use the decimal form when printing to the outer world.

I’m in a Mac OS X, and while analyzing the source of libc I found that the
famous printf function ends up calling a little function __ultoa — after
going through vfprintf_l, the 1104 lines __vfprintf and finally __ultoa.
It’s defined as follows (in this case all these come straight from FreeBSD):

/*
 * Convert an unsigned long to ASCII for printf purposes, returning
 * a pointer to the first character of the string representation.
 * Octal numbers can be forced to have a leading zero; hex numbers
 * use the given digits.
 */
static CHAR *
__ultoa(u_long val, CHAR *endp, int base, int octzero, const char *xdigs)
{
    CHAR *cp = endp;
    long sval;

    /*
     * Handle the three cases separately, in the hope of getting
     * better/faster code.
     */
    switch (base) {
    case 10:
        if (val < 10) {     /* many numbers are 1 digit */
            *--cp = to_char(val);
            return (cp);
        }
        /*
         * On many machines, unsigned arithmetic is harder than
         * signed arithmetic, so we do at most one unsigned mod and
         * divide; this is sufficient to reduce the range of
         * the incoming value to where signed arithmetic works.
         */
        if (val > LONG_MAX) {
            *--cp = to_char(val % 10);
            sval = val / 10;
        } else
            sval = val;
        do {
            *--cp = to_char(sval % 10);
            sval /= 10;
        } while (sval != 0);
        break;

    case 8:
        do {
            *--cp = to_char(val & 7);
            val >>= 3;
        } while (val);
        if (octzero && *cp != '0')
            *--cp = '0';
        break;

    case 16:
        do {
            *--cp = xdigs[val & 15];
            val >>= 4;
        } while (val);
        break;

    default:                /* oops */
        LIBC_ABORT("__ultoa: invalid base=%d", base);
    }
    return (cp);
}

Here CHAR is just typedef’ed to char (for some reason) and to_char does
basically what you’d expect:

#define to_char(n)  ((n) + '0')

The translation to the decimal form occurs in a straightforward way, dividing by
10 and taking %10:

do {
    *--cp = to_char(sval % 10);
    sval /= 10;
} while (sval != 0);

However, while this work for little numbers (up to 8 bytes) it seems too much
“manual labor” for me. In GMP, you can easily calculate 25000:

mpz_t n;
mpz_init(n);
mpz_ui_pow_ui(n, 2ul, 5000ul);
gmp_printf("%Zd\n", n);

While this has an easy representation for bases 2 or 16, the decimal form is a
bit harder to calculate.

So, how exactly libraries like GMP handles these? Looks like taking modulo and
divisions can be expensive for such big numbers. Is there any faster algorithm,
or am I wrong and the standard process is easy for computers?

  • 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-26T07:04:56+00:00Added an answer on May 26, 2026 at 7:04 am

    The standard process is not easy, but one way or another you need to do the equivalent operations to obtain the decimal digits, and this can involve high-precision arithmetic even if the original value in binary is just a few bits or a single bit. See my question:

    How do you print the EXACT value of a floating point number?

    It’s about floating point, but all large floating point numbers are integers anyway, and the very-large and very-small cases are the only interesting cases.

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

Sidebar

Related Questions

Though internal, I presumed that it is safe to use ConcurrentIdentityWeakKeyHashMap generally. However, the
Is it a good idea to use IEEE754 floating point NaN (not-a-number) for values
In a UNIX shell script, what can I use to convert decimal numbers into
Though in general I like case-sensitivity in my database, there are a few times
Though the question is generic, I would mention the scenario which sparked the query.
Though I've programming experience, am completely new to GS, JS, or anything related to
Though it's debatable, I've heard the majority of CSS developers prefer multi-line because of
Though it's on the edge of programming questions, I think this is still relevant
though I have visited this site many times, this is my first question. After
Though the title says everything, I want to let you know the particular scenario

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.