{"id":68169,"date":"2021-01-05T08:54:53","date_gmt":"2021-01-05T14:54:53","guid":{"rendered":"https:\/\/www.johndcook.com\/blog\/?p=68169"},"modified":"2021-01-06T06:07:28","modified_gmt":"2021-01-06T12:07:28","slug":"bootstrapping-math-library","status":"publish","type":"post","link":"https:\/\/www.johndcook.com\/blog\/2021\/01\/05\/bootstrapping-math-library\/","title":{"rendered":"Bootstrapping a minimal math library"},"content":{"rendered":"<p>Sometimes you don&#8217;t have all the math functions available that you would like. For example, maybe you have a way to calculate natural logs but you would like to calculate a log base 10.<\/p>\n<p>The Unix utility <code>bc<\/code> is a prime example of this. It only includes six common math functions:<\/p>\n<ul>\n<li>sine<\/li>\n<li>cosine<\/li>\n<li>arctangent<\/li>\n<li>natural log<\/li>\n<li>exp<\/li>\n<li>square root<\/li>\n<\/ul>\n<p>Users are expected to know how to calculate anything else they need from there. (Inexplicably, <code>bc<\/code> also includes a way to calculate Bessel functions.)<\/p>\n<p>This post collects formulas that let you bootstrap the functions listed above into all the trig and hyperbolic functions and their inverses.<\/p>\n<h2>Logarithms<\/h2>\n<p>Most programming languages provide a way to compute natural logs but not logs in bases other than <em>e<\/em>. If you have a way to compute logs in base <em>b<\/em>, you can compute logs in any other base via<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium aligncenter\" style=\"background-color: white;\" src=\"https:\/\/www.johndcook.com\/bootstrap_log.svg\" alt=\"\\log_a(z) = \\frac{\\log_b z}{\\log_b a}\" width=\"120\" height=\"41\" \/><\/p>\n<p>So, for example, you could compute the log base 10 of a number by computing its natural log and dividing by the natural log of 10.<\/p>\n<h2>Exponents<\/h2>\n<p>If you have a way to calculate <em>e<\/em><sup><em>x<\/em><\/sup> and natural logs, you can compute <em>x<\/em><sup><em>y<\/em><\/sup> via<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium aligncenter\" style=\"background-color: white;\" src=\"https:\/\/www.johndcook.com\/bootstrap_exponent.svg\" alt=\"x^y = \\exp(y \\log x)\" width=\"130\" height=\"18\" \/><\/p>\n<p>Since square roots correspond to exponent 1\/2, you can use this to compute square roots.<\/p>\n<h2>Trig functions<\/h2>\n<p>If you have a way to calculate sine and cosine, you can calculate the rest of the <a href=\"https:\/\/www.johndcook.com\/blog\/2009\/09\/25\/how-many-trig-functions\/\">six<\/a> standard trig functions.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium aligncenter\" style=\"background-color: white;\" src=\"https:\/\/www.johndcook.com\/bootstrap_trig.svg\" alt=\"\\begin{align*} \\sec z &amp;= 1\/\\cos z \\\\ \\csc z &amp;= 1\/\\sin z \\\\ \\tan z &amp;= \\sin z \/ \\cos z \\\\ \\cot z &amp;= \\cos z \/ \\sin z \\end{align*}\" width=\"142\" height=\"97\" \/><\/p>\n<h2>Inverse trig functions<\/h2>\n<p>If you have a way to calculate inverse tangent, you can bootstrap it to compute the rest of the inverse trig functions.<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" style=\"background-color: white;\" src=\"https:\/\/www.johndcook.com\/bootstrap_arctan.svg\" alt=\"\\begin{align*} \\text{arcsin}(x) &amp;= \\arctan(x \/ \\sqrt{1 - x^2}) \\\\ \\text{arccos}(x) &amp;= \\arctan(\\sqrt{1 - x^2 }\/ x) \\\\ \\text{arccot}(x) &amp;= \\pi\/2 - \\arctan(x) \\\\ \\text{arcsec}(x) &amp;= \\arctan(\\sqrt{x^2 - 1}) \\\\ \\text{arccsc}(x) &amp;= \\arctan(1\/\\sqrt{x^2 - 1}) \\end{align*}\" width=\"239\" height=\"136\" \/><\/p>\n<p>Also, you can use arctan to compute \u03c0 since \u03c0 = 4 arctan(1).<\/p>\n<h2>Hyperbolic functions<\/h2>\n<p>If you have a way to compute exponentials, you can calculate hyperbolic functions.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium aligncenter\" style=\"background-color: white;\" src=\"https:\/\/www.johndcook.com\/bootstrap_hyperbolic.svg\" alt=\"\\begin{align*} \\sinh(x) &amp;= \\frac{\\exp(x) - \\exp(-x)}{2} \\\\ \\cosh(x) &amp;= \\frac{\\exp(x) + \\exp(-x)}{2} \\\\ \\tanh(x) &amp;= \\frac{\\exp(x) - \\exp(-x)}{\\exp(x) + \\exp(-x)} \\\\ \\end{align*}\" width=\"222\" height=\"131\" \/><\/p>\n<h2>Inverse hyperbolic functions<\/h2>\n<p>If you can compute square roots and logs, you can compute inverse hyperbolic functions.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium aligncenter\" style=\"background-color: white;\" src=\"https:\/\/www.johndcook.com\/bootstrap_inverse_hyperbolic.svg\" alt=\"\\begin{align*} \\text{arcsinh} (z) &amp;= \\log \\left( z + \\sqrt{1 + z^2} \\right) \\\\ \\text{arccosh} (z) &amp;= 2 \\log\\left( \\sqrt{(z+1)\/2} + \\sqrt{(z-1)\/2} \\right) \\\\ \\text{arctanh} (z) &amp;= \\log \\left( (1+z) \\sqrt{1\/(1 - z^2)}\\right) \\end{align*}\" width=\"364\" height=\"109\" \/><\/p>\n<h2>Complex branch cuts<\/h2>\n<p>Up to this point this post has implicitly assumed we&#8217;re only working with real numbers. When working over complex numbers, inverse functions get more complicated. You have to be explicit about which branch you&#8217;re taking when you invert a function that isn&#8217;t one-to-one.<\/p>\n<p>Common Lisp worked though all this very thoroughly, defining arc tangent first, then defining everything else in a carefully chosen sequence. See <a href=\"https:\/\/www.johndcook.com\/blog\/2016\/12\/17\/branch-cuts-and-common-lisp\/\">Branch cuts and Common Lisp<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you don&#8217;t have all the math functions available that you would like. For example, maybe you have a way to calculate natural logs but you would like to calculate a log base 10. The Unix utility bc is a prime example of this. It only includes six common math functions: sine cosine arctangent natural [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"class_list":["post-68169","post","type-post","status-publish","format-standard","hentry","category-math"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.10 - aioseo.com -->\n\t<meta name=\"description\" content=\"How to start with a minimal math library and compute functions that are not included. e.g. how to use arc tangent to compute the other inverse trig functions.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"John\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.johndcook.com\/blog\/2021\/01\/05\/bootstrapping-math-library\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.10\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"John D. Cook | Applied Mathematics Consulting\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Bootstrapping a small math library\" \/>\n\t\t<meta property=\"og:description\" content=\"How to start with a minimal math library and compute functions that are not included. e.g. how to use arc tangent to compute the other inverse trig functions.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.johndcook.com\/blog\/2021\/01\/05\/bootstrapping-math-library\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2021-01-05T14:54:53+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2021-01-06T12:07:28+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Bootstrapping a small math library\" \/>\n\t\t<meta name=\"twitter:description\" content=\"How to start with a minimal math library and compute functions that are not included. e.g. how to use arc tangent to compute the other inverse trig functions.\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Bootstrapping a small math library","description":"How to start with a minimal math library and compute functions that are not included. e.g. how to use arc tangent to compute the other inverse trig functions.","canonical_url":"https:\/\/www.johndcook.com\/blog\/2021\/01\/05\/bootstrapping-math-library\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"John D. Cook | Applied Mathematics Consulting","og:type":"article","og:title":"Bootstrapping a small math library","og:description":"How to start with a minimal math library and compute functions that are not included. e.g. how to use arc tangent to compute the other inverse trig functions.","og:url":"https:\/\/www.johndcook.com\/blog\/2021\/01\/05\/bootstrapping-math-library\/","article:published_time":"2021-01-05T14:54:53+00:00","article:modified_time":"2021-01-06T12:07:28+00:00","twitter:card":"summary","twitter:title":"Bootstrapping a small math library","twitter:description":"How to start with a minimal math library and compute functions that are not included. e.g. how to use arc tangent to compute the other inverse trig functions.","twitter:image":"https:\/\/www.johndcook.com\/blog\/wp-content\/uploads\/2022\/05\/twittercard.png"},"aioseo_meta_data":{"post_id":"68169","title":"Bootstrapping a small math library","description":"How to start with a minimal math library and compute functions that are not included. e.g. how to use arc tangent to compute the other inverse trig functions.","keywords":[],"keyphrases":{"focus":[],"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":[],"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":"none","schema_type_options":"{\"webPage\":{\"webPageType\":\"WebPage\"},\"article\":{\"articleType\":\"BlogPosting\"},\"book\":[],\"course\":[],\"event\":[],\"jobPosting\":[],\"music\":[],\"person\":[],\"product\":[],\"recipe\":[],\"restaurant\":[],\"service\":[],\"software\":[],\"video\":[]}","pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","location":null,"local_seo":{"businessInfo":{"name":"","urls":{"website":"","aboutPage":"","contactPage":""},"address":{"line1":"","line2":"","zip":"","city":"","state":"","country":""},"contact":{"email":"","phone":"","fax":""},"ids":{"vatID":"","taxID":"","chamberID":""},"payment":{"priceIndication":"","currenciesAccepted":"","methodsAccepted":""},"areaServed":""},"openingHours":{"show":false,"closedLabel":"","open24h":false,"open24hLabel":"","open247":false,"use24hFormat":false,"twoSets":false,"timezone":"","hours":[]}},"breadcrumb_settings":null,"limit_modified_date":false,"created":"2021-01-05 13:51:38","updated":"2025-06-04 01:53:11","ai":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.johndcook.com\/blog\/category\/math\/\" title=\"Math\">Math<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tBootstrapping a minimal math library\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.johndcook.com\/blog"},{"label":"Math","link":"https:\/\/www.johndcook.com\/blog\/category\/math\/"},{"label":"Bootstrapping a minimal math library","link":"https:\/\/www.johndcook.com\/blog\/2021\/01\/05\/bootstrapping-math-library\/"}],"_links":{"self":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/68169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/comments?post=68169"}],"version-history":[{"count":0,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/posts\/68169\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/media?parent=68169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/categories?post=68169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.johndcook.com\/blog\/wp-json\/wp\/v2\/tags?post=68169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}