![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
fmax() and fmin() in C++
In this section we will see how to convert fmax() and fmin() in C++. The fmax() and fmin() are present in the cmath header file.
This function takes two values of type float, or double or long double and returns maximum or minimum using fmax() and fmin() respectively.
If the argument types are different, like if someone wants to compare float and double, or long double with float, then the function typecasts into that value implicitly, then returns the corresponding value.
Example
#include <cmath> #include <iomanip> #include <iostream> using namespace std; main() { double res; //uses of fmax() res = fmax(50.0, 10.0); //compare for both positive value cout << fixed << setprecision(4) << "fmax(50.0, 10.0) = " << res << endl; res = fmax(-50.0, 10.0); //comparison between opposite sign cout << fixed << setprecision(4) << "fmax(-50.0, 10.0) = " << res << endl; res = fmax(-50.0, -10.0); //compare when both are negative cout << fixed << setprecision(4) << "fmax(-50.0, -10.0) = " << res << endl; //uses of fmin() res = fmin(50.0, 10.0); //compare for both positive value cout << fixed << setprecision(4) << "fmin(50.0, 10.0) = " << res << endl; res = fmin(-50.0, 10.0); //comparison between opposite sign cout << fixed << setprecision(4) << "fmin(-50.0, 10.0) = " << res << endl; res = fmin(-50.0, -10.0); //compare when both are negative cout << fixed << setprecision(4) << "fmin(-50.0, -10.0) = " << res << endl; }
Output
fmax(50.0, 10.0) = 50.0000 fmax(-50.0, 10.0) = 10.0000 fmax(-50.0, -10.0) = -10.0000 fmin(50.0, 10.0) = 10.0000 fmin(-50.0, 10.0) = -50.0000 fmin(-50.0, -10.0) = -50.0000
Advertisements