gawk: Computer Arithmetic

1 
1 15.1 A General Description of Computer Arithmetic
1 =================================================
1 
1 Until now, we have worked with data as either numbers or strings.
1 Ultimately, however, computers represent everything in terms of "binary
1 digits", or "bits".  A decimal digit can take on any of 10 values: zero
1 through nine.  A binary digit can take on any of two values, zero or
1 one.  Using binary, computers (and computer software) can represent and
1 manipulate numerical and character data.  In general, the more bits you
1 can use to represent a particular thing, the greater the range of
1 possible values it can take on.
1 
1    Modern computers support at least two, and often more, ways to do
1 arithmetic.  Each kind of arithmetic uses a different representation
1 (organization of the bits) for the numbers.  The kinds of arithmetic
1 that interest us are:
1 
1 Decimal arithmetic
1      This is the kind of arithmetic you learned in elementary school,
1      using paper and pencil (and/or a calculator).  In theory, numbers
1      can have an arbitrary number of digits on either side (or both
1      sides) of the decimal point, and the results of a computation are
1      always exact.
1 
1      Some modern systems can do decimal arithmetic in hardware, but
1      usually you need a special software library to provide access to
1      these instructions.  There are also libraries that do decimal
1      arithmetic entirely in software.
1 
1      Despite the fact that some users expect 'gawk' to be performing
1      decimal arithmetic,(1) it does not do so.
1 
1 Integer arithmetic
1      In school, integer values were referred to as "whole" numbers--that
1      is, numbers without any fractional part, such as 1, 42, or -17.
1      The advantage to integer numbers is that they represent values
1      exactly.  The disadvantage is that their range is limited.
1 
1      In computers, integer values come in two flavors: "signed" and
1      "unsigned".  Signed values may be negative or positive, whereas
1      unsigned values are always greater than or equal to zero.
1 
1      In computer systems, integer arithmetic is exact, but the possible
1      range of values is limited.  Integer arithmetic is generally faster
1      than floating-point arithmetic.
1 
1 Floating-point arithmetic
1      Floating-point numbers represent what were called in school "real"
1      numbers (i.e., those that have a fractional part, such as
1      3.1415927).  The advantage to floating-point numbers is that they
1      can represent a much larger range of values than can integers.  The
1      disadvantage is that there are numbers that they cannot represent
1      exactly.
1 
1      Modern systems support floating-point arithmetic in hardware, with
1      a limited range of values.  There are software libraries that allow
1      the use of arbitrary-precision floating-point calculations.
1 
1      POSIX 'awk' uses "double-precision" floating-point numbers, which
1      can hold more digits than "single-precision" floating-point
1      numbers.  'gawk' has facilities for performing arbitrary-precision
1      floating-point arithmetic, which we describe in more detail
1      shortly.
1 
1    Computers work with integer and floating-point values of different
1 ranges.  Integer values are usually either 32 or 64 bits in size.
1 Single-precision floating-point values occupy 32 bits, whereas
1 double-precision floating-point values occupy 64 bits.
1 (Quadruple-precision floating point values also exist.  They occupy 128
1 bits, but such numbers are not available in 'awk'.)  Floating-point
1 values are always signed.  The possible ranges of values are shown in
DONTPRINTYET 1 ⇒Table 15.1 table-numeric-ranges. and *Note : 
DONTPRINTYET 1DONTPRINTYET 1 ⇒Table 15.1 table-numeric-ranges. and *Note : 
1 
1 Representation           Minimum value            Maximum value
1 ---------------------------------------------------------------------------
1 32-bit signed integer    -2,147,483,648           2,147,483,647
1 32-bit unsigned          0                        4,294,967,295
1 integer
1 64-bit signed integer    -9,223,372,036,854,775,8089,223,372,036,854,775,807
1 64-bit unsigned          0                        18,446,744,073,709,551,615
1 integer
1 
1 Table 15.1: Value ranges for integer representations
1 
1 Representation              Minimum          Minimum finite   Maximum finite
1                             positive         value            value
1                             nonzero value
1 --------------------------------------------------------------------------------
1 Single-precision            1.175494e-38     -3.402823e+38    3.402823e+38
1 floating-point
1 Double-precision            2.225074e-308    -1.797693e+308   1.797693e+308
1 floating-point
1 Quadruple-precision         3.362103e-4932   -1.189731e+4932  1.189731e+4932
1 floating-point
1 
1 Table 15.2: Approximate value ranges for floating-point number
1 representations
1 
1    ---------- Footnotes ----------
1 
1    (1) We don't know why they expect this, but they do.
1