Does typing even work in cpp
Why is 1.0 + 2.01 = 3, it doesn't work this way on my calculator. Why does the function allow me to add an argument of type "float" when the parameter says "int" The function is expected to return a float, why does it let me returned the result to an integer variable in the main function. All these errors are made deliberately, expecting the code to log an error in the console but then it works https://code.sololearn.com/cDUEL49fjFBf/?ref=app
8/7/2020 4:52:01 PMMirielle[ InAcTiVe ]
14 AnswersNew Answer
float is convertible to int and if the compiler is properly set up it should show warning like "loss of precision or similar" The function is returning float but the decimal point is not shown in the default format if it can be shown without using the precision of 6 digits(total digits counting digits before and after decimal point) to show you can use cout << showpoint << floating point no. it will show something like 3.00000 note - this will work if the number is indeed floating point number cout << showpoint << 54/7; output = 7 since 54/7 is integer division
If you want to prevent C++ from choosing the next best thing (float(int, int) for a (double, double) call), you can explicitly disallow it by adding this line: float add(double a, double b) = delete;
Q: Why is 1.0 + 2.01 = 3, it doesn't work this way on my calculator. A: Because you set type for parameter <a> and <b> to be int. 1.0 and 2.01 are implicitly converted to int 1 and 2 respectively because of this. Q: Why does the function allow me to add an argument of type "float" when the parameter says "int" A: It is int + int done here, not float + float, it is only after the integers are summed (with result being an int - 3), the result is then be converted to float before it is returned. This is partly because + operator has higher precedence over = operator. Q: The function is expected to return a float, why does it let me returned the result to an integer variable in the main function. A: I see <num> as a float in main, where is integer variable storing the returned value in main?
Mirielle I have mention in the comment that if the value can be shown without using 6 digit precision then it will not be shown. And to see it you need to tell cout to show the point. And in return type float is not really getting converted to int. The decimal point is not shown.
You set your parameters to integers and even to the function return type is float the arithmetic performed is int + int so its basically 2 + 1
Bartas Dausynas There is no float assigned to int. 1.0 and 2.01 are double by default which are narrowed to int. The integer addition is promoted to float when returned from function. Just take it as a mistake of using the term float instead of double.
~ swim ~ to be precise, float isn't converted to int in this program anywhere... i pretty sure, if you search for function, (i don't rrmember now the real definition) like typeid(2.0).name() it will show type double, cause it's not 2.0f. But yeah, what is happening in this program, function is taking integers, right? So it silently casting doubles to an integer, adding them, and returning them to a caller. The caller is function so, that function now is assigned to a float variable, and now number 3 is upcasting to float. But since int 3 in float is still 3.0f it is outputed as 3...
~ swim ~ with that return type, I was expecting something like "3.0" if at all float get converted to int. Idk how this works in cpp
Any float value assigned to an int will result in lack of precision. You should use a float to store values. But your editor should complain about possible loss of data
Guys, really, where you see float assigned to int there?
~ swim ~ hell yeah! That's what I'm talking about! High five! 😁👍
Because the inputs are type cast to int.....
"Does typing even work in cpp"...rediculous. C++ is reasonably strongly typed, see https://en.cppreference.com/w/cpp/language/implicit_conversion and https://en.cppreference.com/w/cpp/language/explicit_cast