{"id":7291,"date":"2024-03-06T07:25:56","date_gmt":"2024-03-06T07:25:56","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=7291"},"modified":"2024-03-19T09:41:30","modified_gmt":"2024-03-19T09:41:30","slug":"division-in-solidity","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/division-in-solidity\/","title":{"rendered":"How to Perform Division in Solidity?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/division-in-solidity\/#Division_in_general\" title=\"Division in general\">Division in general<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/division-in-solidity\/#Division_in_Python\" title=\"Division in Python\">Division in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/division-in-solidity\/#The_problem_of_division_in_Solidity\" title=\"The problem of division in Solidity\">The problem of division in Solidity<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/division-in-solidity\/#Example_code_1\" title=\"Example code #1\">Example code #1<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/metaschool.so\/articles\/division-in-solidity\/#Example_code_2\" title=\"Example code #2\">Example code #2<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/metaschool.so\/articles\/division-in-solidity\/#How_to_prevent_division_error_in_Solidity\" title=\"How to prevent division error in Solidity?\">How to prevent division error in Solidity?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/metaschool.so\/articles\/division-in-solidity\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>In Solidity, there is no concept of floating point. If you\u2019re reading this then I am assuming that you must know the difference between integers and floating point numbers. A floating point number represents a decimal value like 1.25, while an integer does not. Let\u2019s find out what role floating numbers play in the division.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"division-in-general\"><span class=\"ez-toc-section\" id=\"Division_in_general\"><\/span>Division in general<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>When you divide any two numbers, the result can either be in decimal or integer form. For instance, if you divide 5 by 2, the answer is 2.5, which is a decimal number. On the other hand, if you divide 4 by 2, the answer is 2, which is an integer.<\/p>\n\n\n\n<p>So, how do programming languages account for this difference? Before exploring how Solidity deals with this issue, let&#8217;s first examine how Python handles it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"division-in-python\"><span class=\"ez-toc-section\" id=\"Division_in_Python\"><\/span>Division in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python is a powerful programming language that provides support for the float data type. This data type allows for the manipulation of floating-point numbers, which can be useful in a variety of computational tasks. In particular, the float data type enables easy division between two floating point numbers, allowing for precise and accurate results in your code.<\/p>\n\n\n\n<p>Let\u2019s look at an example.<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>a = 5.0\nb = 2.0\nres = a\/b\n# output will be 2.5<\/code><\/pre>\n\n\n\n<p>You can see why having float-type data is beneficial for programming languages. It helps them find and store the right answer of division between any two numbers. This way you find the right answer to division and you don\u2019t lose any data.<\/p>\n\n\n\n<p>Unlike many programming languages, Solidity doesn\u2019t support floating numbers. Let\u2019s find out what answer we get after doing the simple division in Solidity.<\/p>\n\n\n\n<p><strong>\ud83d\udd25 Check this course out:<\/strong>\u00a0<a href=\"https:\/\/metaschool.so\/courses\/create-your-own-ethereum-token-in-just-30-mins\" target=\"_blank\" rel=\"noreferrer noopener\">Create Your Own Ethereum Token in Just 30 Mins<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-problem-of-division-in-solidity\"><span class=\"ez-toc-section\" id=\"The_problem_of_division_in_Solidity\"><\/span>The problem of division in Solidity<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Solidity offers two types of integers &#8211; signed (<code>int<\/code>) and unsigned integers (<code>uint<\/code>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-code-1\"><span class=\"ez-toc-section\" id=\"Example_code_1\"><\/span>Example code #1<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Let\u2019s perform a division on unsigned integers in Solidity using the following code.<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.8.20;\n\ncontract ExampleContract {\n  \/\/ We will use these variables to perform the division\n\tuint256 num_1;\n  uint256 num_2;\n\n\t\/\/ constructor function is used to initialize the num_1 and num_2 variables\n    constructor(uint256 aNum, uint256 bNum) {\n        num_1 = aNum;\n        num_2 = bNum;\n    }\n\n\t\/\/ division function will perform the division functionality \n\t\/\/ and return the result\n    function division() public view returns (uint256) {\n        return num_1 \/ num_2;\n    }\n}<\/code><\/pre>\n\n\n\n<p>This code will give you the following output for the 5 divided by 2 examples.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"311\" src=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/division-output-1-1024x311.png\" alt=\"\" class=\"wp-image-7294\" srcset=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/division-output-1-1024x311.png 1024w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/division-output-1-300x91.png 300w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/division-output-1-150x46.png 150w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/division-output-1-768x234.png 768w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/division-output-1-1536x467.png 1536w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/division-output-1-1320x402.png 1320w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/division-output-1.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>It seems there is an issue here. The&nbsp;<code>division<\/code>&nbsp;function provided us with an output of 2 instead of 2.5. This indicates that Solidity rounded up the 2.5 and returned 2 as the result. Moreover, it appears that Solidity does not support floating numbers. You can verify this by testing the code in the Remix IDE.<\/p>\n\n\n\n<p>If you divide 5 by 3, Solidity will round the output to 1 instead of 1.666 on a calculator.<\/p>\n\n\n\n<p>Let\u2019s take another example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-code-2\"><span class=\"ez-toc-section\" id=\"Example_code_2\"><\/span>Example code #2<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Let\u2019s perform multiplication and division in one line and see what happens.<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.8.20;\n\ncontract ExampleContract {\n  \/\/ We will use these variables to perform the division\n\tuint256 num_1;\n  uint256 num_2;\n\tuint256 num_3;\n\n\t\t\/\/ constructor function is used to initialize the num_1 and num_2 variables\n    constructor(uint256 aNum, uint256 bNum, uint256 cNum) {\n        num_1 = aNum;\n        num_2 = bNum;\n\t\t\t\tnum_3 = cNum;\n    }\n\n\t\t\/\/ division function will perform the division functionality \n\t\t\/\/ and return the result\n    function division() public view returns (uint256) {\n        return num_1 \/ num_2;\n    }\n\n\tfunction divAndMul() public view returns (uint256) {\n        return (num_1 * num_2) \/ num_3;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>After entering the inputs 2, 3, and 5 for the&nbsp;<code>divAndMul<\/code>&nbsp;function in Remix IDE, the following output is obtained:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"311\" src=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/division-output-2-1024x311.png\" alt=\"\" class=\"wp-image-7295\" srcset=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/division-output-2-1024x311.png 1024w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/division-output-2-300x91.png 300w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/division-output-2-150x46.png 150w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/division-output-2-768x234.png 768w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/division-output-2-1536x467.png 1536w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/division-output-2-1320x402.png 1320w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/division-output-2.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Upon running the Solidity code, it resulted in 1, whereas the calculator gives 1.2 for the same input.<\/p>\n\n\n\n<p>Now, you get the idea of how tricky it is to perform the division in Solidity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-prevent-division-error-in-solidity\"><span class=\"ez-toc-section\" id=\"How_to_prevent_division_error_in_Solidity\"><\/span>How to prevent division error in Solidity?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The most common way to solve the division error in Solidity is by using a multiplier. A multiplier is an additional variable with a value of 10, 100, or 1000. To solve the issue, we multiply the numerator by the multiplier and then divide the result by the denominator. For example, if we use a multiplier of 100, we would multiply the numerator by 100 and then divide the result by the denominator. Let\u2019s look at the example code for this.<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.8.20;\n\ncontract ExampleContract {\n  \/\/ We will use these variables to perform the division\n\tuint256 num_1;\n  uint256 num_2;\n\tuint256 num_3;\n\tuint256 multiplier;\n\n\t\t\/\/ constructor function is used to initialize the num_1 and num_2 variables\n    constructor(uint256 aNum, uint256 bNum, uint256 cNum) {\n        num_1 = aNum;\n        num_2 = bNum;\n\t\t\t\tnum_3 = cNum;\n\t\t\t\tmultiplier = 100;\n    }\n\n\t\t\/\/ division function will perform the division functionality \n\t\t\/\/ and return the result\n    function division() public view returns (uint256) {\n        return (num_1 * multiplier) \/ num_2;\n    }\n\n\t\tfunction divAndMul() public view returns (uint256) {\n\t        return (num_1 * num_2 * multiplier) \/ num_3;\n    }\n}<\/code><\/pre>\n\n\n\n<p>After entering inputs 2, 3, and 5 respectively, we received the following output in Remix IDE for both functions:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"329\" src=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/output-3-1-1-1024x329.png\" alt=\"\" class=\"wp-image-7296\" srcset=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/output-3-1-1-1024x329.png 1024w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/output-3-1-1-300x96.png 300w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/output-3-1-1-150x48.png 150w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/output-3-1-1-768x246.png 768w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/output-3-1-1-1536x493.png 1536w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/output-3-1-1-1320x424.png 1320w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/03\/output-3-1-1.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>If you divide 120 by 100, you\u2019ll get 1.2, which is what we want. If you divide 66 by 100, you\u2019ll get 0.66, which is also what we want. Therefore, using the multiplier resolved the issue.<\/p>\n\n\n\n<p>\ud83d\udd25&nbsp;<strong>Check this course out:<\/strong>&nbsp;<a href=\"https:\/\/metaschool.so\/courses\/build-marketplace-erc404-tokens\" target=\"_blank\" rel=\"noreferrer noopener\">Build a Semi-Fungible ERC404 Tokens&#8217; Marketplace<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are also other methods to resolve this issue, but using the multiplier method is the common and easy way. You can also use libraries like ABDKMath64x64 to resolve the division error.<\/p>\n\n\n\n<p><strong>Try it out, ask us questions, and tell us how it went by tagging Metaschool on Social Media.<\/strong><\/p>\n\n\n\n<p><strong>Follow us on<\/strong>&nbsp;\u2013<\/p>\n\n\n\n<p>\ud83d\udd2eTwitter \u2013\u00a0<a href=\"https:\/\/twitter.com\/0xmetaschool\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/twitter.com\/0xmetaschool<\/a><\/p>\n\n\n\n<p>\ud83d\udd17LinkedIn \u2013\u00a0<a href=\"https:\/\/www.linkedin.com\/company\/0xmetaschool\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.linkedin.com\/company\/0xmetaschool\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":15,"featured_media":7292,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"[\"title\",\"meta\",\"thumbnail\",\"content\",\"tags\",\"comments\"]","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[292],"tags":[51,46,54],"class_list":["post-7291","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-blockchain","tag-solidity","tag-solidity-programming-language"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=7291"}],"version-history":[{"count":4,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7291\/revisions"}],"predecessor-version":[{"id":7353,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7291\/revisions\/7353"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/7292"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=7291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=7291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=7291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}