C++ Floating Point Manipulation (fmod(), remainder(), remquo() … in cmath)
Like integers, C++11 introduced some basic inbuilt functions for handling simple mathematical computations of floating point numbers necessary for day to day programming as well as competitive programming. This article discusses some of the functions introduced.
1. fmod() : This function is used to return the remainder(modulus) of 2 floating point numbers mentioned in its arguments. The quotient computed is truncated.
2. remainder() : This function is also used to return the remainder(modulus) of 2 floating point numbers mentioned in its arguments.The quotient computed is rounded.
3. remquo() : This function returns the remainder and also stored remainder in variable reference passed as argument. This function takes 3 arguments, numerator, denominator and reference of variable where quotient has to be stored.
The remainder computed using fmod() is : 2.6 The remainder computed using remainder() is : -0.9 The remainder part of 9.6/3.5 is : -0.9 The quotient part of 9.6/3.5 is : 3
4. copysign() : This function returns a number with the magnitude to 1st argument and sign of 2nd argument.
5. nextafter() : This function computes the next representable value of 1st argument in the direction of 2nd argument.
The value returned after copysigning is : -9.6 The next value approximated is : -4.94066e-324
6. fmin() : Returns the smallest of two arguments.
7. fmax() : Returns the largest of two arguments.
8. fdim() : Returns the positive difference of the numbers passed as arguments.
9. fma() : This function takes 3 arguments and returns multiply-add “x*y+z” value after computing.
The largest of 2 numbers is : 2.5 The smallest of 2 numbers is : 2 The positive difference of 2 numbers is : 0.5 The multiply-add of 3 numbers is : 7.5
This article is contributed by Manjeet Singh(S. Nandini). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.