int64_t a = 1234;
double d = (double) a;
Is this the recommended way?
According to Wikipedia, you can store 15.95 decimal digits in a 64-bit IEEE 754 floating-point number. double has typically up to 53 bits precision, compared to 64.
A long on some systems is 32 bits (same as an integer), the int64_t is defined as a 64 bit integer on all systems (otherwise known as a long long). Portability may be affected using long, but using int64_t looks like it was created to increase portability.
use static_cast
as strager answers. I recommend against using the implicit cast (or even a C-style cast in C++ source code) for a few reasons:
static_cast
makes your intent immediately obvious.static_cast
and the other C++-style casts are easy for grep
to handle.You should use static_cast
or rely on the implicit cast instead:
int64_t a = 1234;
double d = static_cast<double>(a);
double f = a;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With