AWK's Built-in Math Functions
AWK ships with a compact set of numeric functions that cover most everyday computation: sqrt, exp, log, sin, cos, atan2, int, plus rand and srand for randomness. There is no separate math library to import — these are always available — but the set is deliberately minimal, so operations such as rounding or raising to a power are expressed by combining the primitives with AWK's arithmetic operators.
Cricket analogy: Like a batsman with a compact but complete set of shots — drive, cut, pull, sweep — no exotic strokes, yet enough to score all around the ground, AWK's math kit is small but sufficient.
Arithmetic and Rounding
int(x) truncates toward zero, discarding the fractional part, so int(2.9) is 2 and int(-2.9) is -2, which is not the same as mathematical floor. AWK has no built-in round function; the idiom int(x + 0.5) rounds a positive number to the nearest integer. Exponentiation uses the ^ operator (or ** in gawk), so a square is written x^2 and a cube root is x^(1/3); there is no separate pow function.
Cricket analogy: Like a strike rate shown as a whole number by chopping the decimals, int() lops off the fraction the way a scoreboard shows 137 rather than 137.4.
BEGIN {
x = 2.9
print int(x) # 2 (truncates toward zero)
print int(x + 0.5) # 3 (round to nearest)
print 5^2, 27^(1/3) # 25 3 (power and cube root)
}Trigonometry and Logarithms
The trigonometric functions sin and cos take their argument in radians, not degrees, so you convert with x * atan2(0,-1) / 180, since atan2(0,-1) equals pi. log is the natural logarithm (base e); for any other base use the change-of-base rule log(x)/log(base). exp raises e to a power, and atan2(y,x) returns the angle of the point (x,y) while correctly resolving all four quadrants, which a plain arctangent cannot.
Cricket analogy: Like measuring a shot's angle in the umpire's preferred unit, you must convert degrees to radians the way you would convert overs to balls before the math lines up.
BEGIN {
pi = atan2(0, -1)
print sin(30 * pi / 180) # 0.5 (30 degrees, converted to radians)
print log(100) / log(10) # 2 (logarithm base 10)
}AWK has no factorial, floor, ceil, or pow function. Remember that int() is not floor for negatives: int(-2.5) is -2, whereas mathematical floor(-2.5) is -3. Build floor, ceiling, and powers from the primitives you do have — for example floor via int() with an adjustment for negative values.
Random Numbers with rand() and srand()
rand() returns a pseudo-random number in the half-open range [0, 1). Without calling srand() first, AWK seeds the generator with a fixed value, so every run produces the identical sequence — helpful for reproducible tests, but a trap if you expect real variety. Call srand() with no argument to seed from the current time of day, or srand(n) with a fixed integer to reproduce a specific sequence on demand. To get an integer in a range, scale and truncate, as in int(rand()*6)+1.
Cricket analogy: Like a bowling machine that repeats the exact same delivery sequence unless you reprogram it, rand() without srand() replays identical 'random' balls every session.
BEGIN {
srand() # seed from the current time of day
print int(rand() * 6) + 1 # a random integer from 1 to 6 (a dice roll)
}Calling srand() twice within the same second with no argument can reseed from the same clock value and reproduce the same sequence, because the default seed has only one-second resolution. For distinct sequences generated in rapid succession, pass an explicit, changing seed such as a counter combined with the time.
- AWK's built-in math functions: sqrt, exp, log, sin, cos, atan2, int, rand, srand.
- int(x) truncates toward zero, differing from mathematical floor for negatives.
- There is no round function; use int(x + 0.5) for positive numbers.
- Powers use ^ (or ** in gawk); log is natural log, so use log(x)/log(b) for other bases.
- sin and cos expect radians; convert degrees with x*atan2(0,-1)/180 since atan2(0,-1)=pi.
- rand() returns [0,1); without srand() the sequence repeats identically each run.
- srand() seeds from the clock; srand(n) gives a reproducible sequence.
Practice what you learned
1. What does int(-2.9) return in AWK?
2. How do you round a positive number x to the nearest integer in AWK?
3. In what unit do sin() and cos() expect their argument?
4. What happens if you call rand() without ever calling srand()?
5. Which expression computes pi in AWK?
Was this page helpful?
You May Also Like
Formatted Output with printf
Use AWK's printf statement to control field width, alignment, and numeric precision, producing aligned tables and reports that the plain print statement cannot.
gawk Extensions
Survey the features GNU awk adds over POSIX AWK — gensub, true multidimensional arrays, asort, IGNORECASE, time and networking functions, and the C extension API — and their portability trade-offs.
AWK and CSV Processing
Understand why splitting CSV on commas is unsafe, and learn how gawk's FPAT variable and built-in --csv mode correctly parse quoted fields, embedded commas, and escaped quotes.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics