# Nakafa Framework: LLM
URL: /en/subject/university/bachelor/ai-ds/ai-programming/math-function
Source: https://raw.githubusercontent.com/nakafaai/nakafa.com/refs/heads/main/packages/contents/subject/university/bachelor/ai-ds/ai-programming/math-function/en.mdx
Output docs content for large language models.
---
export const metadata = {
    title: "Mathematical Functions",
    description: "Learn how to use Python's math module for sqrt(), round(), abs() functions, and pi constant. Complete guide to importing and practical usage.",
    authors: [{ name: "Nabil Akbarazzima Fatih" }],
    date: "09/16/2025",
    subject: "AI Programming",
};
## Module Concept in Python
Python is designed as a simple programming language with a small core language that can be greatly extended through modules. Think of Python like a house with a small living room, but with many additional rooms that you can open as needed. Modules are those additional rooms that contain special functions and constants.
A module is a unit of Python code organization that is loaded through the import process. When you need special mathematical functions, you don't need to create them from scratch, but simply import a module that already provides those functions.
## The math Module for Mathematical Functions
The `math` module provides mathematical functions that are useful for `int` and `float` data types. Meanwhile, for calculations involving complex numbers, Python provides the `cmath` module that supports `int`, `float`, and `complex` data types.
## How to Import the math Module
Python offers several ways to import modules, each with different characteristics and usage.
### Basic Import with Namespace
The most basic way to import a module is using the `import math` command. This way, all function and constant names remain bound to the `math` namespace.
### Import with Alias
To make code more concise, you can give an alias to the imported module. This is very useful when the module name is long or frequently used.
### Import All Functions
This method imports all names from the `math` module to the global namespace so they can be accessed directly without a prefix. Although practical for interactive sessions, this method is not recommended for complex Python programs.
Using `from math import *` has several drawbacks that you need to understand. First, there's a risk of name conflicts if you import multiple modules that have functions with the same name. Second, it's difficult to trace the origin of specific functions when reading code. Third, code becomes more difficult to maintain in the long term.
### Import Specific Functions
A more selective approach is to import only the specific functions needed. This provides direct access without a prefix while maintaining code clarity.
### Import with Function Alias
You can also give an alias to specific functions when importing them. This is useful for making function names more concise or avoiding name conflicts.
## Built-in Functions vs Module Functions
Python provides several basic mathematical functions as built-in functions that can be used directly without import. Examples are `abs()` and `round()`. These built-in functions demonstrate the concept of function overloading, where one function name can handle various data types.
### Absolute Value Function
The `abs()` function returns the absolute value of a number. This function can handle integers, floats, and even complex numbers.
For complex numbers, the `abs()` function calculates the modulus or distance from the origin point in the complex plane. The result for `4 + 3j` is  because .
### Rounding Function
The `round()` function rounds floating-point numbers to the nearest integer using the Banker's rounding system. In this system, numbers that are exactly in the middle (like 0.5) are rounded to the nearest even number.
Note that `round(4.5)` produces 4, not 5. This is because Python uses Banker's rounding which rounds to the nearest even number for cases exactly in the middle. The `round()` function also accepts a second parameter to determine the number of decimal digits or rounding position.
## Functions and Constants in the math Module
The `math` module provides various mathematical functions and constants that are frequently used in scientific and technical calculations.
### Square Root and Logarithm Functions
### Trigonometry Functions
### Ceiling Floor and Factorial Functions
= x
print(f"ceil(4.7): {math.ceil(number)}")    # Output: 5
# Floor: largest integer <= x
print(f"floor(4.7): {math.floor(number)}")  # Output: 4
# Factorial
print(f"5!: {math.factorial(5)}")  # Output: 120`
    }
  ]}
/>
### Mathematical Constants
The `math` module also provides important mathematical constants that are frequently used in calculations.
## Choosing the Right Import Method
The choice of the right import method depends on the usage context and complexity of the program you're creating.
### Guide to Choosing Import Methods
1. **Use `import math`** for programs that need many mathematical functions and you want clarity of function origin
2. **Use `import math as m`** when you frequently use mathematical functions and want more concise code
3. **Use `from math import sqrt, pi`** when you only need a few specific functions
4. **Avoid `from math import *`** in production programs because it can cause name conflicts and reduce code readability
### Implementation in Real Applications
By mastering various import methods and functions in the `math` module like `sqrt()`, `round()`, trigonometry functions, and the `pi` constant, you can build applications that perform complex calculations with clean and efficient code. The choice of the right import method will determine how easy your code is to read and maintain in the long term.