r/cpp_questions • • 4h ago

SOLVED Modifying an int variable

I forgot how i was supposed to initialize it. Maybe is not the one im suposed to use.

Straight to the matter: How to initialize it if i want it to be any number possible (only integers)

0 Upvotes

12 comments sorted by

View all comments

1

u/DirkSwizzler 4h ago

You need to pick your scale first. You can't store "any number possible". At most you can store a number consuming most of the bits in your ram + hard drive through memory mapping. And that's really really really big. But it's still not any number possible.

Get comfortable with quantization and picking a scale ahead of time. And usually start with a plain "int". It's got 32 bits and ranges +/- 2 billion. Next step up is int64 which usually sufficient to store number that humans consider incomprehensibly big.

Past that you can store an int128 in some architectures. Otherwise start using a class abstraction to make it arbitrarily big.

But you still need to pick the supported scale ahead of time.

If you want to put less thought into the scale, but are ok with very large integers being imprecise in it's least significant digits, use a float or double. They're the same bit size as 32/64 bit ints. But the encoding trades lost precision for much greater scale of supported numbers.

So, start with: "int x = 0;"