Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer division in awk

I want to divide two numbers in awk, using integer division, i.e truncating the result. For example

k = 3 / 2
print k

should print 1

According to the manual,

Division; because all numbers in awk are floating-point numbers, the result is not rounded to an integer

Is there any workaround to get an integer value?

The reason is that I want to get the middle element of an array with integer indexes [0 to num-1].

like image 480
user000001 Avatar asked Feb 13 '13 16:02

user000001


People also ask

Can you do math in awk?

Awk uses the standard four arithmetic operators: + Addition. - Subtraction. * Multiplication.


3 Answers

Use the int function to get the integer part of the result, truncated toward 0. This produces the nearest integer to the result, located between the result and 0. For example, int(3/2) is 1, int(-3/2) is -1.

Source: The AWK Manual - Numeric Functions

like image 88
Gilles Quenot Avatar answered Sep 18 '22 16:09

Gilles Quenot


In simple cases, you can safely use int() which truncates towards zero1:

awk 'BEGIN { print int(3 / 2) }'    # prints 1 gawk 'BEGIN { print int(-3 / 2) }'  # prints -1; not guaranteed in POSIX awk 

Keep in mind that awk always uses double-precision floating point numbers2 and floating-point arithmetic3, though. The only way you can get integers and integer arithmetic is to use external tools, e.g. the standard expr utility:

awk 'BEGIN { "expr 3 / 2" | getline result; print result; }'    # prints 1 

This is really awkward, long, slow, … but safe and portable.


1 In POSIX awk, truncation to zero is guaranteed only for positive arguments: int(x) — Return the argument truncated to an integer. Truncation shall be toward 0 when x>0. GNU awk (gawk) uses truncation toward zero even for negative numbers: int(x) — Return the nearest integer to x, located between x and zero and truncated toward zero. For example, int(3) is 3, int(3.9) is 3, int(-3.9) is -3, and int(-3) is -3 as well.
2 Numeric expressions are specified as double-precision floats in Expressions in awk in POSIX.
3All arithmetic shall follow the semantics of floating-point arithmetic as specified by the ISO C standard (see Concepts Derived from the ISO C Standard). — POSIX awk: Arithmetic functions


If you choose to use floats, you should know about their quirks and be ready to spot them and avoid related bugs. Several scary examples:

  • Unrepresentable numbers:

    awk 'BEGIN { x = 0.875; y = 0.425; printf("%0.17g, %0.17g\n", x, y) }' # prints 0.875, 0.42499999999999999 
  • Round-off errors accumulation:

    awk 'BEGIN{s=0; for(i=1;i<=100000;i++)s+=0.3; printf("%.10f, %d\n",s,int(s))}' # prints 29999.9999999506, 29999 
  • Round-off errors ruin comparisons:

    awk 'BEGIN { print (0.1 + 12.2 == 12.3) }'    # prints 0 
  • Precision decreases with magnitude, causing infinite loops:

    awk 'BEGIN { for (i=10^16; i<10^16+5; i++) printf("%d\n", i) }' # prints 10000000000000000 infinitely many times 

Read more on how floats work:

  1. Stack Overflow tags floating-point wiki

  2. Wikipedia article Floating point

  3. GNU awk arbitrary precision arithmetic – contains both info on the specific implementation and general knowledge

like image 38
Palec Avatar answered Sep 22 '22 16:09

Palec


Safe and quick awk integer division can be done with:

q=(n-n%d)/d+(n<0)
like image 34
Marijan Unetić Avatar answered Sep 22 '22 16:09

Marijan Unetić