{"id":77,"date":"2007-04-21T09:33:16","date_gmt":"2007-04-21T13:33:16","guid":{"rendered":"http:\/\/jameslao.wordpress.com\/2007\/04\/21\/damn-numerical-error-in-fortran"},"modified":"2007-04-21T09:33:16","modified_gmt":"2007-04-21T13:33:16","slug":"damn-numerical-error-in-fortran","status":"publish","type":"post","link":"https:\/\/www.jlao.net\/en\/technology\/77\/","title":{"rendered":"damn numerical error in Fortran"},"content":{"rendered":"<div id=\"msgcns!A6B6FF1C63DF7D01!273\" class=\"bvMsg\">\n<div>(reference to <a href=\"https:\/\/www.cemtach.com\/reference\/software\/toyFDTD\/fpu.html\">https:\/\/www.cemtach.com\/reference\/software\/toyFDTD\/fpu.html<\/a> )<\/div>\n<div>if we run the following simple code:<\/div>\n<div>\n<table style=\"border-collapse:collapse;border:medium none;\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"284\" valign=\"top\">F90:<\/p>\n<p>program foo<br \/>\nreal*8 :: a = 0<br \/>\ninteger :: n<br \/>\ndo n=0,100,1<br \/>\nprint*,a<br \/>\na = a + 0.01<br \/>\nend do<br \/>\nend program foo<\/td>\n<td width=\"284\" valign=\"top\">C:<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\nvoid main(void)<br \/>\n{<br \/>\ndouble a = 0;<br \/>\nint n;<br \/>\nfor(n=0; n&lt;=100; n++)<br \/>\n{<br \/>\nprintf(&#8221; %.10f n&#8221;,a);<br \/>\na += 0.01;<br \/>\n}<br \/>\n}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>we may find different outputs from different compilers:<\/div>\n<div>\n<table style=\"border-collapse:collapse;border:medium none;\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"189\" valign=\"top\">Intel Fortran<\/td>\n<td width=\"189\" valign=\"top\">g95<\/td>\n<td width=\"189\" valign=\"top\">Gcc<\/td>\n<\/tr>\n<tr>\n<td width=\"189\" valign=\"top\">0.909999979659915<br \/>\n0.919999979436398<br \/>\n0.929999979212880<br \/>\n0.939999978989363<br \/>\n0.949999978765845<br \/>\n0.959999978542328<br \/>\n0.969999978318810<br \/>\n0.979999978095293<br \/>\n0.989999977871776<br \/>\n0.999999977648258<\/td>\n<td width=\"189\" valign=\"top\">0.909999979659915<br \/>\n0.9199999794363976<br \/>\n0.9299999792128801<br \/>\n0.9399999789893627<br \/>\n0.9499999787658453<br \/>\n0.9599999785423279<br \/>\n0.9699999783188105<br \/>\n0.979999978095293<br \/>\n0.9899999778717756<br \/>\n0.9999999776482582<\/td>\n<td width=\"189\" valign=\"top\">0.9100000000<br \/>\n0.9200000000<br \/>\n0.9300000000<br \/>\n0.9400000000<br \/>\n0.9500000000<br \/>\n0.9600000000<br \/>\n0.9700000000<br \/>\n0.9800000000<br \/>\n0.9900000000<br \/>\n1.0000000000<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>if we change that +0.01 to +0.01d0, it will improve the accuracy but still unsatisfactory<\/div>\n<div>\n<table style=\"border-collapse:collapse;border:medium none;\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"281\" valign=\"top\">Ifort  real*16<\/td>\n<td width=\"143\" valign=\"top\">Ifort real*8<\/td>\n<td width=\"144\" valign=\"top\">G95 real*8<\/td>\n<\/tr>\n<tr>\n<td width=\"281\" valign=\"top\">0.910000000000000018943180357666733<br \/>\n0.920000000000000019151347174783950 0.930000000000000019359513991901167 0.940000000000000019567680809018384 0.950000000000000019775847626135601 0.960000000000000019984014443252818 0.970000000000000020192181260370035 0.980000000000000020400348077487251 0.990000000000000020608514894604468 1.00000000000000002081668171172169<\/td>\n<td width=\"143\" valign=\"top\">0.910000000000001<br \/>\n0.920000000000001<br \/>\n0.930000000000001<br \/>\n0.940000000000001<br \/>\n0.950000000000001<br \/>\n0.960000000000001<br \/>\n0.970000000000001<br \/>\n0.980000000000001<br \/>\n0.990000000000001<br \/>\n1.00000000000000<\/td>\n<td width=\"144\" valign=\"top\">0.9100000000000006<br \/>\n0.9200000000000006<br \/>\n0.9300000000000006<br \/>\n0.9400000000000006<br \/>\n0.9500000000000006<br \/>\n0.9600000000000006<br \/>\n0.9700000000000006<br \/>\n0.9800000000000006<br \/>\n0.9900000000000007<br \/>\n1.0000000000000007<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>so.. god bless fortran&#8230;<\/div>\n<div><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>(reference to https:\/\/www.cemtach.com\/reference\/software\/toyFDTD\/fpu.html ) if we run the following simple code: F90: program foo real*8 :: a = 0 integer :: n do n=0,100,1 print*,a a = a + 0.01 end do end program foo C: #include &lt;stdio.h&gt; void main(void) { double a = 0; int n; for(n=0; n&lt;=100; n++) { printf(&#8221; %.10f n&#8221;,a); a &hellip; <a href=\"https:\/\/www.jlao.net\/en\/technology\/77\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">damn numerical error in Fortran<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":0,"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jlao.net\/en\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}