🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The fmod function in Python's math module is used to compute the remainder of the division of one number by another.
Table of Contents
- Introduction
- Importing the
mathModule fmodFunction Syntax- Examples
- Basic Usage
- Handling Negative Numbers
- Handling Edge Cases
- Real-World Use Case
- Conclusion
- Reference
Introduction
The fmod function in Python's math module allows you to calculate the remainder of the division of one floating-point number by another. Unlike the modulus operator (%), fmod returns a result with the same sign as the dividend.
This function is particularly useful in scenarios where you need the remainder after dividing two floating-point numbers, as it preserves the sign of the dividend.
Importing the math Module
Before using the fmod function, you need to import the math module.
import math
fmod Function Syntax
The syntax for the fmod function is as follows:
math.fmod(x, y)
Parameters:
x: The dividend.y: The divisor.
Returns:
- The remainder of
x / ywith the same sign asx.
Examples
Basic Usage
To demonstrate the basic usage of fmod, we will calculate the remainder of the division of a few numbers.
Example
import math
# Remainder of 7.5 divided by 2.5
result = math.fmod(7.5, 2.5)
print(result) # Output: 0.0
# Remainder of 7.5 divided by 2.3
result = math.fmod(7.5, 2.3)
print(result) # Output: 0.6
# Remainder of 9.3 divided by 3.1
result = math.fmod(9.3, 3.1)
print(result) # Output: 0.0
Output:
0.0
0.6000000000000005
4.440892098500626e-16
Handling Negative Numbers
This example demonstrates how fmod handles negative numbers by preserving the sign of the dividend.
Example
import math
# Remainder of -7.5 divided by 2.5
result = math.fmod(-7.5, 2.5)
print(result) # Output: -0.0
# Remainder of 7.5 divided by -2.5
result = math.fmod(7.5, -2.5)
print(result) # Output: 0.0
# Remainder of -9.3 divided by 3.1
result = math.fmod(-9.3, 3.1)
print(result) # Output: -0.0
Output:
-0.0
0.0
-4.440892098500626e-16
Handling Edge Cases
This example demonstrates how fmod handles special cases such as zero and very large values.
Example
import math
# Remainder of 0 divided by any number
result = math.fmod(0, 3.1)
print(result) # Output: 0.0
# Remainder of any number divided by a very large number
large_value = 1e10
result = math.fmod(5.5, large_value)
print(f"Remainder of 5.5 divided by a large number: {result}")
Output:
0.0
Remainder of 5.5 divided by a large number: 5.5
Real-World Use Case
Circular Motion: Calculating Angles
In physics, the fmod function can be used to calculate angles in circular motion, ensuring the angles remain within a standard range (e.g., 0 to 360 degrees).
Example
import math
# Angle in degrees
angle = 450
# Normalize the angle to be within 0 to 360 degrees
normalized_angle = math.fmod(angle, 360)
print(f"Normalized angle: {normalized_angle} degrees")
Output:
Normalized angle: 90.0 degrees
Conclusion
The fmod function in Python's math module is used for calculating the remainder of the division of two floating-point numbers. This function is useful in various numerical and data processing applications, particularly those involving calculations where the sign of the dividend needs to be preserved. Proper usage of this function can enhance the accuracy and efficiency of your computations.
Reference
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment