Awk built-in arithmetic functions

Source: Internet
Author: User
Tags natural logarithm

Awk built-in arithmetic functions

The following table lists the built-in arithmetic functions of awk. x and y are arbitrary expressions in the table.
Arithmetic functions

Function Name Return Value
Atan2 (x, y) Returns the arc tangent of y/x in the value range.
Cos (x) Returns the cosine of x, which is a radian.
Exp (x) E-exponential function of x
Int (x) An integer of x. when x is greater than 0, the value is rounded down.
Log (x) Natural logarithm of x (base number is e)
Rand () Random Number r (0
Sin (x) Returns the sine of x, which is a radian.
Sqrt (x) Square root of x
Srand (x) X is the new seed of rand ().

 

Integer Functions
The int function removes all digits after the decimal point and generates an integer. The int function does not perform rounding.
Example

$ Awk 'end {print 31/3} 'filename
10.3333
$ Awk 'end {print int (31/3)} 'filename
10

Note:
1. The END Block prints the result of the division operation as a floating point number.
2. The int function in the END block removes the result of division from the decimal point. The result is an integer.

Random number generator
The rand function generates a pseudo-random floating point number greater than or equal to 0 and less than 1.
Example

$ Awk '{print rand ()}' filename
0.513871
0.175726
0.308634

$ Awk '{print rand ()}' filename
0.513871
0.175726
0.308634

Note:
Each running program prints the same number group. You can use the srand function to set a new value for the seed of the rand function. Otherwise, as shown in the preceding example, each rand call will only repeat the same sequence.

If no parameter is specified for the srand function, the srand function generates a seed for the rand function based on the current time. Srand (x) sets x as a seed. In general, the program should constantly change the value of x during the running process.
Example

$ Awk 'in in {srand ()} {print rand ()} 'filename
0.508744
0.639485
0.657277
$ Awk 'in in {srand ()} {print rand ()} 'filename
0.133518
0.324747
0.691794

Note: The srand function sets a new seed for rand. The starting point is the current time. Therefore, each call to rand prints a new series.
Example

$ Awk 'in in {srand ()} {print 1 + int (rand () * 25 )}'
6
24
14

Note: The srand function sets a new seed for rand. The starting point is the current time. The rand function escapes a random number between 0 and 25 and converts it to an integer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.