C fmax() Function

The fmax() function in C is a standard library function that returns the maximum of two given floating-point numbers. It is part of the C standard library (math.h). This function is useful for performing mathematical operations where the maximum value between two numbers is required.

Table of Contents

  1. Introduction
  2. fmax() Function Syntax
  3. Understanding fmax() Function
  4. Examples
    • Finding the Maximum Value
    • Using fmax() with User Input
  5. Real-World Use Case
  6. Conclusion

Introduction

The fmax() function calculates the maximum of two given floating-point numbers ( x ) and ( y ). This function is widely used in mathematical computations and applications where the maximum value between two numbers is required.

fmax() Function Syntax

The syntax for the fmax() function is as follows:

#include <math.h>
double fmax(double x, double y);

Parameters:

  • x: The first floating-point value.
  • y: The second floating-point value.

Returns:

  • The function returns the maximum of x and y.

Understanding fmax() Function

The fmax() function takes two floating-point numbers ( x ) and ( y ) and returns the larger of the two. If either value is NaN (Not-a-Number), the function returns the other value. If both values are NaN, the function returns NaN.

Examples

Finding the Maximum Value

To demonstrate how to use fmax() to find the maximum value between two numbers, we will write a simple program.

Example

#include <stdio.h>
#include <math.h>

int main() {
    double x = 5.7;
    double y = 3.2;

    // Compute the maximum value
    double result = fmax(x, y);

    // Print the result
    printf("Maximum value between %.2f and %.2f is: %.2f\n", x, y, result);

    return 0;
}

Output:

Maximum value between 5.70 and 3.20 is: 5.70

Using fmax() with User Input

This example shows how to use fmax() to find the maximum value between two numbers provided by the user.

Example

#include <stdio.h>
#include <math.h>

int main() {
    double x, y;

    // Get user input for the values
    printf("Enter the value for x: ");
    scanf("%lf", &x);
    printf("Enter the value for y: ");
    scanf("%lf", &y);

    // Compute the maximum value
    double result = fmax(x, y);

    // Print the result
    printf("Maximum value between %.2f and %.2f is: %.2f\n", x, y, result);

    return 0;
}

Output (example user input x "2.5" and y "3.8"):

Enter the value for x: 2.5
Enter the value for y: 3.8
Maximum value between 2.50 and 3.80 is: 3.80

Real-World Use Case

Determining Maximum Scores in a Game

In real-world applications, the fmax() function can be used to determine the maximum score between two players in a game, ensuring that the highest score is always identified.

Example: Determining Maximum Score

#include <stdio.h>
#include <math.h>

int main() {
    double player1_score, player2_score;
    double max_score;

    // Get user input for the scores of the two players
    printf("Enter the score for Player 1: ");
    scanf("%lf", &player1_score);
    printf("Enter the score for Player 2: ");
    scanf("%lf", &player2_score);

    // Determine the maximum score
    max_score = fmax(player1_score, player2_score);

    // Print the result
    printf("The maximum score between Player 1 and Player 2 is: %.2f\n", max_score);

    return 0;
}

Output (example user input player1_score "85.5" and player2_score "92.0"):

Enter the score for Player 1: 85.5
Enter the score for Player 2: 92.0
The maximum score between Player 1 and Player 2 is: 92.00

Conclusion

The fmax() function is essential for finding the maximum value between two floating-point numbers in C. It is useful in various mathematical calculations, particularly in fields like finance, engineering, and gaming, where determining the maximum value is important.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare