 [SOLVED]C++ Challenge problem | SoloLearn: Learn to code for FREE!

+16

# [SOLVED]C++ Challenge problem

I am recently facing a problem with a challenge code.....And can't also find out the logic of being that kind of output... Here is the code: https://code.sololearn.com/cYlZxVhMK2o6/?ref=app Here please explain why the output is Bike instead of cello and kindly tell me the reason behind it

+19

Simple! This is called method overloading! Since 5.0 - 5 = 0.0 and not 0 Hence it will call the function that takes float input!

+11

This is explained by the rounding error that occurs for number representation and the precision used. Numbers are stored in memory as a close approximate to the real number it represents. This is due to the nature of the computer being composed of binary values 0 and 1. The difference is very small. 5.0 could actually be 5.000000000001 or something like that. However, if you do repeated calculations with floating point numbers (float data type), you will be able to see a significant difference. 5 on the other hand is a truncated version of the representation for the number 5. So to conclude, 5.0 - 5 != 0 in computer logic. It results in a very very small float number. Hence, v is float and func(float a) is called. Try using greater precision to store and print a number. (e.g. 0.1). Run the following code snippet you'll see: #include <iomanip> // required library to use setprecision() #include <iostream> using namespace std; int main() { double d = 0.1; cout << d << endl; // use default cout precision of 6 cout << setprecision(20); //set precision (size of mantissa) to 17 cout << d << endl; return 0; } Reference: https://www.learncpp.com/cpp-tutorial/floating-point-numbers/

+6

Namit Jain I understood It earlier but float-int=float I knew that today

+5

See it again!🙂 https://code.sololearn.com/cFK5qrhoAh57/?ref=app

+5

AteFish If you now understand and got the solution then just add [SOLVED] before your question or just add that in the tags!✌️ So that people know that they don't have to answer your question! As it is already solved!👦 I don't want intellectuals wasting their time in already solved question!🙂

+4

AteFish The compiler doesnt know that. Also the value of the variable could be different.

+4

https://code.sololearn.com/cFK5qrhoAh57/?ref=app Here see this! In this example we cannot say that type of a and b is int

+4

Namit Jain Yup......Now I am understanding...

+4

Namit Jain Ik how to find out the type name and now I understood

+4

Actually the auto keyword is deducing the ans of expression in float data type that's why it is printing Bike U can do some googling related to auto keyword then u will understand more better🙂

+4

It which is the more generic type which is float rather than int...

+4

There's a concept called operator overloading that takes place when you subtract an int from a float and vice versa. So what happens is the Cpp runtime coerses the int into a float and the resultant of the operation is a float. That's why the function that has a float parameter was called.

+3

AteFish you can try it in the code playground 😕 It will be float

+3

Namit Jain But why?😣😣😣😣

+3

AteFish Do you want it to be like this: 5.3-5 = 0 Or 5.3-5 = 0.3 Which one would you prefer? Of course the second case So why should 5.0 be treated differently?

+3

In C++ you could try that The output will be 0...

+3

Alex Yup.....But still it is in a conclusion

+2

Namit Jain Yup but I said that the output will be 0

+2

AteFish I have never used C++ to that extent! Output would be 0 🙂👍 But you can't say the type of 0 by just seeing at it! C++ has a kind of flaw🙁

+2