{"id":1069,"date":"2008-02-18T17:31:29","date_gmt":"2008-02-18T08:31:29","guid":{"rendered":"http:\/\/na-inet.jp\/new-na-inet\/weblog\/2008\/02\/strange_phenomenon_of_cosine_f\/"},"modified":"2008-02-18T17:31:29","modified_gmt":"2008-02-18T08:31:29","slug":"strange_phenomenon_of_cosine_f","status":"publish","type":"post","link":"https:\/\/na-inet.jp\/weblog2\/2008\/02\/18\/strange_phenomenon_of_cosine_f\/","title":{"rendered":"Strange Phenomenon of Cosine function on Linux x86_64 + gcc env"},"content":{"rendered":"<p>The below article has been submitted to <a href=\"http:\/\/www.netlib.org\/na-digest-html\/\">NA Digest<\/a> (So, it would not be received &#8230;). Japanese version of that can be read <a href=\"http:\/\/na-inet.jp\/weblog\/archives\/001043.html\">here<\/a>.<br \/>\n&#8212;-<br \/>\nDear NA digest subscribers,<br \/>\nI am in trouble about the problem on cosine function in IEEE754 double precision.<br \/>\nOn Fedora Core 4 or CentOS 4\/5 x86_64 env, I ran the program which included the function below in 4 different rounding-modes (RN, RZ, RP, RM) in order to know amount of round-off error.<br \/>\n<strong>double cos_rmode(double x, int rmode)<br \/>\n{<br \/>\nint current_rmode;<br \/>\ndouble ret;<br \/>\ncurrent_rmode = fegetround(); \/\/ get current rounding mode<br \/>\nfesetround(rmode);            \/\/ change rounding mode<br \/>\nret = cos(x);                 \/\/ cosine function<br \/>\nfesetround(current_rmode);    \/\/ restore rounding mode<br \/>\nreturn ret;<br \/>\n}<br \/>\n<\/strong><br \/>\nAs a result, very different values of cosine func at x >> 1 are obtained like:<br \/>\n<strong>cos(  5.04710873550435011e+01) =<br \/>\nRN, RZ:   9.78937668119415738e-01,   9.78937668119415627e-01<br \/>\nRP, RM:   <u>5.55012195441045186e-01<\/u>,   9.78937668119415627e-01<br \/>\ncos(  5.48598775598298900e+01) =<br \/>\nRN, RZ:  -1.17720272477898139e-01,  <u>-3.04940663255730016e+00<\/u><br \/>\nRP, RM:  -1.17720272477898139e-01,  <u>-3.04940663255730016e+00<\/u>.<br \/>\n<\/strong>The underlined values above are incorrect.<br \/>\nIn the RN mode, the cosine function always returns correct values, but incorrect and very different values in other 3 modes. If you want to check this phenomenon on your env, you can download my sample program from:<br \/>\n<a href=\"http:\/\/na-inet.jp\/weblog\/archives\/test_cos.c\">http:\/\/na-inet.jp\/weblog\/archives\/test_cos.c<\/a><br \/>\nBy trying to run the above &#8220;test_cos.c&#8221; on 32bit and 64bit Linux environments around me, this phenomenon could not be found in 32bit env such as Pentium 3\/4 and Mac OS X 10.4 + Xcode + Core2Duo.<br \/>\nI will appreciate your sending the information about this problem if you have anything.<br \/>\nSincerely yours,<br \/>\n&#8212;<br \/>\nTomonori KOUYA <tkouya [ * } na-net.ornl.gov><br \/>\n<a href=\"http:\/\/na-inet.jp\/\">http:\/\/na-inet.jp\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The below article has been submitted to NA Digest (So, it would not be received &#8230;). Japanese version of  &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/na-inet.jp\/weblog2\/2008\/02\/18\/strange_phenomenon_of_cosine_f\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Strange Phenomenon of Cosine function on Linux x86_64 + gcc env&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[8],"tags":[],"class_list":["post-1069","post","type-post","status-publish","format-standard","hentry","category-articles-in-english"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/na-inet.jp\/weblog2\/wp-json\/wp\/v2\/posts\/1069","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/na-inet.jp\/weblog2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/na-inet.jp\/weblog2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/na-inet.jp\/weblog2\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/na-inet.jp\/weblog2\/wp-json\/wp\/v2\/comments?post=1069"}],"version-history":[{"count":0,"href":"https:\/\/na-inet.jp\/weblog2\/wp-json\/wp\/v2\/posts\/1069\/revisions"}],"wp:attachment":[{"href":"https:\/\/na-inet.jp\/weblog2\/wp-json\/wp\/v2\/media?parent=1069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/na-inet.jp\/weblog2\/wp-json\/wp\/v2\/categories?post=1069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/na-inet.jp\/weblog2\/wp-json\/wp\/v2\/tags?post=1069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}