AndrewFNAF
AndrewFNAF

Reputation: 11

Largest possible data type

I was writing a C++ code and I found out that the input might be all the way up to 10^25, which as far as I know exceeds unsigned long long.

This raised 2 questions in my mind, and the articles out there aren't quite helpful:

  1. What is the max size of the biggest data type in C++? I know the biggest data type is ull, but many sources contradict each other, saying that it's on 4 bits, but then int is on 32, and that's ... not coherent, since there is also long int, long long, and those have to be stored on some power of 2 number of bits.
  2. How can one work with numbers above that "limit"? Can you define some class that stores huge values by splitting them into pieces...but what's a way to even read that? You have to save into something, and reading character by character doesn't go too well.

*Thank you in advance for your help! I wish you all a fantastic day!*

Upvotes: 1

Views: 2385

Answers (5)

Deumaudit
Deumaudit

Reputation: 1016

About your questions

  1. The maximum size is determined by your RAM (or even more if you choose to load dynamically from hard drive)
  2. Well, you can read your number character by character, and insert each character into the resulting class separately

And BTW, if you are using g++ compiler and want to store 10**25, you may try to use __int128(which is compiler extension, not part of the standart)

Upvotes: 1

IWonderWhatThisAPIDoes
IWonderWhatThisAPIDoes

Reputation: 1048

  1. Sizes of types are not consistent. Well, most types. There are actually several fixed-size integers, the largest of which is int64_t or uint64_t, which, as you may have guessed, is 64 bits. They may not always be present though.

  2. That's up to you. There are already libraries that can handle this for you if that's what you want. Yes, splitting them up is the obvious way. You could represent them as strings and perform calculations the way you were taught in third grade math (with overflow digits and stuff). A more concise (and memory-efficient) way would be to split the numbers into something smaller, like uint32_t. Performing operations in the larger type (uint64_t) would take care of the overflow bits.

.

struct myuint96 {
    uint32_t data[3];
};

myuint96 operator+(const myuint96& left, const myuint96& right) {
    myuint96 result;
    uint64_t sum = (uint64_t)left.data[0] + right.data[0];
    result.data[0] = sum & UINT32_MAX;
    // This would be done in a for loop for bigger types
    sum = (sum & ~(uint64_t)UINT32_MAX) >> 32 + left.data[1] + right.data[1];
    result.data[1] = sum & UINT32_MAX;
    sum = (sum & ~(uint64_t)UINT32_MAX) >> 32 + left.data[2] + right.data[2];
    result.data[2] = sum & UINT32_MAX;
    // Here we don't care about overflow bits, or you can throw an exception
    return result;
}

Upvotes: 1

Lukas-T
Lukas-T

Reputation: 11340

I'll start with the latter part of your question, because I feel it's easier to answer:

How can one work with numbers above that "limit"?

Use (or write) a library. You've got the basic idea right: split a huge number in different parts. One libray I've worked with is libgmp but there are many others.


The first part of your question is bit more tricky in my opinion.

What is the max size of the biggest data type in C++?

It depends on some factors what the "biggest" data type is. It's different on different platforms and it depends on wether you focus on integral types only or take floating point numbers into account. Because of your latter statements I assume you are talking about integral types only.

In general: You can use sizeof(some_type) to get the size in bytes of a type or std::numeric_limits<some_type>::max() to check the largest value a given type can represent (reference).

I know the biggest data type is ull, but many sources contradict each other, saying that it's on 4 bits

Probably that source mentioned 4 bytes (= 32 bits) rather than 4 bits.

but then int is on 32,

On all platforms I've worked with that's true, but the standard only requires int to be at least 16 bits wide (see this reference).

and that's ... not coherent, since there is also long int, long long

long and long int are the same. Also long long is the same as long long int and, yes, they are allowed to be inconsistent or redundant (probably a suboptimal wording though).

long must be at least 32 bits wide and long long at least 64. (reference as above)

and those have to be stored on some power of 2 number of bits.

That's true from a technical perspective, for basically all modern platforms. I think the standard would also work for systems that are not based on powers of 2, for example where a byte has 7 or 9 bits (That's way out of my league though). I'm not sure if, why or where, you think this would not be the case here, maybe this problem was already covered in a previous point.

Upvotes: 0

user4290866
user4290866

Reputation:

  1. There is no straight forward answer to your question. The build in types may (with few exceptions) differ in size see depending on compiler and architecture (check cppreference.com for more information). The contradiction you mentioned may come from a discussion of typical values. Maybe they said 4bytes and 32bits, which would be the same thing (but this is me speculating now and unimportant to answering your question).
  2. There are however libraries which support "arbitrary" sizes like the GMP. Which library is the best to use is strongly problem dependent.

Upvotes: 1

Jarod42
Jarod42

Reputation: 217275

  • What is the max size of the biggest data type in C++?

    From https://en.cppreference.com/w/cpp/types/size_t:

    std::size_t can store the maximum size of a theoretically possible object of any type (including array). A type whose size cannot be represented by std::size_t is ill-formed (since C++14).

    So max size of the biggest data type in C++ should be less than max value of std::size_t.

    standard recommends a minimum of 262144 for the limit.

    You might see from https://github.com/fritzone/cpp-stresstest#size-of-an-object---sizeofanobject

    current limits from some compilers:

    gcc     clang   msvc    intel  
    2097152 2097152 524288  2097152
    
  • How can one work with numbers above that "limit"?

There are libraries for big-number, there are basically array of integer types.

Upvotes: -1

Related Questions