{"id":743,"date":"2017-08-13T14:03:59","date_gmt":"2017-08-13T14:03:59","guid":{"rendered":"http:\/\/people.utm.my\/kohmenghock\/?page_id=743"},"modified":"2017-08-13T14:05:17","modified_gmt":"2017-08-13T14:05:17","slug":"codeblocks-simple-guide-simple-mathematical-operations-ii","status":"publish","type":"page","link":"https:\/\/people.utm.my\/kohmenghock\/codeblocks-simple-guide-simple-mathematical-operations-ii\/","title":{"rendered":"Code::Blocks simple guide &#8211; simple mathematical operations II"},"content":{"rendered":"<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nIn our previous discussion, we stopped at an example of implementing mathematical operations. However, I mentioned that there were some mistakes in the code. Did you managed to spot those mistakes?<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nI&#8217;m sure you can easily detect the first one. The results for the line<\/span><\/p>\n<p style=\"text-align: center\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\">e = a \/ b<\/span><\/p>\n<p style=\"text-align: left\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nyields e = 0 in the terminal. With a = 2 and b = 3, the output on the terminal is clearly wrong. Why does this happen then?<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nThe cause of this error is due to the fact that we have declare the variable &#8220;e&#8221; as integer. If we perform this calculation on a calculator, we will get e = 0.66667. However, the code only retrieved the first number before the decimal point and thus results in a wrong output. In order to rectify this, we just need to change the variable type of &#8220;e&#8221; to real number.<br \/>\n<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nAt the same time, we also need to convert variable &#8220;a&#8221; and &#8220;b&#8221; to real numbers before performing the mathematical operation. We do this by using the command<\/span><\/p>\n<p style=\"text-align: center\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><span style=\"color: #ff0000\"><strong>real(a)<\/strong><\/span> and <span style=\"color: #ff0000\"><strong>real(b)<\/strong><\/span><\/span><\/p>\n<p><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nTo allow for comparison between the previous &#8220;wrong&#8221; output to the one obtained after the modification, we will now attribute the same operation to a new variable &#8220;g&#8221; such that:<\/span><\/p>\n<p style=\"text-align: center\"><strong><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\">\u00a0g = real(a) \/ real(b)<\/span><\/strong><\/p>\n<p><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nThe results obtained after executing the code will be given at the end of this discussion. I will now discuss about the second error in the previous code. You may well expect it by now. The error is with the output of variable &#8220;f&#8221;. The second term on the right hand side of<\/span><\/p>\n<p style=\"text-align: center\"><strong><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\">f = a**2 + b**(3\/2)<\/span><\/strong><\/p>\n<p><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nyields a non-integer value. Therefore, using an integer type variable is not correct to begin with. In that case, we need to declare a real type variable for this operation.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nOther than having to change the variable type as we did for the first error above, we also need to pay attention to the following differences:<\/span><\/p>\n<p style=\"text-align: center\"><strong><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\">h = real(a)**2 + real(b)**(3\/2)\u00a0<\/span><\/strong><\/p>\n<p style=\"text-align: center\"><strong><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\">h = real(a)**2.0 + real(b)**(3.0\/2.0)\u00a0<\/span><\/strong><\/p>\n<p><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nThe first line will not give you the correct result. Only the second line returns a correct value. Since variable &#8220;h&#8221; is of a real type, therefore the exponents should also be in <strong>real numbers<\/strong>.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nThe following is the output printed on the terminal after amendments to the previous main.f95 code. The modified version of the Fortran code can be found <a href=\"https:\/\/people.utm.my\/kohmenghock\/codeblocks-simple-guide-simple-mathematical-operations-ii\/example-of-fortran-code-on-basic-mathematical-operations\/\" target=\"_blank\" rel=\"noopener\">here<\/a>. Note that the wrong values for &#8220;e&#8221; and &#8220;f&#8221; were retained for comparison to the correct &#8220;g&#8221; and &#8220;h&#8221; outputs.<br \/>\n<\/span><\/p>\n<p><a href=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/terminal_output_correct_version.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-744 size-large\" src=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/terminal_output_correct_version-1024x582.png\" alt=\"\" width=\"1024\" height=\"582\" srcset=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/terminal_output_correct_version-1024x582.png 1024w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/terminal_output_correct_version-300x170.png 300w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/terminal_output_correct_version-768x436.png 768w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/terminal_output_correct_version.png 1348w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our previous discussion, we stopped at an example of implementing mathematical operations. However, I mentioned that there were some mistakes in the code. Did you managed to spot those mistakes? I&#8217;m sure you can easily detect the first one. The results for the line e = a \/ b yields e = 0 in &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/people.utm.my\/kohmenghock\/codeblocks-simple-guide-simple-mathematical-operations-ii\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Code::Blocks simple guide &#8211; simple mathematical operations II&#8221;<\/span><\/a><\/p>\n","protected":false},"author":14180,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-743","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/pages\/743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/users\/14180"}],"replies":[{"embeddable":true,"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/comments?post=743"}],"version-history":[{"count":0,"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/pages\/743\/revisions"}],"wp:attachment":[{"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/media?parent=743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}