{"id":7453,"date":"2020-07-28T15:18:46","date_gmt":"2020-07-28T15:18:46","guid":{"rendered":"https:\/\/www.askpython.com\/?p=7453"},"modified":"2022-08-06T13:29:23","modified_gmt":"2022-08-06T13:29:23","slug":"python-decimal-module","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/python-decimal-module","title":{"rendered":"Python decimal module &#8211; 7 functions you need to know!"},"content":{"rendered":"\n<p>Hey, all! In this article, we will have a look at one of the interesting modules &#8211; The <strong>Python Decimal module<\/strong>.<\/p>\n\n\n\n<p>Be it any domain, we do come across the need to search for functions to perform mathematical operations. Python decimal module serves us with all the mathematical functions we need.<\/p>\n\n\n\n<p>So, let us get started!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding the Python decimal module<\/h2>\n\n\n\n<p>Python decimal module contains various functions to handle numeric data and perform different mathematical operations on it. Using the decimal module, we can handle decimal numbers efficiently throughout our program.<\/p>\n\n\n\n<p><code>The decimal module<\/code> provides us with functions to control and overcome the problem of precision in the decimal values.<\/p>\n\n\n\n<p>Having understood the need of Decimal module, let us now have a look at some of the important functions offered by the module.<\/p>\n\n\n\n<p>In order to use the functions, we need to import the module as shown below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport decimal\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">The Decimal Module Functions and Implementation<\/h2>\n\n\n\n<p>Different arithmetic operations can be performed on the decimal or numeric data to boost the outcome. <\/p>\n\n\n\n<p>We can define the decimal point numbers using the <code>decimal.Decimal() function<\/code> as shown below&#8211;<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport decimal\nvariable = decimal.Decimal(decimal-number)\n<\/pre><\/div>\n\n\n<p>Further, we can control the precision value of the results of the decimal point numbers using an in-built function by the decimal module &#8212; <code>decimal.getcontext().prec<\/code> function.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndecimal.getcontext().prec = precision value\n<\/pre><\/div>\n\n\n<p>The below-explained functions help us perform decimal point arithmetic operations in an efficient manner and at a great pace.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. The exp() function &#8211; Exponent calculation<\/h3>\n\n\n\n<p>The <code>exp() function<\/code> computes the <strong>exponent value i.e. e^x<\/strong> of the particular decimal point number passed to it.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndecimal.Decimal(decimal-number).exp()\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport decimal as d\n\nd.getcontext().prec = 5\n\n#Intializing with an addition operation\nval = d.Decimal(12.201) + d.Decimal(12.20)\n\n#Calculating exponential of the decimal value\nexp = val.exp()\n\n#variable with no calculations\nno_math = d.Decimal(1.131231)\n\nprint(&quot;Sum: &quot;,val)\nprint(&quot;Exponential: &quot;, exp)\nprint(no_math)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nDecimal Number:  24.401                                                                                                       \n3.9557E+10                                                                                                                    \n1.131231000000000097571728474576957523822784423828125   \n<\/pre><\/div>\n\n\n<p>Notice how the total number of digits in our output are 5? That&#8217;s because of the precision value that we set up here. <\/p>\n\n\n\n<p>One thing to remember is that the precision value applies when you perform mathematical operations on two decimals not when you directly initiate a variable with the values as shown with the &#8220;no_math&#8221; variable above.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. The sqrt() function &#8211; Square Root<\/h3>\n\n\n\n<p>The sqrt() function calculates and returns the <strong>square root value <\/strong>of the passed decimal number to it.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndecimal.Decimal(decimal-number).sqrt()\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport decimal as d\n\nd.getcontext().prec = 3\n\nval = d.Decimal(122.20)\nsqrt = val.sqrt()\nprint(&quot;Decimal Number: &quot;,val)\nprint(&quot;Square root of the decimal number: &quot;,sqrt)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nDecimal Number:  122.2000000000000028421709430404007434844970703125\nSquare root of the decimal number:  11.1\n<\/pre><\/div>\n\n\n<p>Again, note how the declared value contains the complete decimal number while the computed value follows our precision set of 3 digits.<\/p>\n\n\n\n<p>To find more mathematical operations, read our article on <a href=\"https:\/\/www.askpython.com\/python-modules\/python-math-module\" class=\"rank-math-link\">math module in Python<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. Logarithmic functions<\/h3>\n\n\n\n<p>Decimal module provides us with the below functions to calculate the logarithmic values of the decimal point numbers&#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>decimal.ln()<\/strong><\/li><li><strong>decimal.log10()<\/strong><\/li><\/ul>\n\n\n\n<p>The <code>decimal.ln() function <\/code>returns the natural log value of the decimal number as shown below&#8211;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndecimal.Decimal(decimal-number).ln()\n<\/pre><\/div>\n\n\n<p>The decimal.log10() function is used to calculate the log value of the base 10 of the decimal number passed to it.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndecimal.Decimal(decimal-number).log10()\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport decimal as d\n\nd.getcontext().prec = 2\n\nval = d.Decimal(122.20)\n\nlog = val.ln()\nprint(&quot;Natural log value of the decimal number: &quot;,log)\n\nlog_10 = val.log10()\nprint(&quot;Log value with base 10 of the decimal number: &quot;,log_10)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nNatural log value of the decimal number:  4.8\nLog value with base 10 of the decimal number:  2.1\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. The compare() function <\/h3>\n\n\n\n<p>The <code>decimal.compare() function<\/code> compares two decimal point numbers and returns the values depending upon the conditions as follows&#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>It returns -1, if the first decimal number is smaller than the second decimal number.<\/strong><\/li><li><strong>It returns 1, if the first decimal number is greater than the second decimal number.<\/strong><\/li><li><strong>Returns 0, if both the decimal point values are equal.<\/strong><\/li><\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport decimal as d\n\nvalx = d.Decimal(122.20)\nvaly = d.Decimal(123.01)\n\nprint(&quot;Value 1: &quot;,valx)\nprint(&quot;Value 2: &quot;,valy)\n\ncompare = valx.compare(valy)\nprint(compare)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nValue 1:  122.2000000000000028421709430404007434844970703125\nValue 2:  123.0100000000000051159076974727213382720947265625\n-1\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. The copy_abs() function<\/h3>\n\n\n\n<p>The <code>decimal.copy_abs() function<\/code> returns the absolute values of the signed decimal number passed to it.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndecimal.Decimal(signed decimal number).copy_abs()\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport decimal as d\n\nvalx = d.Decimal(-122.20)\nprint(&quot;Value 1: &quot;,valx)\n\nabsolute = valx.copy_abs()\nprint(&quot;Absolute value of the given decimal number: &quot;,absolute)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nValue 1:  -122.2000000000000028421709430404007434844970703125\nAbsolute value of the given decimal number:  122.2000000000000028421709430404007434844970703125\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. Max and Min functions<\/h3>\n\n\n\n<p>The Python decimal module contains the following functions to calculate the minimum and maximum values of the decimal point numbers.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>min() function: Returns the minimum of the two decimal values.<\/strong><\/li><li><strong>max() function: Returns the maximum of the two decimal values.<\/strong><\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#Syntax for min() function-\ndecimal1.min(decimal2)\n\n#Syntax for max() function-\ndecimal1.max(decimal2)\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport decimal as d\n\nvalx = d.Decimal(122.20)\nvaly = d.Decimal(123.01)\n\nprint(&quot;Value 1: &quot;,valx)\nprint(&quot;Value 2: &quot;,valy)\n\nmin_val = valx.min(valy)\nprint(&quot;The minimum of the two values: &quot;,min_val)\n\nmax_val = valx.max(valy)\nprint(&quot;The maximum of the two values: &quot;,max_val)\n\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nValue 1:  122.2000000000000028421709430404007434844970703125\nValue 2:  123.0100000000000051159076974727213382720947265625\nThe minimum of the two values:  122.2000000000000028421709430\nThe maximum of the two values:  123.0100000000000051159076975\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. Logical operations with decimal module<\/h3>\n\n\n\n<p>Decimal module contains a set of in-built functions to perform <a href=\"https:\/\/www.askpython.com\/python\/python-logical-operators\" class=\"rank-math-link\">logical operations<\/a> on the decimal numbers such as AND, OR, XOR, etc.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>logical_and() function: <\/strong>It performs logical AND operation on the two decimal numbers and returns the result.<\/li><li><strong>logical_or() function: <\/strong>It performs logical OR operation on the two decimal numbers and returns the result.<\/li><li><strong>logical_xor() function: <\/strong>It performs logical XOR operation on the two decimal numbers and returns the result.<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#Syntax for logical_and() function-\ndecimal1.logical_and(decimal2)\n\n#Syntax for logical_or() function-\ndecimal1.logical_or(decimal2)\n\n#Syntax for logical_xor() function-\ndecimal1.logical_xor(decimal2)\n\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport decimal as d\n\nvalx = d.Decimal(1001)\nvaly = d.Decimal(1111)\n\nprint(&quot;Value 1: &quot;,valx)\nprint(&quot;Value 2: &quot;,valy)\n\nAND = valx.logical_and(valy)\nprint(&quot;The logical AND value of the two decimals: &quot;,AND)\n\nOR = valx.logical_or(valy)\nprint(&quot;The logical OR value of the two decimals: &quot;,OR)\n\nXOR = valx.logical_xor(valy)\nprint(&quot;The logical XOR value of the two decimals: &quot;,XOR)\n\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nValue 1:  1001\nValue 2:  1111\nThe logical AND value of the two decimals:  1001\nThe logical OR value of the two decimals:  1111\nThe logical XOR value of the two decimals:  110\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.<\/p>\n\n\n\n<p>Till then, Happy Learning!!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Understanding Python decimal module &#8212; JournalDev<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hey, all! In this article, we will have a look at one of the interesting modules &#8211; The Python Decimal module. Be it any domain, we do come across the need to search for functions to perform mathematical operations. Python decimal module serves us with all the mathematical functions we need. So, let us get [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":7515,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-7453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=7453"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7453\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/7515"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=7453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=7453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=7453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}