I have an integer variable, that can get a value larger than 4294967295.
What type should I use for it (long long, or double, or something else)?
I have an integer variable, that can get a value larger than 4294967295.
What type should I use for it (long long, or double, or something else)?
Doubles are floating-point. You should use long long, probably. I don't know which alias is the preferred one.
Use long long
and if possible add a compile-time assertion that this type is wide enough (smth like sizeof( long long ) >= 8
).
double
is for floating-point, not integer.
if you don't need negative numbers, unsigned long long sounds like most you can get.
There is no portable way of doing this in C++, as the language does not specify the size of integer types (except sizeof char is 1). You need to consult your compiler documentation.
Try:
http://gmplib.org/ big num.
http://mattmccutchen.net/bigint/ big int.
I've used neither, but I've used similiar things in Java.
If your compiler does not have long long you can implement them yourself with a structure containing two long but you will need to be caurseful with carry etc. You could of course look for a Multiple Precision Arithmetic like GMP
Both proposals aren't good because long long is not a standard C++ data type, and double is a floating-point.
Since my program has to be portable, I am going to #define my own types, that suit all the compilers that I use (visual studio and gcc) :
#ifdef WIN32
#define unsigned_long_long unsigned __int64
#define long_long __int64
#else // gcc. Might not work on other compilers!
#define unsigned_long_long unsigned long long
#define long_long long long
#endif
Try TTMath. All you need to do is include a single header and then declare a bignum type such as:
typedef ttmath::UInt<100> BigInt;
which creates a type that can hold unsigned integers between 0 and 2 ^ (32*100)-1.
Then just use BigInt
wherever you would use int
.
Of course you can choose whatever size you like for the template parameter. 100 might be overkill ;-)
Just realised, the lib only works on x86 and x64, but is OS cross-platform on those processors.
Don't use double, because:
cout.setf(ios::fixed);
cout << LONG_LONG_MAX << endl;
cout << double(LONG_LONG_MAX) << endl;
cout << LONG_LONG_MAX-100 << endl;
cout << double(LONG_LONG_MAX-100) << endl;
Output:
9223372036854775807
9223372036854775808.000000
9223372036854775707
9223372036854775808.000000
A lot of current C/C++ compilers have either stdint.h or inttypes.h header.
int_fast64_t
or int64_t
may be an option (IMHO the most portable).
I'm going to assume your numbers will fit in 64 bits. If not, then you need an arbitrary-precision arithmetic library such as GMP.
In theory, there's no easy, portable way to do 64-bit maths in C++. In practise, most C++ compilers also support the "old fashioned" C headers, and C99 has a nice header called stdint.h.
So first do:
#incude <stdint.h>
Then use types int64_t
(signed) and uint64_t
(unsigned).
How portable should your program be? TR1 has cstdint and stdint.h so it's likely supported by most up-to-date compilers. Then there is Boost cstdint.hpp that you should be able to use if cstdint is not supported.