What are Floating Point Functions?
Floating point functions are mathematical functions in C that operate on real numbers (decimal values). These functions are defined in the math.h header file and work with floating point types like float, double, and long double.
They are used for calculations involving non-integer values—such as computing square roots, powers, trigonometric functions, logarithms, and other advanced math operations.
Floating Point Data Types in C
Type | Description | Range (Approx.) |
---|---|---|
float | Single precision (32-bit) | ±3.4E−38 to ±3.4E+38 |
double | Double precision (64-bit) | ±1.7E−308 to ±1.7E+308 |
long double | Extended precision (platform-specific) | Larger than double |
Most math functions return a double, but C also provides specific versions for float and long double.
Header File: math.h
To use floating point functions, you must include the math.h header at the beginning of your program:
#include <math.h>
If you forget this, the compiler may throw an error or warning about undefined functions.
Common Floating Point Functions in C
Function | Description | Syntax |
---|---|---|
fabs(x) | Returns absolute value of x | double fabs(double x); |
sqrt(x) | Square root of x | double sqrt(double x); |
ceil(x) | Smallest integer ≥ x | double ceil(double x); |
floor(x) | Largest integer ≤ x | double floor(double x); |
round(x) | Rounds x to nearest integer | double round(double x); |
trunc(x) | Truncates decimal part | double trunc(double x); |
fmod(x, y) | Returns remainder of x/y | double fmod(double x, double y); |
copysign(x, y) | Returns x with the sign of y | double copysign(double x, double y); |
isnan(x) | Checks if x is Not-a-Number | int isnan(double x); |
isinf(x) | Checks if x is infinite | int isinf(double x); |
Example:
#include <stdio.h> #include <math.h> int main() { double quiz = -7.35, test = 16.0, mcq = 4.8; printf("fabs(quiz) = %.2f\n", fabs(quiz)); // Absolute printf("sqrt(test) = %.2f\n", sqrt(test)); // Square root printf("ceil(mcq) = %.2f\n", ceil(mcq)); // Ceil printf("floor(mcq) = %.2f\n", floor(mcq)); // Floor printf("fmod(test, mcq) = %.2f\n", fmod(test, mcq)); // Remainder return 0; }
Output:
fabs(quiz) = 7.35 sqrt(test) = 4.00 ceil(mcq) = 5.00 floor(mcq) = 4.00 fmod(test, mcq) = 2.40
This C program demonstrates the use of several math functions from the <math.h> library. It starts by calculating the absolute value of -7.35 using fabs(), which returns 7.35. Then it computes the square root of 16.0 with sqrt(), resulting in 4.0. The program also uses ceil() to round 4.8 up to 5.0 and floor() to round it down to 4.0. Finally, it calculates the remainder of dividing 16.0 by 4.8 using fmod(), which returns 2.4. The results are printed with two decimal places.
Trigonometric Functions in C
In C, trigonometric functions are part of the <math.h> library. These functions are used to perform operations on angles (in radians) and return floating-point results.
- sin(x) – Sine of angle x
- cos(x) – Cosine of angle x
- tan(x) – Tangent of angle x
Example:
#include <stdio.h> #include <math.h> int main() { double angleDeg = 30.0; double angleRad = angleDeg * M_PI / 180.0; printf("Angle in degrees: %.2f\n", angleDeg); printf("sin(%.2f rad) = %.4f\n", angleRad, sin(angleRad)); printf("cos(%.2f rad) = %.4f\n", angleRad, cos(angleRad)); printf("tan(%.2f rad) = %.4f\n", angleRad, tan(angleRad)); return 0; }
Output:
Angle in degrees: 30.00 sin(0.52 rad) = 0.5000 cos(0.52 rad) = 0.8660 tan(0.52 rad) = 0.5774
log() and log10() – Logarithms
In C, the <math.h> library provides logarithmic functions to compute the natural logarithm and base-10 logarithm of a number.
1. log() Function – Natural Logarithm
Returns the natural logarithm (base e) of a positive number.
Syntax:
double log(double x);
2. log10() Function – Base-10 Logarithm
Returns the logarithm of a number to the base 10.
Syntax:
double log10(double x);
Example:
#include <stdio.h> #include <math.h> int main() { double quizScore = 10.0; double testResult = log(quizScore); // Natural logarithm double certification = log10(quizScore); // Base-10 logarithm printf("log(%f) = %f\n", quizScore, testResult); printf("log10(%f) = %f\n", quizScore, certification); return 0; }
Output
log(10.000000) = 2.302585 log10(10.000000) = 1.000000
Note: Passing zero or a negative value to these functions results in domain errors (e.g., NaN or a runtime error depending on the system).
Common Pitfalls and How to Avoid Them
- Precision Loss: Be aware of the limitations of floating point precision. Use double or long double when higher precision is required.​
- Rounding Errors: Understand that operations may introduce rounding errors. Design algorithms that are tolerant to such inaccuracies.​​
- Domain Errors: Functions like sqrt and log have domain restrictions. Ensure input values are within valid ranges to avoid undefined behavior.​
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Practice BCA MCQs
- Apply for C Internship
- Practice Computer Science MCQs