12.4. Numeric Functions

MySQL 5.0

12.4. Numeric Functions

12.4.1. Arithmetic Operators

The usual arithmetic operators are available. The precision of the result is determined according to the following rules:

  • Note that in the case of , , and , the result is calculated with (64-bit) precision if both arguments are integers.

  • If one of the arguments is an unsigned integer, and the other argument is also an integer, the result is an unsigned integer.

  • If any of the operands of a , , , , is a real or string value, then the precision of the result is the precision of the argument with the maximum precision.

  • In multiplication and division, the precision of the result when using two integer values is the precision of the first argument + the value of the global variable. For example, the expression would have a precision of six decimal places ().

These rules are applied for each operation, such that nested calculations imply the precision of each component. Hence, , would resolve first to , with the final result having 8 decimal places ().

Because of these rules and the method they are applied, care should be taken to ensure that components and sub-components of a calculation use the appropriate level of precision. See Section 12.8, “Cast Functions and Operators”.

  • Addition:

    mysql> 
            -> 8
    
  • Subtraction:

    mysql> 
            -> -2
    
  • Unary minus. This operator changes the sign of the argument.

    mysql> 
            -> -2
    

    Note: If this operator is used with a , the return value is also a . This means that you should avoid using on integers that may have the value of –263.

  • Multiplication:

    mysql> 
            -> 15
    mysql> 
            -> 324518553658426726783156020576256.0
    mysql> 
            -> 0
    

    The result of the last expression is incorrect because the result of the integer multiplication exceeds the 64-bit range of calculations. (See Section 11.2, “Numeric Types”.)

  • Division:

    mysql> 
            -> 0.60
    

    Division by zero produces a result:

    mysql> 
            -> NULL
    

    A division is calculated with arithmetic only if performed in a context where its result is converted to an integer.

  • Integer division. Similar to , but is safe with values.

    mysql> 
            -> 2
    

12.4.2. Mathematical Functions

