Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nextafter vs nexttoward functions in C++ 2011?

Tags:

What is the difference between the nextafter and the nexttoward functions of the C++ 2011 standard library ?

like image 726
Vincent Avatar asked Aug 21 '12 22:08

Vincent


2 Answers

Since the functions originate from C, they can't be overloaded, which means two different names for functions that do the same but have different parameter(-type)s. Here are the original signatures:

float nextafter(float, float);
float nexttoward(float, long double);

And now the standard just says there should be a few overloads to make things nicer in C++ (§26.8 [c.math] p11):

Moreover, there shall be additional overloads sufficient to ensure:

  1. If any argument corresponding to a double parameter has type long double, then all arguments corresponding to double parameters are effectively cast to long double.
  2. Otherwise, if any argument corresponding to a double parameter has type double or an integer type, then all arguments corresponding to double parameters are effectively cast to double.
  3. Otherwise, all arguments corresponding to double parameters are effectively cast to float.

See also: ISO C 7.5, 7.10.2, 7.10.6.

like image 124
Xeo Avatar answered Jan 08 '23 07:01

Xeo


Read man page:

The nexttoward() functions do the same as the nextafter() functions, except that they have a long double second argument.

like image 23
hired777 Avatar answered Jan 08 '23 07:01

hired777