+ 1

I don't understand thís program, what is double(x) mean? Why double(x) =50? Please explain to me if you understand the program.

#include <iostream> using namespace std; int main() { int x; x=50.89; cout << double(x) << endl; return 0; }

23rd Mar 2022, 6:14 AM
VDng150101
VDng150101 - avatar
5 Antworten
+ 4
It's one of the methods in C++ for explicit type conversion, it's working exactly like the C-style type casting. Look at #2 here ... https://en.cppreference.com/w/cpp/language/explicit_cast When you assign a floating point literal e.g. 50.89 to an integer variable, the fractal value e.g. 0.89 is ignored (discarded), and only 50 will be assigned to the integer variable.
23rd Mar 2022, 8:12 AM
Ipang
+ 3
G'day VDng150101 the variable x is created as an int (short for integer, meaning whole number) Then it is assigned a number with a decimal point, we can call that a floating decimal (because the location of the point can float around to give more or less digits before/after according to the value we give it). C++ calls that type of variable a float. {edit: this isn't correct, but is kinda close to ok enough for now} Another way to deal with floating point numbers is to store the two sides of the decimal point in two memory locations. This type of variable is called double. By telling cout the type of variable it is getting, it handles it differently, your code is casting x as a double, cout now knows that it will be printing a double type variable.
23rd Mar 2022, 6:48 AM
HungryTradie
HungryTradie - avatar
+ 2
I found this: What’s the difference ? double has 2x more precision then float. float is a 32 bit IEEE 754 single precision Floating Point Number1 bit for the sign, (8 bits for the exponent, and 23* for the value), i.e. float has 7 decimal digits of precision. double is a 64 bit IEEE 754 double precision Floating Point Number (1 bit for the sign, 11 bits for the exponent, and 52* bits for the value), i.e. double has 15 decimal digits of precision.
23rd Mar 2022, 6:48 AM
HungryTradie
HungryTradie - avatar
- 1
Hh
24th Mar 2022, 10:01 AM
Wã Şş Îm