-4

I'm still a beginner in C++ so I seek some help with the basics. Here, in the following code, I'm using type-casting to find value of 122/65 but I'm getting only the integer part even with double data type.

#include <iostream>

using namespace std;

int main()

{

    double a=(double)('z'/'A');

    cout<<a;

    return 0;

}

Can someone provide me a good reason for this??

Thank you.

KeLiuyue
  • 8,149
  • 4
  • 25
  • 42

1 Answers1

5

You make an integer division and then you typecast the result to double. Basically you have:

(double) (122/65) = (double) (1) = 1.0
                              ^ truncated -> integer division

If you want a floating point division you can do it this way:

double a = (double)'z' / (double)'A';
//     a = 122.0       / 65.0
izlin
  • 2,129
  • 24
  • 30