Below code - simply experimenting with it to learn more abt static cast functions. THe purpose of the code was to show type conversion. What I don't understand is why in static_cast<float>(j)/k; fx you only need one integer from the two integers(j,k) to make a conversion to float to work. Plz see quotation below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<iostream>
usingnamespace std;
int main()
{
int j(10); int k(6);
float m = j/k;
float d = static_cast<float>(j)/k;
cout<<"m = "<<m<<endl;
cout<<"d = "<<d<<endl;
return 0;
}
In this example, m = j/v; produces an answer of type int because both j and v are integers. Conversely, d = static_cast<float>(j)/v; produces an answer of type float. The static_cast operator converts variable j to a type float. This allows the compiler to generate a division with an answer of type float. All static_cast operators resolve at compile time and do not remove any const or volatile modifiers.
Thanks that got most of the problem. Remaining question : how do I static_cast a variable then static cast that variable back to what its intial type was? For example int-->float-->int. The ide forces me to use a new variable(s):
1 2 3 4 5 6 7 8 9 10 11
int j = 41;
int v = 4;
float m = j/v;
float d = static_cast<float>(j)/v;
cout << "m = " << m << endl;
cout << "d = " << d << endl;
float e = static_cast<int>(j)/v;
cout<<"e = "<<e<<endl;
float x = static_cast<float>(j)/v;
cout<<"x = "<<x<<endl;
so I want to change float d back to integer instead of using a new variable like x or e .. 'll start losing track of thing otherwise
Microsoft Windows [Version 6.2.9200]
msc++: 1800
int: +2147483647 int=>float: +2147483648.00 int=>float=>int: -2147483648
int: +2147483647 int=>double: +2147483647.00 int=>double=>int: +2147483647
int: -2147483648 int=>float: -2147483648.00 int=>float=>int: -2147483648
int: -2147483648 int=>double: -2147483648.00 int=>double=>int: -2147483648
long long: +9223372036854775807 long long=>float: +9223372036854775800.00 long long=>float=>long long: -9223372036854775808
long long: +9223372036854775807 long long=>double: +9223372036854775800.00 long long=>double=>long long: -9223372036854775808
long long: -9223372036854775808 long long=>float: -9223372036854775800.00 long long=>float=>long long: -9223372036854775808
long long: -9223372036854775808 long long=>double: -9223372036854775800.00 long long=>double=>long long: -9223372036854775808
Hi LB & JLBorges, yes I agree its not a practical exercise.
Why not go:
float e = static_cast<int>(j)/v;
cout<<"e = "<<e<<endl;
float e = static_cast<float>(j)/v;
But it is a good thought experiment for me to learn more abt this. My desire to use an int-->float-->int again, but using the same assignment variable. I want to recycle the same assigment var going from int to float to int., if its possible(e.g.int-float below)
1 2 3 4 5 6 7 8 9 10 11
int j = 41;
int v = 4;
float e = static_cast<int>(j)/v;
cout<<"e = "<<e<<endl;
float x = static_cast<float>(j)/v;
if I dothis:
float e = static_cast<int>(j)/v;
cout<<"e = "<<e<<endl;
float e = static_cast<float>(j)/v
int v = 41 ; // declare and initialise v
std::cout << v << '\n' ;
float f = static_cast<float>(v) ; // declare and initialise f
std::cout << f << '\n' ;
v = static_cast<int>(f) ; // assign to v
std::cout << v << '\n' ;
f = static_cast<float>(v) ; // assign to f
std::cout << f << '\n' ;
v = static_cast<int>(f) ; // assign to v
std::cout << v << '\n' ;
f = static_cast<float>(v) ; // assign to f
std::cout << f << '\n' ;
// etc.