q: abt static-cast function

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>

using namespace 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.
Last edited on
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
Last edited on
If you want to start and end with integer, why do you use float at all? You would get the same precision either way.
> how do I static_cast a variable then static cast that variable back to what its intial type was?
> For example int-->float-->int.

>> If you want to start and end with integer, why do you use float at all?
>> You would get the same precision either way.

This is an interesting exercise.
Those unfamiliar with floating point may be surprised by the results.

For instance:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <iomanip>
#include <limits>

int main()
{
    std::cout << std::fixed << std::setprecision(2) << std::showpos ;

    int i = std::numeric_limits<int>::max() ;
    std::cout << "int: " << i << "   int=>float: " << float(i)
              <<  "   int=>float=>int: " << int( float(i) ) << '\n'
              << "int: " << i << "  int=>double: " << double(i)
              << "  int=>double=>int: "  << int( double(i) ) << "\n\n" ;

    i = std::numeric_limits<int>::min() ;
    std::cout << "int: " << i << "   int=>float: " << float(i)
              <<  "   int=>float=>int: " << int( float(i) ) << '\n'
              << "int: " << i << "  int=>double: " << double(i)
              << "  int=>double=>int: "  << int( double(i) ) << "\n\n" ;

    long long ll = std::numeric_limits<long long>::max() ;
    std::cout << "long long: " << ll << "   long long=>float: " << float(ll)
              <<  "    long long=>float=>long long: " << (long long)( float(ll) ) << '\n'
              << "long long: " << ll << "  long long=>double: " << double(ll)
              << "   long long=>double=>long long: "  << (long long)( double(ll) ) << "\n\n" ;

    ll = std::numeric_limits<long long>::min() ;
    std::cout << "long long: " << ll << "   long long=>float: " << float(ll)
              <<  "    long long=>float=>long long: " << (long long)( float(ll) ) << '\n'
              << "long long: " << ll << "  long long=>double: " << double(ll)
              << "   long long=>double=>long long: "  << (long long)( double(ll) ) << "\n\n" ;
}


With defaulted floating point related compiler options:
Linux stacked-crooked 3.2.0-74-virtual #109-Ubuntu SMP Tue Dec 9 17:04:48 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

clang version 3.6.0 (tags/RELEASE_360/final 235480)

int: +2147483647   int=>float: +2147483648.00   int=>float=>int: +4201984
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: +9223372036854775808.00    long long=>float=>long long: +4201984
long long: +9223372036854775807  long long=>double: +9223372036854775808.00   long long=>double=>long long: +4201984

long long: -9223372036854775808   long long=>float: -9223372036854775808.00    long long=>float=>long long: -9223372036854775808
long long: -9223372036854775808  long long=>double: -9223372036854775808.00   long long=>double=>long long: -9223372036854775808



g++ (GCC) 5.2.0

int: +2147483647   int=>float: +2147483648.00   int=>float=>int: +2147483647
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: +9223372036854775808.00    long long=>float=>long long: +9223372036854775807
long long: +9223372036854775807  long long=>double: +9223372036854775808.00   long long=>double=>long long: +9223372036854775807

long long: -9223372036854775808   long long=>float: -9223372036854775808.00    long long=>float=>long long: -9223372036854775808
long long: -9223372036854775808  long long=>double: -9223372036854775808.00   long long=>double=>long long: -9223372036854775808

http://coliru.stacked-crooked.com/a/cb465a10a5bbaee5

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

http://rextester.com/CGD9093
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 do this:
         float e = static_cast<int>(j)/v;
	  cout<<"e = "<<e<<endl;
	  float e = static_cast<float>(j)/v

error redeclaration

etc.
Last edited on
> error redeclaration

Don't redeclare; reuse the same variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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. 
Topic archived. No new replies allowed.