C语言 C 编程 sqrt 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4120592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:56:12  来源:igfitidea点击:

C programming sqrt function

c

提问by matthewmpp

#include <math.h> 
#include <stdio.h> 

int main(void) 
{ 
    double x = 4.0, result; 

    result = sqrt(x); 
    printf("The square root of %lf is %lfn", x, result); 
    return 0; 
} 

This code does not work because it is taking the square root of a variable. If you change the sqrt(x), to sqrt(20.0), the code works just fine, why? Please explain.

此代码不起作用,因为它正在取变量的平方根。如果您将sqrt(x),更改为,sqrt(20.0)代码工作正常,为什么?请解释。

Also, how do I get the square root of the variable (which is what I really need)?

另外,如何获得变量的平方根(这是我真正需要的)?

OUTPUT:

输出:

matthewmpp@annrogers:~/Programming/C.progs/Personal$ vim sqroot1.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -c sqroot1.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -o sqroot1 sqroot1.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ ./sqroot1
4.472136
matthewmpp@annrogers:~/Programming/C.progs/Personal$ vim sqroot2.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -c sqroot2.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -o sqroot2 sqroot2.c
/tmp/ccw2dVdc.o: In function `main':
sqroot2.c:(.text+0x29): undefined reference to `sqrt'
collect2: ld returned 1 exit status
matthewmpp@annrogers:~/Programming/C.progs/Personal$ 

NOTE:sqroot1 is the sqroot of 20.0. sqroot2 is the sqroot of a variable.

注意:sqroot1 是 20.0 的 sqroot。sqroot2 是变量的平方根。

matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -o sqroot2 sqroot2.c -lm
matthewmpp@annrogers:~/Programming/C.progs/Personal$ ./sqroot2
4.472136
matthewmpp@annrogers:~/Programming/C.progs/Personal$ 

SOLVED.

解决了。

回答by Kizaru

The code should work just fine if you are linking in the proper libraries (libc.a and libm.a). Your issue is probably that you are using gcc and you are forgetting to link in libm.a via -lm, which is giving you an undefined reference to sqrt. GCC calculates the sqrt(20.0)at compile time because it is a constant.

如果您在正确的库(libc.a 和 libm.a)中进行链接,代码应该可以正常工作。您的问题可能是您正在使用 gcc 并且您忘记在 libm.a 中通过 链接-lm,这给了您对 sqrt 的未定义引用。GCCsqrt(20.0)在编译时计算它,因为它是一个常数。

Try to compile it with

尝试编译它

gcc myfile.c -lm

EDIT: Some more information. You can confirm this by looking at the generated assembly when you replace x with a constant in the sqrtcall.

编辑:更多信息。您可以通过在sqrt调用中将 x 替换为常量时查看生成的程序集来确认这一点。

gcc myfile.c -S

Then take a look at the assembly in myfile.sand you will not see the line call sqrtanywhere.

然后看看里面的装配myfile.s,你不会在call sqrt任何地方看到这条线。

回答by subhash kumar singh

You should do it like this:

你应该这样做:

root@bt:~/Desktop# gcc -lm sqrt.c -o sqrt
root@bt:~/Desktop# ./sqrt
The square root of 4.000000 is 2.000000n
root@bt:~/Desktop#