All mathematical functions return in the event of an error.

  • )

    Returns the absolute value of .

    mysql> 
            -> 2
    mysql> 
            -> 32
    

    This function is safe to use with values.

  • )

    Returns the arc cosine of , that is, the value whose cosine is . Returns if is not in the range to .

    mysql> 
            -> 0
    mysql> 
            -> NULL
    mysql> 
            -> 1.5707963267949
    
  • )

    Returns the arc sine of , that is, the value whose sine is . Returns if is not in the range to .

    mysql> 
            -> 0.20135792079033
    mysql> 
    
    +-------------+
    | ASIN('foo') |
    +-------------+
    |           0 |
    +-------------+
    1 row in set, 1 warning (0.00 sec)
    
    mysql> 
    +---------+------+-----------------------------------------+
    | Level   | Code | Message                                 |
    +---------+------+-----------------------------------------+
    | Warning | 1292 | Truncated incorrect DOUBLE value: 'foo' |
    +---------+------+-----------------------------------------+
    
  • )

    Returns the arc tangent of , that is, the value whose tangent is .

    mysql> 
            -> 1.1071487177941
    mysql> 
            -> -1.1071487177941
    
  • ,), ,)

    Returns the arc tangent of the two variables and . It is similar to calculating the arc tangent of / , except that the signs of both arguments are used to determine the quadrant of the result.

    mysql> 
            -> -0.78539816339745
    mysql> 
            -> 1.5707963267949
    
  • ), )

    Returns the smallest integer value not less than .

    mysql> 
            -> 2
    mysql> 
            -> -1
    

    These two functions are synonymous. Note that the return value is converted to a .

  • )

    Returns the cosine of , where is given in radians.

    mysql> 
            -> -1
    
  • )

    Returns the cotangent of .

    mysql> 
            -> -1.5726734063977
    mysql> 
            -> NULL
    
  • )

    Computes a cyclic redundancy check value and returns a 32-bit unsigned value. The result is if the argument is . The argument is expected to be a string and (if possible) is treated as one if it is not.

    mysql> 
            -> 3259397556
    mysql> 
            -> 2501908538
    
  • )

    Returns the argument , converted from radians to degrees.

    mysql> 
            -> 180
    mysql> 
            -> 90
    
  • )

    Returns the value of e (the base of natural logarithms) raised to the power of .

    mysql> 
            -> 7.3890560989307
    mysql> 
            -> 0.13533528323661
    mysql> 
            -> 1
    
  • )

    Returns the largest integer value not greater than .

    mysql> 
            -> 1
    mysql> 
            -> -2
    

    Note that the return value is converted to a .

  • ,)

    Formats the number to a format like , rounded to decimal places, and returns the result as a string. For details, see Section 12.3, “String Functions”.

  • )

    Returns the natural logarithm of ; that is, the base-e logarithm of .

    mysql> 
            -> 0.69314718055995
    mysql> 
            -> NULL
    

    This function is synonymous with ).

  • ), ,)

    If called with one parameter, this function returns the natural logarithm of .

    mysql> 
            -> 0.69314718055995
    mysql> 
            -> NULL
    

    If called with two parameters, this function returns the logarithm of for an arbitrary base .

    mysql> 
            -> 16
    mysql> 
            -> 2
    

    ,) is equivalent to ) / LOG().

  • )

    Returns the base-2 logarithm of .

    mysql> 
            -> 16
    mysql> 
            -> NULL
    

    is useful for finding out how many bits a number requires for storage. This function is equivalent to the expression ) / LOG(2).

  • )

    Returns the base-10 logarithm of .

    mysql> 
            -> 0.30102999566398
    mysql> 
            -> 2
    mysql> 
            -> NULL
    

    ) is equivalent to ).

  • ,), % , MOD

    Modulo operation. Returns the remainder of divided by .

    mysql> 
            -> 4
    mysql> 
            -> 1
    mysql> 
            -> 2
    mysql> 
            -> 2
    

    This function is safe to use with values.

    also works on values that have a fractional part and returns the exact remainder after division:

    mysql> 
            -> 1.5
    
  • Returns the value of π (pi). The default number of decimal places displayed is seven, but MySQL uses the full double-precision value internally.

    mysql> 
            -> 3.141593
    mysql> 
            -> 3.141592653589793116
    
  • ,), ,)

    Returns the value of raised to the power of .

    mysql> 
            -> 4
    mysql> 
            -> 0.25
    
  • )

    Returns the argument , converted from degrees to radians. (Note that π radians equals 180 degrees.)

    mysql> 
            -> 1.5707963267949
    
  • , )

    Returns a random floating-point value between and inclusive (that is, in the range <= <= ). If an integer argument is specified, it is used as the seed value, which produces a repeatable sequence.

    mysql> 
            -> 0.9233482386203
    mysql> 
            -> 0.15888261251047
    mysql> 
            -> 0.15888261251047
    mysql> 
            -> 0.63553050033332
    mysql> 
            -> 0.70100469486881
    mysql> 
            -> 0.15888261251047
    

    To obtain a random integer in the range <= <= , use the expression + RAND() * (). For example, to obtain a random integer in the range of 7 to 12 inclusive, you could use the following statement:

    SELECT FLOOR(7 + (RAND() * 5));
    

    You cannot use a column with values in an clause, because would evaluate the column multiple times. However, you can retrieve rows in random order like this:

    mysql>  ORDER BY RAND();
    

    combined with is useful for selecting a random sample from a set of rows:

    mysql>  -> 
    

    Note that in a clause is re-evaluated every time the is executed.

    is not meant to be a perfect random generator, but instead is a fast way to generate ad hoc random numbers which is portable between platforms for the same MySQL version.

  • ), ,)

    Returns the argument , rounded to the nearest integer. With two arguments, returns rounded to decimal places. can be negative to cause digits left of the decimal point of the value to become zero.

    mysql> 
            -> -1
    mysql> 
            -> -2
    mysql> 
            -> 2
    mysql> 
            -> 1.3
    mysql> 
            -> 1
    mysql> 
            -> 20
    

    The return type is the same type as that of the first argument (assuming that it is integer, double, or decimal). This means that for an integer argument, the result is an integer (no decimal places).

    Before MySQL 5.0.3, the behavior of when the argument is halfway between two integers depends on the C library implementation. Different implementations round to the nearest even number, always up, always down, or always toward zero. If you need one kind of rounding, you should use a well-defined function such as or instead.

    As of MySQL 5.0.3, uses the precision math library for exact-value arguments when the first argument is a decimal value:

    • For exact-value numbers, uses the “round half up” or “round toward nearest” rule: A value with a fractional part of .5 or greater is rounded up to the next integer if positive or down to the next integer if negative. (In other words, it is rounded away from zero.) A value with a fractional part less than .5 is rounded down to the next integer if positive or up to the next integer if negative.

    • For approximate-value numbers, the result depends on the C library. On many systems, this means that uses the "round to nearest even" rule: A value with any fractional part is rounded to the nearest even integer.

    The following example shows how rounding differs for exact and approximate values:

    mysql> 
    +------------+--------------+
    | ROUND(2.5) | ROUND(25E-1) |
    +------------+--------------+
    | 3          |            2 |
    +------------+--------------+
    

    For more information, see Chapter 21, Precision Math.

  • )

    Returns the sign of the argument as , , or , depending on whether is negative, zero, or positive.

    mysql> 
            -> -1
    mysql> 
            -> 0
    mysql> 
            -> 1
    
  • )

    Returns the sine of , where is given in radians.

    mysql> 
            -> 1.2246063538224e-16
    mysql> 
            -> 0
    
  • )

    Returns the square root of a non-negative number .

    mysql> 
            -> 2
    mysql> 
            -> 4.4721359549996
    mysql> 
            -> NULL        
    
  • )

    Returns the tangent of , where is given in radians.

    mysql> 
            -> -1.2246063538224e-16
    mysql> 
            -> 1.5574077246549
    
  • ,)

    Returns the number , truncated to decimal places. If is , the result has no decimal point or fractional part. can be negative to cause digits left of the decimal point of the value to become zero.

    mysql> 
            -> 1.2
    mysql> 
            -> 1.9
    mysql> 
            -> 1
    mysql> 
            -> -1.9
    mysql> 
           -> 100
    mysql> 
           -> 1028
    

    All numbers are rounded toward zero.