Is that code correct? Please don't answer anymore. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Is that code correct? Please don't answer anymore.

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

5th Aug 2020, 7:02 AM
TeaserCode
17 Answers
+ 4
yes its correct... try inputting 2 3 8 you'll get output as 1 (2^3 = 8) (1.5)^(2.5) =2.75567596063(as per my calculator) So your code is returning 0 because double has no fixed representation like integers do.
5th Aug 2020, 7:15 AM
Rohit
+ 4
Yes the code is correct !
7th Aug 2020, 4:23 AM
Sakshi💕
Sakshi💕 - avatar
+ 2
Yes the code is correct
6th Aug 2020, 6:12 AM
Aman kumar
Aman kumar - avatar
+ 1
The code is syntactically correct but won't work as expected. double and float numbers can't be compared with == because they have no fixed representation like integers. Instead they contain small differences from rounding the result. For example 3.0 might actually be stored as 2.9999998 because the binary format can't directly store decimal numbers. Therefore using == on doubles or floats will almost always return false. P. S. in the bool function you could return true and false instead of 1 and 0. But that's just for better readability.
5th Aug 2020, 7:16 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 1
But what about you enter double variables because there are double parameters.
5th Aug 2020, 7:17 AM
TeaserCode
+ 1
Then how to make correct it?
5th Aug 2020, 7:22 AM
TeaserCode
+ 1
So one way to make it work is by rounding the decimals to a fixed places like so... (that rounding function is taken from geeksforgeeks. com) https://code.sololearn.com/cdTOO7Gv1Giu/?ref=app
5th Aug 2020, 7:29 AM
Rohit
+ 1
// 37.66666 * 100 =3766.66     // 3766.66 + .5 =3767.16    for rounding off value     // then type cast to int so value is 3767     // then divided by 100 so the value converted into 37.67
5th Aug 2020, 7:30 AM
Rohit
+ 1
Ok, I understand you can introduce addition function to make it simplier.
5th Aug 2020, 7:31 AM
TeaserCode
+ 1
yeah...kind of.
5th Aug 2020, 7:32 AM
Rohit
+ 1
for example FLOOR function.
5th Aug 2020, 7:33 AM
TeaserCode
+ 1
yeah, give it a try and share here.
5th Aug 2020, 7:33 AM
Rohit
+ 1
https://code.sololearn.com/cW1Myc20xzCs/?ref=app With your help I repair it, so I use floor function on both side of == operatant. If I use floor only on left side it won't be right. I am satisfied because it makes it. One more lesson I learn.
5th Aug 2020, 8:46 AM
TeaserCode
+ 1
Yes it's correct
6th Aug 2020, 11:04 PM
Kasey
+ 1
Try 2 3 8....It works 😎
7th Aug 2020, 4:52 AM
Sanjay Kamath
Sanjay Kamath - avatar
0
Not for all types of number.
6th Aug 2020, 6:21 PM
Learner
Learner - avatar
0
Yes
7th Aug 2020, 6:05 AM
Harshit Singh
Harshit Singh - avatar