100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Math Functions in AWK

Explore AWK's built-in numeric functions — sqrt, exp, log, sin, cos, atan2, int, rand, and srand — and the idioms for rounding, other-base logarithms, degree-based trig, and reproducible randomness.

Text ProcessingIntermediate9 min readJul 10, 2026
Analogies

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.

awk
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.

awk
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.

awk
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

Was this page helpful?

Topics covered

#Programming#AWKStudyNotes#MathFunctionsInAWK#Math#Functions#AWK#Built#StudyNotes#SkillVeris#ExamPrep