is the following allowed in C ? ( root over a^2 + b^2 ) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

is the following allowed in C ? ( root over a^2 + b^2 )

printf("Sum : %d", pow((pow(entity1, 2) + pow(entity2, 2)), 1 / 2)); everytime i run this, it prints Sum : 0

17th May 2022, 6:36 PM
blueshipswims
blueshipswims - avatar
4 Answers
+ 2
1/2 result 0 in c. So pow(n, 0) is 1.0000 a double value.. If you are trying squire root then use sqrt() function.. what are entity1, entity2 values? it should result always 1, not 0 pow() returns a double type value and converting %d truncates precision, or garbage value.. edit: hmmm printf("Sum : %lf", sqrt(pow(entity1, 2) + pow(entity2, 2))); Hope it helps..
17th May 2022, 7:33 PM
Jayakrishna 🇮🇳
+ 2
Or you can write pow(n, 1.0/2) or pow(n, 0.5)
17th May 2022, 9:58 PM
YUGRAJ
+ 2
If you are getting 0, then something else is terribly wrong in the program. I cannot say what it is without seeing the rest of the program. At least part of the problem is that the format specifier is incorrect. It should be %f, not %d. Another problem was mentioned already, regarding integer math versus floating point math. There is a better function than either pow or sqrt that performs the exact calculation you want. It is the hypot function. printf("Sum : %f", hypot(entity1, entity2));
17th May 2022, 11:58 PM
Brian
Brian - avatar
+ 2
Jayakrishna🇮🇳 Brian YUGRAJ thank you guys...i understood the things. i know now why i was getting zero, and ty for letting me know about the new functions.... the hypotenuse is easier to use.
19th May 2022, 8:19 AM
blueshipswims
blueshipswims - avatar