Introduction to postgresql--Mathematical functions

Source: Internet
Author: User
Tags abs acos arithmetic asin cos floor function sin square root

1. Mathematical Functions

Mathematical functions are mainly used to deal with numerical data, the main mathematical functions are: absolute value function, trigonometric function (including sine function, tangent function, cotangent function, etc.), logarithmic function, random number function and so on. When there is an error, the math function returns null value.


1.1, the Absolute function abs (x) and return pi function pi ()


Example: Finding the absolute value of a 2,-3.3,-33

testdb=# Select ABS (2), ABS ( -3.3), ABS (-33);

ABS | ABS | Abs

-----+-----+-----

2 |  3.3 | 33

(1 row)


Example: Return pi value, as follows:

testdb=# select Pi ();

Pi

------------------

3.14159265358979

(1 row)


1.2, square root function sqrt (x) and the remainder function mod

SQRT (x) returns the two square root of a non-negative x

MoD (x, y) returns the remainder after x is removed by Y. MoD () also works for numeric values with fractional parts, returning the exact remainder after the division operation.


Example: For two square roots of 9 and 40:

testdb=# Select sqrt (9), sqrt (40);

sqrt | sqrt

------+------------------

3 | 6.32455532033676

(1 row)


Note: Negative numbers do not have square roots, and if the value is negative, an error message will be prompted.


Example: For the remainder operation:

testdb=# Select mod (31,8), mod (234,10), mod (45.5,6);

MoD | MoD | MoD

-----+-----+-----

7 | 4 | 3.5

(1 row)



1.3. Function ceil (x), ceiling (x) and floor (x) for integer

Ceil (x) and ceiling (x) have the same meaning and return a minimum integer value of no less than X, and the return value is converted to a bigint.


Example: Use the ceil and ceiling functions to return the smallest integer, as follows:

testdb=# Select Ceil ( -3.35), ceiling (3.35);

Ceil | Ceiling

------+---------

-3 | 4

(1 row)


Example: Use the floor function to return the maximum integer value, as follows

testdb=# Select Floor ( -3.35), floor (3.35);

Floor | Floor

-------+-------

-4 | 3

(1 row)


1.4. Rounding function round (x) and round

Round (x) returns the integer closest to parameter x, rounding the X value.

Round (x, Y) returns the number closest to the parameter x, whose value remains to the Y-bit after the decimal point, and if Y is negative, the X value is preserved to the left Y-bit of the decimal point.


Example: Use the round (x) function to round up the operands, such as:

testdb=# Select Round ( -1.15), round ( -1.68), round (1.15), round (1.68);

Round | Round | Round | Round

-------+-------+-------+-------

-1 |     -2 |     1 | 2

(1 row)


Example: Use the round (x, Y) function to round up the operands, as

testdb=# Select Round (1.38,1), round (1.38,0), round (231.36,-1), round (231.36,-2);

Round | Round | Round | Round

-------+-------+-------+-------

1.4 |   1 |   230 | 200

(1 row)


1.5. Symbol function sign (x)

sign (x) returns the symbol for the parameter, the value of x is negative, 0, or the return result is: -1,0 or 1.


Example:

testdb=# Select sign ( -21), sign (0), sign (21);

Sign | Sign | Sign

------+------+------

-1 |    0 | 1

(1 row)


1.6, Power arithmetic function pow (x, y), Power (y) and exp (×)

Pow (x, y), the power (x, Y) function returns the result value of the Y-exponent of X;

EXP (x) returns the value after the X-exponentiation of E;


Example: Use the Pow,power function to perform a exponentiation operation, such as:

testdb=# Select POW (2,2), Power (2,2), pow (2,-2), Power (2,-2);

Pow | Power | Pow | Power

-----+-------+------+-------

4 | 4 |  0.25 | 0.25

(1 row)


Example: Using exp (x) to return the value of the X-exponentiation of E

testdb=# Select exp (3), exp ( -3), exp (0);

Exp | Exp | Exp

------------------+--------------------+-----

20.0855369231877 |   0.0497870683678639 | 1

(1 row)


1.7. Logarithmic arithmetic function: log (x)

Log (x) returns the natural number of x, relative to the logarithm of cardinality e. The logarithmic definition field cannot be negative, so the array is negative and the error message pops up.

testdb=# Select log (3);

Log

-------------------

0.477121254719662

(1 row)


1.8. Functions of angle and Radian conversion: radians (x) and degrees (x)

radians (x) converts the parameter x from an angle to a radian.

Such as:

testdb=# Select radians (All), radians (180);

radians | Radians

-----------------+------------------

1.5707963267949 | 3.14159265358979

(1 row)


Degrees (x) converts the parameter x from radians to an angle, such as:

testdb=# Select Degrees (Pi ()), Degrees (PI ()/2);

Degrees | Degrees

---------+---------

180 | 90

(1 row)


1.9. Sine function: sin (x) and inverse sine function: ASIN (x)

Sin (x) returns the X-sine, where x is the Radian value.


testdb=# Select sin (1), round (Sin (pi));

Sin | Round

-------------------+-------

0.841470984807897 | 0

(1 row)


ASIN (x) returns the inverse of x, which is the value of the sine of X. If x is not in the range 1 to 1, an error message pops up: The input is out of range.


1.10. Cosine function: cos (x) and inverse cosine function: ACOs (x)

COS (x) returns the cosine of x, where x is the Radian value.

testdb=# Select cos (0), cos (PI ()), cos (1);

Cos |       Cos | Cos

-----+-----+------------------

1 | -1 | 0.54030230586814

(1 row)


ACOs (x) returns the inverse cosine of x, that is, the cosine is the value of X. If x is not within the range of 1 to 1, an error message pops up.

testdb=# Select ACOs (1), ACOs (0), round (ACOs (0.54030230586814));

ACOs | ACOs | Round

------+-----------------+-------

0 |     1.5707963267949 | 1

(1 row)



1.11. Tangent function: Tan (x), inverse tangent function: atan (x), cotangent function: Cot (x)

Tan (x) returns the tangent of x, where x is the given radian value.


Example:

testdb=# Select Tan (0.3), round (Tan (PI ()/4));

Tan | Round

-------------------+-------

0.309336249609623 | 1

(1 row)



Atan (x) returns the inverse tangent of x, which is the value of the tangent to X.


Example:

testdb=# Select Atan (0.309336249609623), atan (1);

Atan | Atan

------+-------------------

0.3 | 0.785398163397448

(1 row)


Cot (x) returns the cotangent of X.


Example:

testdb=# Select Cot (0.3), 1/tan (0.3), cot (pi ()/4);

Cot |     ? column? | Cot

------------------+------------------+-----

3.23272814376583 |   3.23272814376583 | 1

(1 row)


Introduction to postgresql--Mathematical functions

Related Article

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.