万万想不到的 C pow函数,真神奇
环境:os: Ubuntu 20.04.2 LTS
shell: bash
compiler collection: gcc 9.3.0
#include <stdio.h>
#include <math.h>
int main() {
int result = 0;
for(int i = 0; i <= 3; i++) {
result += pow(2, i);
}
printf("%d", result);
return 0;
}
编译运行结果,你猜怎么着,以下:
/usr/bin/ld: /tmp/ccBMn7hy.o: in function `main':
wheat_count.c:(.text+0x32): undefined reference to `pow'
collect2: error: ld returned 1 exit status
再看
#include <stdio.h>
#include <math.h>
int main() {
int result = 0;
for(int i = 0; i <= 3; i++) {
result += pow(2, 3);
}
printf("%d\n", result);
return 0;
}
你再猜怎么着, 以下:
32
现象:
用循环索引时,找不到pow库函数; 不用,它就工作正常,C语言真高级,或着说这个gcc真高级
错误截图:
追一下:
环境:
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
你猜怎么着,FUCKING THE SAME. stack overflow上找到解决方法了
编译时要加-lm选项:
gcc xxx.c -lm
使用循环索引时要额外加-lm, SB. gcc -o sphere sphere.c -lm 傻眼貓咪 发表于 2021-10-10 11:51
gcc -o sphere sphere.c -lm
@傻眼貓咪
stackflow 还是强大哈。 您猜怎么着,我没看懂啊哈哈{:10_266:} 我的没问题,不用加选项,gcc 10.3.0。 很简单啊
2 的 3 次方,编译器根本就没有调用 pow 函数,编译器直接生成浮点指令进行计算的
当然不会编译报错,因为根本就没有调用 pow 函数
#include <stdio.h>
#include <math.h>
int main() {
10040107f: 90 nop
0000000100401080 <main>:
100401080: 55 pushq%rbp
100401081: 48 89 e5 movq %rsp,%rbp
100401084: 48 83 ec 30 subq $0x30,%rsp
100401088: e8 63 00 00 00 callq1004010f0 <__main>
int result = 0;
10040108d: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp)
for(int i = 0; i <= 3; i++) {
100401094: c7 45 f8 00 00 00 00 movl $0x0,-0x8(%rbp)
10040109b: eb 1c jmp 1004010b9 <main+0x39>
result += pow(2, 3);
10040109d: f2 0f 2a 45 fc cvtsi2sdl -0x4(%rbp),%xmm0
1004010a2: f2 0f 10 0d 5e 1f 00 movsd0x1f5e(%rip),%xmm1 # 100403008 <.rdata+0x8>
1004010a9: 00
1004010aa: f2 0f 58 c1 addsd%xmm1,%xmm0
1004010ae: f2 0f 2c c0 cvttsd2si %xmm0,%eax
1004010b2: 89 45 fc movl %eax,-0x4(%rbp)
for(int i = 0; i <= 3; i++) {
1004010b5: 83 45 f8 01 addl $0x1,-0x8(%rbp)
1004010b9: 83 7d f8 03 cmpl $0x3,-0x8(%rbp)
1004010bd: 7e de jle 10040109d <main+0x1d>
}
printf("%d\n", result);
1004010bf: 8b 45 fc movl -0x4(%rbp),%eax
1004010c2: 89 c2 movl %eax,%edx
1004010c4: 48 8d 0d 35 1f 00 00 leaq 0x1f35(%rip),%rcx # 100403000 <.rdata>
1004010cb: e8 30 00 00 00 callq100401100 <printf>
return 0;
1004010d0: b8 00 00 00 00 movl $0x0,%eax
}
1004010d5: 48 83 c4 30 addq $0x30,%rsp
1004010d9: 5d popq %rbp
1004010da: c3 retq
1004010db: 90 nop
1004010dc: 90 nop
1004010dd: 90 nop
1004010de: 90 nop
1004010df: 90 nop
#include <stdio.h>
#include <math.h>
int main() {
10040107f: 90 nop
0000000100401080 <main>:
100401080: 55 pushq%rbp
100401081: 48 89 e5 movq %rsp,%rbp
100401084: 48 83 ec 40 subq $0x40,%rsp
100401088: e8 83 00 00 00 callq100401110 <__main>
int result = 0;
10040108d: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp)
for(int i = 0; i <= 3; i++) {
100401094: c7 45 f8 00 00 00 00 movl $0x0,-0x8(%rbp)
10040109b: eb 36 jmp 1004010d3 <main+0x53>
result += pow(2, i);
10040109d: f2 0f 2a 45 f8 cvtsi2sdl -0x8(%rbp),%xmm0
1004010a2: 48 8b 05 5f 1f 00 00 movq 0x1f5f(%rip),%rax # 100403008 <.rdata+0x8>
1004010a9: 66 0f 28 c8 movapd %xmm0,%xmm1
1004010ad: 48 89 45 e8 movq %rax,-0x18(%rbp)
1004010b1: f2 0f 10 45 e8 movsd-0x18(%rbp),%xmm0
1004010b6: e8 65 00 00 00 callq100401120 <pow>
1004010bb: 66 0f 28 c8 movapd %xmm0,%xmm1
1004010bf: f2 0f 2a 45 fc cvtsi2sdl -0x4(%rbp),%xmm0
1004010c4: f2 0f 58 c1 addsd%xmm1,%xmm0
1004010c8: f2 0f 2c c0 cvttsd2si %xmm0,%eax
1004010cc: 89 45 fc movl %eax,-0x4(%rbp)
for(int i = 0; i <= 3; i++) {
1004010cf: 83 45 f8 01 addl $0x1,-0x8(%rbp)
1004010d3: 83 7d f8 03 cmpl $0x3,-0x8(%rbp)
1004010d7: 7e c4 jle 10040109d <main+0x1d>
}
printf("%d", result);
1004010d9: 8b 45 fc movl -0x4(%rbp),%eax
1004010dc: 89 c2 movl %eax,%edx
1004010de: 48 8d 0d 1b 1f 00 00 leaq 0x1f1b(%rip),%rcx # 100403000 <.rdata>
1004010e5: e8 46 00 00 00 callq100401130 <printf>
return 0;
1004010ea: b8 00 00 00 00 movl $0x0,%eax
}
1004010ef: 48 83 c4 40 addq $0x40,%rsp
1004010f3: 5d popq %rbp
1004010f4: c3 retq
1004010f5: 90 nop
人造人 发表于 2021-10-10 14:34
来了个狠的 我草 最佳答案是个啥我怎么看不懂啊 Python初学者8号 发表于 2021-10-14 21:29
我草 最佳答案是个啥我怎么看不懂啊
页:
[1]