__Math Functions__

__Math Functions__

Apache Pig supports various types of Math Functions such as ABS, ACOS, ASIN, ATAN, and so on to perform different types of operations.

The following is the list of Math functions supported by Apache Pig.

S.No. | Eval Functions | Description |
---|---|---|

1 | ABS(expression) | After applying this function the result is returned as an expression of absolute value. |

2 | ACOS(expression) | After applying this function the result is returned as an expression of arc cosine. |

3 | ASIN(expression) | This function is used to get the arcsine of an expression. |

4 | ATAN(expression) | After applying this function the result is returned as an expression of arctangent. |

5 | CBRT(expression) | After applying this function the result is returned as an expression of the cube root. |

6 | CEIL(expression) | After applying this function the result is returned as an expression rounded up to the closest integer. |

7 | COS(expression) | After applying this function the result is returned as an expression of trigonometric cosine. |

8 | COSH(expression) | After applying this function the result is returned as an expression of hyperbolic cosine. |

9 | EXP(expression) | This function is used to get the Euler’s number e raised to the power of a. |

10 | FLOOR(expression) | This function is used to get the value of an expression rounded down to the nearest integer. |

11 | LOG(expression) | This function is used to get the natural logarithm (base e) of an expression. |

12 | LOG10(expression) | This function is used to get the base 10 logarithm of an expression. |

13 | RANDOM( ) | This function is used to get a pseudo-random number (type double) greater than or equal to 0.0 and less than 1.0. |

14 | ROUND(expression) | This function is used to get the value of an expression rounded to an integer (if the result type is float) or rounded to a long (if the result type is double). |

15 | SIN(expression) | This function is used to get the sine of an expression. |

16 | SINH(expression) | This function is used to get the hyperbolic sine of an expression. |

17 | SQRT(expression) | This function is used to get the positive square root of an expression. |

18 | TAN(expression) | This function is used to get the trigonometric tangent of an angle. |

19 | TANH(expression) | This function is used to get the hyperbolic tangent of an expression. |

Let us see a couple of examples.

__ABS()__

__ABS()__

If the ABS function is applied to an expression then an absolute value is returned.

__Syntax:__

__Syntax:__

**grunt> ABS(expression)**

We have used the “absdata.txt” dataset to perform this operation. We will put “absdata.txt” in the HDFS location “/pigexample/” from the local file system.

**Content of “absdata.txt”:**

```
```
9
12
67
54
34
15
8
2.5
5.9
3.1
3.2
5.6
3.9

We will load “absdata.txt” from the local filesystem into HDFS “/pigexample/” using the below commands.

__Command:__

__Command:__

**$hadoop fs -copyFromLocal /home/cloudduggu/pig/tutorial/absdata.txt /pigexample/**

Now we will create relation "absexample" and load data from HDFS to Pig.

__Command:__

__Command:__

**grunt> absexample = LOAD '/pigexample/absdata.txt' USING PigStorage(',')**

as (absdata:float);

as (absdata:float);

Now we will use the ABS() function to calculate absolute values of the contents "absdata.txt" and print the result on the terminal using the DUMP operator.

__Command:__

__Command:__

**grunt> abscompute = foreach absexample generate (absdata), ABS(absdata);**

**grunt> DUMP abscompute;**

__Output:__

__Output:__

__ACOS()__

__ACOS()__

ACOS function is used to return the arc cosine of an expression.

__Syntax:__

__Syntax:__

**grunt> ACOS(expression)**

Now we will use ACOS() function to calculate arc cosine values of the contents "absdata.txt" and print results on the terminal using the DUMP operator.

__Command:__

__Command:__

**grunt> acoscompute = foreach absexample generate (absdata), ACOS(absdata);**

**grunt> DUMP abscompute;**

__Output:__

__Output:__