{"id":1845,"date":"2015-10-06T11:43:29","date_gmt":"2015-10-06T09:43:29","guid":{"rendered":"http:\/\/www.blue.pri.ee\/ttu\/?page_id=1845"},"modified":"2025-09-26T16:45:04","modified_gmt":"2025-09-26T14:45:04","slug":"math-h-library","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/math-h-library\/","title":{"rendered":"math.h library"},"content":{"rendered":"<p>To help us solve mathematical functions, we can use a variety of libraries, depending on our goals. The main library is\u00a0 <span class=\"lang:c highlight:0 decode:true crayon-inline \">math.h<\/span>\u00a0 (<strong><a href=\"https:\/\/cppreference.com\/w\/c\/header\/math.html\">https:\/\/cppreference.com\/w\/c\/header\/math.html<\/a><\/strong>).<\/p>\n<p>If we also want to include math with complex numbers, we can use\u00a0 <span class=\"lang:c highlight:0 decode:true crayon-inline \">complex.h<\/span>\u00a0 and\/or\u00a0 <span class=\"lang:c highlight:0 decode:true crayon-inline\">tgmath.h<\/span>\u00a0 libraries.<\/p>\n<pre class=\"lang:c decode:true \">\/**\r\n * File:        math.c\r\n * Author:      Risto Heinsar\r\n * Created:     05.10.2015\r\n * Modified:    26.09.2021\r\n *\r\n * Description: Some example use of math.h library. \r\n * \r\n * Note:        To use math.h library You must compile the program with the\r\n *              linker flag \"-lm\", without quotes. In geany, you can add the -lm\r\n *              to build path under build -&gt; set build commands.\r\n *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\nint main(void)\r\n{\r\n    \/\/ Integer division leads to integer answers with decimal places truncated\r\n    printf(\"5 \/ 10 is %d\\n\", 5 \/ 10);\r\n    printf(\"10 \/ 10 is %d\\n\", 10 \/ 10);\r\n    printf(\"28 \/ 10 is %d\\n\\n\", 28 \/ 10);\r\n    \r\n    \/\/ To get deimal places, at least one operand has to be float\/double\r\n    printf(\"9.0 \/ 10 is %.2f\\n\", 9.0 \/ 10);\r\n    printf(\"9 \/ 10.0 is %.2f\\n\", 9 \/ 10.0);\r\n    printf(\"Using typecasting: 9 \/ 10 is %.2f\\n\\n\", 9 \/ (float)10);\r\n\r\n    float num1 = 9;\r\n    float num2 = -5.4f;\r\n    float num3 = pow(num1, 2);\r\n    \r\n    \/\/ Using math.h functions\r\n    printf(\"Num1 squared is %.2f\\n\", pow(num1, 2));\r\n    printf(\"Num1 squared can be also found like this: %.2f\\n\", num3);\r\n    printf(\"Number 7 to the power of 3 is: %.2f\\n\", pow(7, 3));\r\n    printf(\"The square root of num1 is: %.2f\\n\", sqrt(num1));\r\n    printf(\"Alternative square root: %.2f\\n\", pow(num1, 1.0\/2.0));\r\n    \r\n    float equation = num1 * pow(num2, 2) + sqrt(num1);\r\n    printf(\"The solution for the equation %.3f\\n\", equation);\r\n    \r\n    return 0;\r\n}\r\n<\/pre>\n<p>Notice the following<\/p>\n<ul>\n<li>When compiling the program, we must specify an additional build argument (-lm) to the compiler.\u00a0 This links the mathematics library to our program.<\/li>\n<li>Math functions typically return their result. If you want to use the result later on, you need to store the result in a variable. If however you only need it as a part of a larger equation, you can\u00a0 use it in-place without separately storing the result.<\/li>\n<li>To get n-th root, we can use the properties of power. Make sure to use floats for the division (e.g. 1.0 instead of 1). This is necessary due to integer division truncating the decimal places.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>To help us solve mathematical functions, we can use a variety of libraries, depending on our goals. The main library is\u00a0 math.h\u00a0 (https:\/\/cppreference.com\/w\/c\/header\/math.html). If we also want to include math with complex numbers, we can use\u00a0 complex.h\u00a0 and\/or\u00a0 tgmath.h\u00a0 libraries. \/** * File: math.c * Author: Risto Heinsar * Created: 05.10.2015 * Modified: 26.09.2021 * &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/math-h-library\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">math.h library<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1521,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width.php","meta":{"footnotes":""},"class_list":["post-1845","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/1845","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/comments?post=1845"}],"version-history":[{"count":2,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/1845\/revisions"}],"predecessor-version":[{"id":10723,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/1845\/revisions\/10723"}],"up":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/1521"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=1845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}