operator double();

Input:
1
2
3
Rational A(3,2);

double x = A;


Expected Output: "1.5";
Resultant Output: "1";

//operator double();
1
2
3
4
5
6
7
8
Rational::operator double(){
		//Inside the rational double operator
		double x = static_cast<double>(numerator / denominator);

		std::cout << numerator << "/" << denominator << " is equivalent to " << x << std::endl;

		return x;
	}


I'm not seeing why my result isn't 1.5. I've verified that numerator and denominator are 3 and 2 respectively by running my complier in debugger mode.

Thanks for taking a look.
I'm guessing numerator and denominator are both ints? If so:

numerator / denominator does integer division before you cast it to a double.
Thanks!

Problem solved!

1
2
3
4
5
6
7
8
9
Rational::operator double(){
		//Inside the rational double operator
		
		double x = (static_cast<double>(numerator) / static_cast<double>(denominator));

		std::cout << numerator << "/" << denominator << " is equivalent to " << x << std::endl;

		return x;
	}
Topic archived. No new replies allowed.