0

Undefined reference to sqrt()

I include the <math.h> but the code still not recognising basic operations such as sqrt or pow. https://code.sololearn.com/cEBuyGQdCrHL/?ref=app

25th Oct 2019, 9:47 PM
Guillermo Alonso
9 Answers
+ 10
First of all, don't be frustrated and congratulation as you've just discovered a bug in SoloLearn! After a quick check I believe it's a linker problem of the C online compiler used by SoloLearn. You may report this behaviour to [email protected] and meanwhile you may try to run your code at other cloud compilers available like:- https://www.onlinegdb.com P/S: I've verified with link above and it works! 😉
26th Oct 2019, 1:18 AM
Zephyr Koo
Zephyr Koo - avatar
+ 4
Hi Zephyr Koo , It's been a long time! Been busy with work I guess? This is really intriguing case, because I could get the code to run (Yes in Code Playground) when I pass a literal value as argument for `sqrt` printf("sqrt(361) = %.0lf", sqrt(361)); // this works, strangely At first I thought the problem was with the format specifier "%d\n" at line 10, but nope, I was wrong. It still doesn't work although I changed it to "%d" only. Tried to change some numbers by adding decimal point, and occasional casting, but it didn't work either. s = s + 1.0 / (double)(i*i); // didn't help pi = sqrt(6.0 * s); // didn't help Looking forward for others' opinions : )
26th Oct 2019, 3:19 AM
Ipang
+ 4
I also tried the code with no modifications on my machine, no issues. Ipang seems to have found a temporary fix for now. Another suggestion is to write your own version of the sqrt function. Sure it may not be as efficient as the library one but it will give you the experience of understanding how the function is built. 🙂
26th Oct 2019, 3:26 AM
Ace
Ace - avatar
+ 4
Chum4k3r Wow indeed thanks for informing! It's probably the weirdest bug I've seen in SoloLearn. 👍
27th Oct 2019, 11:52 PM
Zephyr Koo
Zephyr Koo - avatar
+ 4
Zephyr Koo it's pretty common with initial setups.
27th Oct 2019, 11:54 PM
Ace
Ace - avatar
+ 3
Guillermo Alonso Zephyr Koo Ace I found these on SO, seems it be a compiler issue (gcc if I understand it correctly). No experience with compiler command options so I'm leaving this to you guys 👍 [Referenced from the thread below] https://stackoverflow.com/questions/5248919/undefined-reference-to-sqrt-or-other-mathematical-functions [Marked as duplicate (forwarded to one above)] https://stackoverflow.com/questions/10409032/why-am-i-getting-undefined-reference-to-sqrt-error-even-though-i-include-math
26th Oct 2019, 4:22 AM
Ipang
+ 3
Ipang Yup it has been awhile and feel grateful to be able to come back from time to time! You've got some good observation and I really wonder the strange behaviour as well. Nice catch! 😉
27th Oct 2019, 1:58 PM
Zephyr Koo
Zephyr Koo - avatar
+ 3
I was facing this issue in my code yesterday, but today it worked. Seems like the linker bug has been fixed
27th Oct 2019, 7:58 PM
Chum4k3r
Chum4k3r - avatar
+ 2
Good to have you back Zephyr Koo 👍
27th Oct 2019, 2:12 PM
Ipang