{"id":81417,"date":"2021-07-12T09:00:12","date_gmt":"2021-07-12T03:30:12","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=81417"},"modified":"2021-07-12T09:00:12","modified_gmt":"2021-07-12T03:30:12","slug":"math-functions-in-cpp","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/","title":{"rendered":"Math Functions in C++"},"content":{"rendered":"<p>C++ offers a lot of helpful mathematical functions that facilitate various mathematical calculations. Rather than implementing those calculations, we can directly use these built-in functions to make our programs simpler.<\/p>\n<p>In order to use math functions in C++, we must include &lt;math.h&gt; or &lt;cmath&gt; header file in our program.<\/p>\n<p>In this article, we will discuss various math functions included in the C++ standard library.<\/p>\n<h3>Math Function in C++<\/h3>\n<p><strong>1. pow (base, exponent):<\/strong> We use pow() function to compute the value of base raised to exponent.<\/p>\n<p><strong>2. sqrt (parameter):<\/strong> It returns the square root of a number. The number entered as its argument cannot be negative.<\/p>\n<p><strong>3. abs (parameter):<\/strong> It takes an integer argument and returns its absolute value.<\/p>\n<p><strong>4. fabs (parameter):<\/strong> fabs() function, similar to abs(), returns absolute value of its argument. The difference is that we can also pass floating point values to it.<\/p>\n<p><strong>5. ceil (parameter):<\/strong> It returns the smallest integer which is greater than or equal to the argument passed.<\/p>\n<p><strong>6. floor (parameter):<\/strong> It returns the largest integer which is less than or equal to the argument passed.<\/p>\n<p><strong>7. log (parameter):<\/strong> It returns the base e logarithm value of the argument.<\/p>\n<p><strong>8. log10 (parameter):<\/strong> It returns the base 10 logarithm value of the argument.<\/p>\n<p><strong>9. exp (parameter):<\/strong> It returns the value of e (exponential) raised to the number entered as argument.<\/p>\n<p><strong>10. exp2 (parameter):<\/strong> It returns the base 2 exponential value i.e., 2 raised to the number entered as argument.<\/p>\n<p><strong>11. hypot (side1, side2):<\/strong> It takes two arguments as two sides of a right angled triangle and computes the value of hypotenuse.<\/p>\n<p><strong>12. fmod (numerator, denominator):<\/strong> It computes the floating point remainder when numerator \/ denominator.<\/p>\n<p><strong>13. modf (number, *ptr):<\/strong> modf() divides a number into integral and fractional parts. The function returns the fractional part, while the integer part is saved in the address pointed to by the pointer supplied as second argument to modf().<\/p>\n<p><strong>14. round (parameter):<\/strong> It returns the integral value that is closest to the argument, rounded away from zero in midway cases.<\/p>\n<p><strong>15. trunc (parameter):<\/strong> It returns the closest integral number that is not larger than the parameter, rounding it towards zero.<\/p>\n<p><strong>16. sin (parameter):<\/strong> It computes the sine of the angle in radians passed as argument.<\/p>\n<p><strong>17. cos (parameter):<\/strong> It computes the cosine of the angle in radians passed as argument.<\/p>\n<p><strong>18. tan (parameter):<\/strong> It computes the tangent of the angle in radians passed as argument.<\/p>\n<p><strong>19. asin (parameter):<\/strong> It computes the inverse sine of the argument. The range of argument is [-1,1] and the result is obtained in radians.<\/p>\n<p><strong>20. acos (parameter):<\/strong> It computes the inverse cosine of the argument. The range of argument is [-1,1] and the result is obtained in radians.<\/p>\n<p><strong>21. atan (parameter):<\/strong> It computes the inverse tangent of the argument. The argument can be any number and the result is obtained in radians.<\/p>\n<p><strong>22. atan2 (parameter1, parameter2):<\/strong> It computes inverse tangent of parameter1\/parameter2<\/p>\n<p><strong>23. sinh (parameter):<\/strong> It computes the hyperbolic sine of the angle in radians passed as argument.<\/p>\n<p><strong>24. cosh (parameter):<\/strong> It computes the hyperbolic cosine of the angle in radians passed as argument.<\/p>\n<p><strong>25. tanh (parameter):<\/strong> It computes the hyperbolic tangent of the angle in radians passed as argument.<\/p>\n<p><strong>Example of math functions in C++<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\n#include &lt;math.h&gt;\nusing namespace std;\n\nint main() {\n  double x = 4.6, y = 34;\n    double result;\n    result = pow(x, y);\n    cout&lt;&lt;\"4.6 raised to 34 = \"&lt;&lt;result&lt;&lt;endl;\n    \n    result = sqrt(y);\n    cout&lt;&lt;\"square root of 34 = \"&lt;&lt;result&lt;&lt;endl;\n    \n    cout&lt;&lt;\"absolute value of 4.6 = \"&lt;&lt;fabs(x)&lt;&lt;endl;\n    \n    cout&lt;&lt;\"ceil value of 11.8 = \"&lt;&lt;ceil(11.8)&lt;&lt;endl;\n    cout&lt;&lt;\"floor value of 11.8 = \"&lt;&lt;floor(11.8)&lt;&lt;endl;\n    \n    cout&lt;&lt;\"log of 1000 (base 10) = \"&lt;&lt;log10(1000)&lt;&lt;endl;\n    \n    cout&lt;&lt;\"exponential value of 4.6 = \"&lt;&lt;exp(x)&lt;&lt;endl;\n    \n    double angle = 45;\n    cout&lt;&lt;\"sin 45 = \"&lt;&lt;sin(angle)&lt;&lt;endl;\n    \n    int n = 1;\n    cout&lt;&lt;\"arc tan of 1 = \"&lt;&lt;atan(n)&lt;&lt;endl;\n    \n    cout&lt;&lt;\"cosh 45 = \"&lt;&lt;cosh(angle);\n  return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">4.6 raised to 34 = 3.41795e+22<br \/>\nsquare root of 34 = 5.83095<br \/>\nabsolute value of 4.6 = 4.6<br \/>\nceil value of 11.8 = 12<br \/>\nfloor value of 11.8 = 11<br \/>\nlog of 1000 (base 10) = 3<br \/>\nexponential value of 4.6 = 99.4843<br \/>\nsin 45 = 0.850904<br \/>\narc tan of 1 = 0.785398<br \/>\ncosh 45 = 1.74671e+19<\/div>\n<h3>Summary<\/h3>\n<p>In this article, we studied various useful mathematical functions offered by C++. We can directly use them in our program wherever required by including &lt;math.h&gt; or &lt;cmath&gt; header file. We also learnt how to use these functions with an example.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++ offers a lot of helpful mathematical functions that facilitate various mathematical calculations. Rather than implementing those calculations, we can directly use these built-in functions to make our programs simpler. In order to use&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":81756,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3405],"tags":[3710],"class_list":["post-81417","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-math-functions-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Math Functions in C++ - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn about various math functions included in the C++ standard library with their syntax and examples for better understanding.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Math Functions in C++ - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn about various math functions included in the C++ standard library with their syntax and examples for better understanding.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-12T03:30:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Math-Functions-in-C.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Math Functions in C++ - TechVidvan","description":"Learn about various math functions included in the C++ standard library with their syntax and examples for better understanding.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/","og_locale":"en_US","og_type":"article","og_title":"Math Functions in C++ - TechVidvan","og_description":"Learn about various math functions included in the C++ standard library with their syntax and examples for better understanding.","og_url":"https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-07-12T03:30:12+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Math-Functions-in-C.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Math Functions in C++","datePublished":"2021-07-12T03:30:12+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/"},"wordCount":596,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Math-Functions-in-C.jpg","keywords":["Math Functions in C++"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/","url":"https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/","name":"Math Functions in C++ - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Math-Functions-in-C.jpg","datePublished":"2021-07-12T03:30:12+00:00","description":"Learn about various math functions included in the C++ standard library with their syntax and examples for better understanding.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Math-Functions-in-C.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Math-Functions-in-C.jpg","width":1200,"height":628,"caption":"Math Functions in C++"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/math-functions-in-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Math Functions in C++"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=81417"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81417\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/81756"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=81417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=81417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=81417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}