Tobias Langner
Tobias Langner

Reputation: 10808

Is there a floating point data type that converts without loss from int64?

is there a floating point data type in Visual C++ that has enough precision in the mantissa to hold an INT64?

Example it should be possible to do the following:

__int64 from = 0xFFFFFFFFFFFFFFFF;
mightyFP intermediate;
__int64 to;
intermediate = from;
to = intermediate;
assert(from == to);

where mightyFP is the unknown type that I search.

regards, Tobias

Upvotes: 6

Views: 972

Answers (3)

Shelwien
Shelwien

Reputation: 2200

See
http://en.wikipedia.org/wiki/Long_double
http://en.wikipedia.org/wiki/Extended_precision

In short, there's a 10-byte "extended precision" type supported by all x86 cpus (actually FPUs, later CPUs always use it for internal representations), which is commonly referenced as "long double". But because of no support for this type in SSE, modern compilers made it hard to access - it may be turned into an alias for "double" depending on compiler options (like /Qlong-double, /Qpc80 in IntelC) and even target cpu.

Still, even if its really impossible to enable it in modern VS, writing a simple wrapper class with one-line asm implementations may be still an option.

Upvotes: 2

Erik
Erik

Reputation: 91270

Short answer, no. long double and double have the same representation in visual studio. For x86 the size of both is 64 bits, which isn't enough to hold the full range of a 64-bit integer.

You need GMP

Upvotes: 2

michel-slm
michel-slm

Reputation: 9756

There is quadruple-precision floating point (long double) but I'm not sure it's precise enough. You might want to use an arbitrary-precision library such as GMP or MPFR

Upvotes: 1

Related Questions