Dark Mode On/Off

# Python Math functions - ceil(), floor(), fabs(), and copysign()

Posted in Programming   LAST UPDATED: SEPTEMBER 1, 2021

In previous posts, we have seen trigonometric functions from the math module. In today's post, we will understand the following functions of the `math` module. Before using any of the math functions, the `math` module has to be imported. It comes inbuilt with Python's default libraries. Importing it gives us access to the library functions defined in it.

1. `ceil()`

2. `floor()`

3. `fabs()`

4. `copysign()`

## 1. The `ceil` method

It takes one parameter as the argument, whose ceil value is to be returned, i.e the number is rounded up to its next integer value. This is done irrespective of whether the number after the decimal is less than 5 or greater than 5. If the number is an integer, it is returned unchanged. If the number is a negative number, it moves the number up on the number line to the next integer value and returns that as output.

### Time for an example:

``````import math

my_int = 4.5467
print (math.ceil(my_int))

my_int = 4.1467
print (math.ceil(my_int))

my_int = 4
print (math.ceil(my_int))

my_int = -4.5467    # It behaves in the same way when used with negative numbers also
print (math.ceil(my_int))``````

Output:

```5
5
4
-4```

## 2. The `floor` method

It is the opposite of the `ceil` method and it returns the floor value of the number, i.e it is rounded down to the previous integer value irrespective of whether the number after the decimal is less than 5 or greater than it. If the number passed to the `floor` method is an integer, it is returned unchanged. If a negative number is passed to the `floor` method, it rounds down the number, i.e it returns the next lesser integer number present on the number line.

### Time for an example:

``````import math

my_int = 4.5467
print (math.floor(my_int))

my_int = 4.9467
print (math.floor(my_int))

my_int = 4
print (math.floor(my_int))

my_int = -4.5467
print (math.floor(my_int))``````

Output:

```4
4
4
-5```

## 3. The `fabs` method

This method takes one value as an argument and returns the absolute value of the number, i.e it removes the negative (-) sign attached to any value and returns the value.

### Time for an example:

``````import math

my_int = 4.5467
print (math.fabs(my_int))

my_int = -4.5467
print (math.fabs(my_int))

my_int = 4
print (math.fabs(my_int))``````

Output:

```4.5467
4.5467
4.0```

## 4. The `copysign` method

This method takes 2 arguments as parameters. As the name suggests, this method copies the sign of the second parameter and attaches it to the first parameter and returns it as a floating-point value.

### Time for an example:

``````import math

my_val_1 = 8
my_val_2 = -6.9
print(math.copysign(my_val_1, my_val_2))

my_val_1 = -6.9
my_val_2 = 8
print(math.copysign(my_val_1, my_val_2))``````

Output:

```-8.0
6.9```

## Conclusion

In this post, we understood how the `ceil()`, `floor()`, `fabs()` and the `copysign()` methods of the math module works.