Is it possible to use long double as an integer? (adding, substracting, multiplying, dividing, perbit operations ...)
As far as I know in C++ standart sizeof(l开发者_开发技巧ong double)
is 10 byte = 80 bit => the variable of this long double
type can carry 2^80 different values. So my question is here, is it possible to write some wrapper or I don't know what (using, maybe, perbit operations). To use long double
(or it wrapper) as full integer? And if there is possible, then why standart doesn't give as 80bit integer?
The C++ standard does not specify the exact size of long double
. All it says is that
the type long double provides at least as much precision as double.
On my computer, a long double
is actually 16 bytes (128-bit) in size.
That said, you can create an arbitrarily large integer type using arrays, however usually you will need a special library for this because arithmetic operations on such arrays are non-trivial to implement efficiently. If you need to work with larger integers, look into an arbitrary precision arithmetic library, such as GMP.
You may better wrap array of char than double. This would give the flexibility of any integer size.
If you are looking for arbitrary precision numbers you may look at MAPM.
Why standart doesn't give as 80bit integer?
Standard does not say anything about integer sizes. Integer may be 80 bit if the compiler creator wants it to be so.
精彩评论