{"id":3046,"date":"2017-05-31T21:04:52","date_gmt":"2017-05-31T20:04:52","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=3046"},"modified":"2017-05-31T21:06:38","modified_gmt":"2017-05-31T20:06:38","slug":"c-guns-nan-and-min-max","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=3046","title":{"rendered":"C++ Guns - NaN and min() max()"},"content":{"rendered":"<p>As I told you in my last post, Fortran sucks! This is the C++ Version and its only half as long.<\/p>\n<pre><code>\r\n#include &ltiostream>\r\n#include &ltlimits>\r\n#include &ltcmath>\r\nusing namespace std;\r\n\r\ntemplate&lttypename Func> \r\nvoid test(Func func, string funcname, double a, double b, double x) {\r\n  double res = func(a,b);\r\n  cout << funcname << \"(\" << a << \",\" << b << \") = \" << res << \"\\n\";\r\n  \r\n  if(isnan(x)) {\r\n    if(not isnan(res)) {\r\n      cout << \"But should be \" << x << \"\\n\";\r\n    }\r\n  } else if(res != x) {\r\n    cout << \"But should be \" << x << \"\\n\";\r\n  }\r\n}\r\n\r\nint main() {\r\n  double NaN = numeric_limits&ltdouble>::quiet_NaN();\r\n  test(min&ltdouble>, \"min\", -3.0, NaN, -3.0);\r\n  test(min&ltdouble>, \"min\", NAN, -3.0, -3.0);\r\n  test(min&ltdouble>, \"min\", NAN,  NAN, NAN);\r\n  \r\n  test(max&ltdouble>, \"max\", -3.0, NAN, -3.0);\r\n  test(max&ltdouble>, \"max\", NAN, -3.0, -3.0);\r\n  test(max&ltdouble>, \"max\", NAN,  NAN, NAN);\r\n}\r\n<\/code><\/pre>\n<p>The ugly procedure pointer is replaced with a simple template. But std::min() and std::max() are also template functions. So we must give the compiler a hint which one we want. We want std::min() for doubles. The code is fine, nothing more to say. Except it dosent work as expected ;)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I told you in my last post, Fortran sucks! This is the C++ Version and its only half as long. #include &#038;ltiostream> #include &#038;ltlimits> #include &#038;ltcmath> using namespace std; template&#038;lttypename Func> void test(Func func, string funcname, double a, double b, double x) { double res = func(a,b); cout<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[17],"class_list":["post-3046","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-cpp"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/3046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3046"}],"version-history":[{"count":6,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/3046\/revisions"}],"predecessor-version":[{"id":3052,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/3046\/revisions\/3052"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3046"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}