Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to postgresql-- Mathematical function

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1. Mathematical function

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 an error occurs, the mathematical function returns a null value of null.

1.1.The absolute value function ABS (x) and the function PI () that returns pi

Example: find the absolute value of 2mai 3.3 mai 33

Testdb=# select abs (2), abs (- 3.3), abs (- 33)

Abs | abs | abs

-+-

2 | 3.3 | 33

(1 row)

Example: returns the value of pi, as follows:

Testdb=# select pi ()

Pi

-

3.14159265358979

(1 row)

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

Sqrt (x) returns the quadratic square root of a non-negative number x

Mod (xPowery) returns the remainder of x divided by y. Mod () also works for values with decimal parts, returning the exact remainder after division.

Example: find the quadratic square root of 9 and 40:

Testdb=# select sqrt (9), sqrt (40)

Sqrt | sqrt

-+-

3 | 6.32455532033676

(1 row)

Note: a negative number does not have a square root. If the value evaluated is negative, an error message will be prompted.

Example: perform the remainder operation:

Testdb=# select mod (31), mod (234), mod (45.5)

Mod | mod | mod

-+-

7 | 4 | 3.5

(1 row)

1.3.Functions to get integers ceil (x), ceiling (x) and floor (x)

Ceil (x) and ceiling (x) have the same meaning, returning a minimum integer value of not 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.The rounding functions round (x) and round (x)

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

Round returns the number closest to the parameter x, whose value is retained to y places after the decimal point, and if y is negative, the x value is retained to the left of the decimal place.

Example: use the round (x) function to round 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: round up operands using the round (xQuery y) function, as shown in

Testdb=# select round (1.38), round (1.38), round (231.36), round (231.36)

Round | round | round | round

-+-

1.4 | 1 | 230 | 200

(1 row)

1.5. symbolic function sign (x)

Sign (x) returns the symbol of the parameter. If the value of x is negative, zero or positive, the return result is:-1 zero 0 or 1.

Example:

Testdb=# select sign (- 21), sign (0), sign (21)

Sign | sign | sign

-+-

-1 | 0 | 1

(1 row)

1.6.The power operation functions pow (x ~ () ~ y), power (x ~ ()) and exp (x)

The function pow (xMagine y), power (xjingy) returns the result value of the y power of x.

Exp (x) returns the value of e to the power of x

Example: use the pow,power function for multiplier operations, such as:

Testdb=# select pow (2), power (2), pow (2), power (2)

Pow | power | pow | power

-+-

4 | 4 | 0.25 | 0.25

(1 row)

Example: use exp (x) to return the value of e to the power of x

Testdb=# select exp (3), exp (- 3), exp (0)

Exp | exp | exp

-+-

20.0855369231877 | 0.0497870683678639 | 1

(1 row)

1.7. logarithmic operation function: log (x)

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

Testdb=# select log (3)

Log

-

0.477121254719662

(1 row)

1.8. the function of conversion between angle and Radian: radians (x) and degrees (x)

Radians (x) converts the parameter x from angles to radians.

Such as:

Testdb=# select radians 90, radians 180)

Radians | radians

-+-

1.5707963267949 | 3.14159265358979

(1 row)

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

Testdb=# select degrees (pi ()), degrees (pi () / 2)

Degrees | degrees

-+-

180 | 90

(1 row)

1.9. sine function: sin (x) and Arcsine function: asin (x)

Sin (x) returns the sine of x, where x is the arc value.

Testdb=# select sin (1), round (sin (pi ()

Sin | round

-+-

0.841470984807897 | 0

(1 row)

Asin (x) returns the sine of x, that is, the value where the sine is x. If x is not in the range of-1 to 1, an error message pops up: 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 arc 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 in the range of-1 to 1, an error message will pop up.

Testdb=# select acos (1), acos (0), round (acos (0.54030230586814))

Acos | acos | round

-+-

0 | 1.5707963267949 | 1

(1 row)

Tangent function: tan (x), Arctangent function: atan (x), Cotangent function: cot (x)

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

Example:

Testdb=# select tan, round (tan (pi () / 4))

Tan | round

-+-

0.309336249609623 | 1

(1 row)

Atan (x) returns the inverse tangent of x, that 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, 1/tan, cot (pi () / 4)

Cot |? column? | cot

-+-

3.23272814376583 | 3.23272814376583 | 1

(1 row)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 282

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report