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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:19:58+00:00 2026-05-12T08:19:58+00:00

For a simple project I have to make large numbers (e.g. 4294967123) readable, so

  • 0

For a simple project I have to make large numbers (e.g. 4294967123) readable, so I’m writing only the first digits with a prefix (4294967123 -> 4.29G, 12345 -> 12.34K etc.)

The code (simplified) looks like this:

const char* postfixes=" KMGT";
char postfix(unsigned int x)
{
     return postfixes[(int) floor(log10(x))];
}

It works, but I think that there’s a more elegant/better solution than computing the full precision logarithm, rounding it and casting it down to an int again.

Other solutions I thought of:

int i=0;
for(; x >= 1000 ; ++i) x/=1000;
return postfixes[i];

(This is significantly slower, but easier to read)

The numbers are distributed between according to Benford’s Law and the number should be treated as unsigned 64 bit-number, as there should be no rounding error near 10^x (e.g. in python math.log(1000,10) returns 2.999996, which gets floored to 2).
Is there any fast, accurate other way I’m missing?

  • 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-12T08:19:58+00:00Added an answer on May 12, 2026 at 8:19 am

    Your log10/floor code is perfectly readable, and its performance cost will likely be dwarfed by that of the string formatting you will subsequently be doing on your output.

    However, suppose you were to really need the performance…

    Note that log10(x) == log2(x) / log2(10) == log2(x) * 1/log2(10)

    1/log2(10) is a constant

    log2(x) can usually be performed cheaply in the integer pipeline on modern architectures using instructions such as CLZ or a bit twiddling hack, yielding a number between 0 and 63 for a 64-bit integer. That fits in 6 bits, leaving us up to 58 bits after the radix point usable for fixed point arithmetic in a 64-bit type.

    So we can then use fixed-point arithmetic to find the log10:

    unsigned long long integer_log10( unsigned long long _in )
    {
        unsigned long long log10fp6x58 = 0x134413509f79ff0llu; // (unsigned long long) (double(1llu<<58) / log2(10.0))
        return (((integer_log2(_in)) * log10fp6x58)+(1llu<<57)) >> 58;
    }
    

    The implementation of integer_log2 is compiler/platform-dependent; e.g. on GCC/PowerPC, it’s

    unsigned long long integer_log2( unsigned long long _in )
    {
        return 63 - __cntlzd(_in);
    }
    

    This approach can be generalised for finding the logarithm of any base, simply calculate the appropriate constant as described above.

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

Sidebar

Ask A Question

Stats

  • Questions 163k
  • Answers 163k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Looking at the code snippet you paste, I would assume… May 12, 2026 at 12:08 pm
  • Editorial Team
    Editorial Team added an answer The "all" name is misleading. If you want a page… May 12, 2026 at 12:08 pm
  • Editorial Team
    Editorial Team added an answer Generically, you could try something like: /** * Count number… May 12, 2026 at 12:08 pm

Related Questions

Assuming a green field project, what choice of technologies, libraries, middleware, etc. would make
I am a fairly new MySQL developer and am starting on a project that
I am trying to understand if I really have any case for using git/mercurial.
I have a project here that connects to an in-production database and grabs tables
I have been tasked with implementing a PKI library in C# for a company

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.