Does someone know why this code fit with double but return errors with float? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Does someone know why this code fit with double but return errors with float?

https://code.sololearn.com/clt1JFvt89bW/?ref=app

14th Jun 2021, 7:28 AM
Nur Anisa Hidayatul Masruroh
Nur Anisa Hidayatul Masruroh - avatar
2 Answers
+ 3
because without specifying numeric literal operator, literals with a dot default to double... so compiler doesn't know if it may cast value to float or to int (ambiguous) ^^ with float, you must specify your literal float: add(1.2f, 6.5f);
14th Jun 2021, 7:43 AM
visph
visph - avatar
+ 2
Because for whatever reason cpp see's those floats as doubles regardless of how you define the function. Look here, i changed the function to float, then forced the two float numbers to be read as a float by postfixing an 'f' on the end of the number for 'float' https://code.sololearn.com/cLfJKBLh3FQ2/?ref=app
14th Jun 2021, 7:43 AM
Slick
Slick - avatar