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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:26:16+00:00 2026-05-28T01:26:16+00:00

On my platform, unsigned long long is 64 bits (8 bytes). Suppose I have

  • 0

On my platform, unsigned long long is 64 bits (8 bytes). Suppose I have two such variables:

unsigned long long partialSize;
unsigned long long totalSize;
//somehow determine partialSize and totalSize

How can I reliably determine how many percentages (rounded to a nearby integer) partialSize is of totalSize? (If possible, it would be nice if I wouldn’t have to assume that the former is less than the latter, but if I really have to make this assumption, it’s fine. But we can, of course, assume that both are non-negative.)

For example, is the following code completely bulletproof? My fear is that it contains some kind of rounding, casting, or conversion errors that could cause the ratio to go out of whack under some conditions.

unsigned long long ratioPercentage
    = (unsigned long long)( ((double)partialSize)/((double)totalSize) * 100.0 );
  • 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-28T01:26:17+00:00Added an answer on May 28, 2026 at 1:26 am

    It’s not completely bullet-proof. double mantissae are only 53 bits (52 + 1 implicit), so if your numbers are larger than 2^53, the conversion to double will in general introduce rounding errors. However, the rounding errors are very small in relation to the numbers itself, so a percentage calculation resulting in an integer value will introduce more inaccuracy than the conversion.

    A possibly more serious concern is that this will always round downwards, e.g. for totalSize = 1000 and partialSize = 99, it will return 9 rather than the closer value 10. You can get better rounding by adding 0.5 before casting to unsigned long long.

    You can get exact results using only integer arithmetic (if the final result doesn’t overflow), it’s fairly easy if partialSize is not too large:

    if (partialSize <= ULLONG_MAX / 100) {
        unsigned long long a = partialSize * 100ULL;
        unsigned long long q = a / totalSize, r = a % totalSize;
        if (r == 0) return q;
        unsigned long long b = totalSize / r;
        switch(b) {
            case 1: return q+1;
            case 2: return totalSize % r ? q : q+1; // round half up
            default: return q;
        }
    }
    

    Easy modifications if you want floor, ceiling or round-half-to-even.

    It’s okay if totalSize >= 100 and ULLONG_MAX / 100 >= partialSize % totalSize,

    unsigned long long q0 = partialSize / totalSize;
    unsigned long long r = partialSize % totalSize;
    return 100*q0 + theAbove(r);
    

    It gets more fiddly in the other cases, I’m not keen on doing it, but I could be persuaded if you need it.

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

Sidebar

Related Questions

In Visual C++ a DWORD is just an unsigned long that is machine, platform,
I have a function that takes a unsigned long* and needs to pass it
I have a cross platform program that runs on Windows, Linux and Macintosh. My
I have the following code, for an embedded platform where an int is 16
On my platform this prints 9223372036854775808. double x = 1e19; std::cout << static_cast<unsigned __int64>(x)
Given an integer typedef: typedef unsigned int TYPE; or typedef unsigned long TYPE; I
I have a lot of code that performs bitwise operations on unsigned integers. I
Is there a cross-platform way (in C/C++) to make from a unsigned char an
I'm on a 64bit platform, so all memory adrs are 8 bytes. So to
Where can I go to get information about the size of, say, unsigned int

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.