Data type conversion int to double within function

I'm in my first programming class and am having a little trouble understanding what I should do. I extracted the trouble shooting problem from my actual assignment, but it still does not make sense to me/ I don't know how to get around it.



#include <iostream>
using namespace std;

int main()
{
int a = 5;
int b = 2;
double test;
test = a / b;
cout << part << endl;
}
The output is 2.

I need to make the variable test, compute and store the numbers in a and b. However it appears that although test is a floating-point type, the variables inside the function are integers, and therefore the outcome is an integer, but the variable test stores the integer value as a floating-point.

My problem: I don't know how to ave integer variables within a function that have to be int type, and have the outcome be a floating-point type. (My professor specified we are not permitted to use any type but int for a and b.
Thank you
test = a / b;

Should be :
test = static_cast<double>(a) / (double)(1.0 * b);


test = (double)a / (double)b ;
It works!

Thank you
The (double)a is a C-style cast. An instrument so blunt that the C++ introduced a new cast syntax:
static_cast<double>(a)
The static_cast is not the only one; there are others too. All more specific than the C-style bludgeon.

Lets assume that there are two division operators:
1
2
int    operator/ (int, int);       // #1
double operator/ (double, double); // #2 

Both a and b are int, so it is clear that the compiler uses #1 for a / b.
If you do cast both a and b to double, then the use of #2 is obvious.

What if only one side is double, as in static_cast<double>(a) / b?

The compiler has two options:
1. Implicitly convert b to double and then call #2
2. Implicitly convert a to int and then call #1

Option 1 wins. All these call the #2:
1
2
3
static_cast<double>(a) / b
a / static_cast<double>(b)
static_cast<double>(a) / static_cast<double>(b)



SakurasouBusters' (double)(1.0 * b) is "tårta på tårta". The multiplication has two operands, a double (1.0) and int (b). Therefore, b converts implicitly to double, the multiplication operates on doubles and the result is a double. An explicit C-style cast to double does nothing to a double value.
Thank you so much @keskiverto!

It makes a lot more sense now that you clarified the topic. One question, I understand programmers tend to use shorthands as they are faster, so why would anyone use static_cast<double> instead of (double)?
Because it is C++ language.
(double) belongs to C language. One ought to use excessively C++ features, and avoid C code style as much as possible.

Also, static_cast<double> is more readable than plain (double).
Lets say that you have thousand files in your program code and million lines of code. Now you want to find every place where you do some explicit cast.

There are tools to find text or regular expressions (e.g. findstr, grep) from multiple files, but which is easier to search for:
1. "_cast"
2. "(" all possible typenames ")" something

Oh, wait this is valid too: double(a)

Read about static_cast, dynamic_cast, const_cast, ...
you will see that they do specific operations.


Yes, there is a balance between short and more expressive. Remember that someone may have to read, understand, and update the code later. That someone might be you.
Topic archived. No new replies allowed.