16

The standard library function abs() is declared in stdlib.h, while fabs() is in math.h.

Why are they reside in different headers?

nalzok
  • 14,965
  • 21
  • 72
  • 139
Sanjana Jose
  • 238
  • 1
  • 8
  • 3
    `abs()` is for `int` type, `fabs()` is for `double`. – Weather Vane Aug 23 '16 at 09:58
  • 4
    I put it down to historical reasons. – Bathsheba Aug 23 '16 at 09:59
  • 2
    @WeatherVane I think the OP knows that. He is curious why are they in different headers. – Mirakurun Aug 23 '16 at 09:59
  • 3
    I would say it is obvious; because you won't want the `math.h` header when you are not using floating point arithmetic, and conversely `fabs` belongs with other floating point definitions. – Weather Vane Aug 23 '16 at 10:00
  • 15
    Because when C was created, FPUs was a rare occurence so floating point functions are put to separate library and headers. This anwer has more info on it: http://stackoverflow.com/a/1034012/2709018 – myaut Aug 23 '16 at 10:01
  • The mathematical functions in `math.h` all use floating point. – bzeaman Aug 23 '16 at 10:01
  • If you're looking for extravagant implementations, then, AFIAR, in Plan9 all of the standard library macros and declarations come from a single header file (which is nonstandard). – Alexey Frunze Aug 23 '16 at 11:09
  • http://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c – Hans Passant Aug 23 '16 at 12:34

2 Answers2

5

math.h first appears in 7th Research Unix. It is hard to tell how it got there. For example, [1] claims that bits of C library were merged from "PWB/Unix" which included troff and C compiler pcc, but I cannot prove it.

Another interesting piece of information is library manual from 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>.

If you look into V7 commands makefiles, only few C programs are linked with -lm flag. So my conclusion is speculative:

  1. libm.a (and math.h) was primarily needed for FORTRAN programs mostly, so it was separated into library to reduce binary footprint (note that it was linked statically).
  2. Not many machines had floating point support. For example, you would need to buy an optional FPP for PDP-11 [2], there is also libfpsim simulation library in Unix to mitigate that, so floating point can be hardly used in early C programs.

1. A History of UNIX before Berkeley: UNIX Evolution: 1975-1984

2. PDP-11 architecture

myaut
  • 11,174
  • 2
  • 30
  • 62
-1

Most operators like + - / * are also math operators yet these are also readily available. When programming you use so much math, that developers have started to differentiate between math that is needed for everyday stuff and math that is more specialized that you only use some of the time. Abs is one of those functions that are just used to often. Like with pointer arithmetic when you just want to know the difference to calculate the size of a memory block. But you are not interested in knowing which is higher in memory and which is lower.

So to sum up: abs is used often because it calculates the difference of two integers. The difference between two pointers for instance is also an integer. And so it is in stdlib.h. fabs how ever is not something you will need much unless you are doing math specific stuff. Thus it is in math.h.

Dave Kok
  • 892
  • 9
  • 19
  • This does not answer why those functions are in separate files. – HolyBlackCat Aug 23 '16 at 12:48
  • @HolyBlackCat: It does now :) – Dave Kok Aug 23 '16 at 13:34
  • "[...] because it calculates the difference of two integers". No, it doesn't, since it receives a single integer value argument, which can be either a integer literal or an expression and returns the absolute value (or in simple words, the distance from zero). The subtraction of memory addresses for calculating the size of a memory block may be an expression provided as an argument to the function, that is as a result evaluated to a single value and doesn't correspond to the functionality of the abs function itself. – dandev486 May 14 '21 at 11:06