nextafter vs nexttoward functions in C++ 2011?

江枫思渺然 提交于 2019-12-06 17:14:37

问题


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


回答1:


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.




回答2:


Read man page:

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



来源:https://stackoverflow.com/questions/12063821/nextafter-vs-nexttoward-functions-in-c-2011

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!