Well I was trying to compile following code (just a snippet) in Java:
class MyClass{
public float get100(){
return 100.05; // returning 100.05f should work
}
}
But as you can see 100.5 being a double in nature but get100() is returning float by its declaration, javac gives error as: can't convert double to float.
Which is totally understandable. But Here is what i noticed about c++
I tried following code (Almost similar to the one mentioned above):
#include<iostream>
using namespace std;
class MyClass{
public:
float get100(){
return 100.05;
}
};
int main(){
MyClass m;
cout<<m.get100()<<endl;
return 0;
}
and this code is running fine in c++..
Can anyone tell me why is it so? or does it mean c++ compiler is smart enough that it can auto convert double to float?
But as double is of higher range than float, how is that working in c++?
Any help/Suggestion is appreciated. Thanks in advance.
Yes, the C++ compiler is smart enough that it can auto convert double to float. The Java compiler could do the same thing, but the designers of Java decided that it's better to not allow it, in case it is a mistake by the programmer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With