15

标准库函数abs()在 中声明stdlib.h,而fabs()math.h.

为什么它们驻留在不同的标题中?

4

2 回答 2

5

math.h首次出现在 7th Research Unix 中。很难说它是如何到达那里的。例如,[1] 声称 C 库的一些部分是从包含troffC 编译器的“PWB/Unix”合并而来的pcc,但我无法证明这一点。

另一个有趣的信息是来自 V7 Unix 的库手册: intro.3

(3)   These functions, together with those of section 2 and those marked (3S),
      constitute library libc, which is automatically loaded by the C compiler
      cc(1) and the Fortran compiler f77(1).  The link editor  ld(1)  searches
      this  library  under  the  `-lc' option.  Declarations for some of these
      functions may be obtained from include files indicated on the  appropri-
      ate pages.

<...>

(3M)  These  functions  constitute the math library, libm.  They are automati-
      cally loaded as needed by the Fortran compiler f77(1).  The link  editor
      searches  this  library  under the `-lm' option.  Declarations for these
      functions may be obtained from the include file <math.h>.

如果您查看 V7 命令 makefile,只有少数 C 程序与-lmflag 链接。所以我的结论是推测性的:

  1. libm.a(and math.h) 主要是 FORTRAN 程序需要的,所以它被分离到库中以减少二进制占用(注意它是静态链接的)。
  2. 没有多少机器支持浮点。例如,您需要为 PDP-11 [2]购买可选的 FPP,Unix 中还有 libfpsim 模拟库来缓解这种情况,因此浮点很难在早期的 C 程序中使用。

1.伯克利之前的 UNIX 历史:UNIX 演变:1975-1984

2. PDP-11架构

于 2016-08-23T23:22:41.447 回答
-1

大多数运算符(如 + - / *)也是数学运算符,但它们也很容易获得。在编程时,你使用了太多的数学,以至于开发人员已经开始区分日常事物所需的数学和只在某些时候使用的更专业的数学。Abs 是那些经常使用的功能之一。当您只想知道计算内存块大小的差异时,就像指针算术一样。但是你对知道哪个内存更高哪个更低不感兴趣。

所以总结一下:abs经常被使用,因为它计算两个整数的差。例如,两个指针之间的差异也是一个整数。所以它在 stdlib.h 中。除非您正在做数学特定的事情,否则晶圆厂永远不是您需要的东西。因此它在 math.h 中。

于 2016-08-23T11:16:28.650 回答