Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
isgreater() in C/C++
In C programming, the isgreater() function is a type-generic macro used to compare two floating-point values safely. It is declared in the <math.h> header file and returns a non-zero value (true) if the first argument is greater than the second, otherwise returns 0 (false). Unlike the regular > operator, isgreater() never raises floating-point exceptions.
Syntax
int isgreater(x, y);
Parameters:
- x − First floating-point value to compare
- y − Second floating-point value to compare
Return Value: Returns 1 if x is greater than y, otherwise returns 0.
Example 1: Basic Usage with Float Values
Here's a simple example demonstrating isgreater() with floating-point numbers −
#include <stdio.h>
#include <math.h>
int main() {
float val1 = 28.5;
float val2 = 8.3;
int result;
result = isgreater(val1, val2);
printf("isgreater(%.1f, %.1f) = %d\n", val1, val2, result);
result = isgreater(val2, val1);
printf("isgreater(%.1f, %.1f) = %d\n", val2, val1, result);
return 0;
}
isgreater(28.5, 8.3) = 1 isgreater(8.3, 28.5) = 0
Example 2: Comparing with Special Values
The isgreater() function handles special floating-point values like NaN safely −
#include <stdio.h>
#include <math.h>
int main() {
double a = 5.0;
double b = 3.0;
double nan_val = 0.0/0.0; // NaN
printf("isgreater(5.0, 3.0) = %d\n", isgreater(a, b));
printf("isgreater(3.0, 5.0) = %d\n", isgreater(b, a));
printf("isgreater(5.0, NaN) = %d\n", isgreater(a, nan_val));
printf("isgreater(NaN, 5.0) = %d\n", isgreater(nan_val, a));
return 0;
}
isgreater(5.0, 3.0) = 1 isgreater(3.0, 5.0) = 0 isgreater(5.0, NaN) = 0 isgreater(NaN, 5.0) = 0
Key Points
- The
isgreater()function is exception-safe and never raises floating-point exceptions. - It returns 0 when either operand is NaN, making it safer than the
>operator. - It works with float, double, and long double types.
- The function is implemented as a type-generic macro.
Conclusion
The isgreater() function provides a safe way to compare floating-point values without triggering exceptions. It is particularly useful when dealing with special values like NaN or when exception-safe code is required.
