{"id":25544,"date":"2022-12-11T00:03:31","date_gmt":"2022-12-10T18:33:31","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/"},"modified":"2022-12-11T00:03:31","modified_gmt":"2022-12-10T18:33:31","slug":"solved-do-arithmetic-operations-on-float-numbers-correctly-in-php","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/","title":{"rendered":"[Solved] Do arithmetic operations on FLOAT numbers correctly in PHP"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-51309285\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"51309285\" data-parentid=\"51140939\" data-score=\"4\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Sure the problem of PHP floating point precision was a topic on SO quite often, still I cannot see why this questions albeits 18 downvotes as the questionare asked very precisly for a concrete problem with a concrete floating point number. I have solved this in a way that everybody should understand clearly what the problem here is (not just those unprecise reasoning).<\/p>\n<p>Here we go &#8211; see the results of the following:<\/p>\n<pre><code>$example = 90000000000009132 - 1;\n\nvar_dump(PHP_INT_MAX);\nvar_dump($example); \nvar_dump(PHP_FLOAT_MAX);\nvar_dump(sprintf('%.0g',  $example)); \nvar_dump(sprintf('%.0f',  $example));\n<\/code><\/pre>\n<p>The results:<\/p>\n<pre><code>int(9223372036854775807) \nint(90000000000009131) \nfloat(1.7976931348623E+308) \nstring(7) \"9.0e+16\" \nstring(17) \"90000000000009136\"\n<\/code><\/pre>\n<p>That means: <\/p>\n<ul>\n<li>You are not above the maximum integer size which would make<br \/>\narithmetic operations fail<\/li>\n<li>The result of your calculation is correct with integers<\/li>\n<li>You are not above PHP_FLOAT_MAX (requires PHP 7.2) though the size of this depends on your environment<\/li>\n<li>The floating point representation of your calculation is unexpectedly wrong (as your printf statement converts to float and then to string)<\/li>\n<\/ul>\n<p>So why do we have this strange result? Lets make some tests:<\/p>\n<pre><code>var_dump(sprintf('%.0f',  90000000000009051));\nvar_dump(sprintf('%.0f',  90000000000009101));\nvar_dump(sprintf('%.0f',  90000000000009105));\nvar_dump(sprintf('%.0f',  90000000000009114));\nvar_dump(sprintf('%.0f',  90000000000009121));\nvar_dump(sprintf('%.0f',  90000000000009124));\nvar_dump(sprintf('%.0f',  90000000000009128));\nvar_dump(sprintf('%.0f',  90000000000009130));\nvar_dump(sprintf('%.0f',  90000000000009131));\nvar_dump(sprintf('%.0f',  90000000000009138));\nvar_dump(sprintf('%.0f',  90000000000009142));\nvar_dump(sprintf('%.0f',  90000000000009177));\n<\/code><\/pre>\n<p>The result of this is:<\/p>\n<pre><code>string(17) \"90000000000009056\"\nstring(17) \"90000000000009104\"\nstring(17) \"90000000000009104\"\nstring(17) \"90000000000009120\"\nstring(17) \"90000000000009120\"\nstring(17) \"90000000000009120\"\nstring(17) \"90000000000009120\"\nstring(17) \"90000000000009136\"\nstring(17) \"90000000000009136\"\nstring(17) \"90000000000009136\"\nstring(17) \"90000000000009136\"\nstring(17) \"90000000000009184\"\n<\/code><\/pre>\n<p>Look at those numbers. The digits at the end are always dividable by 16. And what means the 16 in information technology? A byte. Hexadecimal. What does it tell us?<br \/>\nYou probably have exceeded the limit where floats are basically precise (not necessarily in arithmetic operations).<\/p>\n<p>What if we reduce those numbers by one zero to 16 digits?<\/p>\n<pre><code>var_dump(sprintf('%.0f',  9000000000009051));\nvar_dump(sprintf('%.0f',  9000000000009101));\nvar_dump(sprintf('%.0f',  9000000000009124));\nvar_dump(sprintf('%.0f',  9000000000009138));\nvar_dump(sprintf('%.0f',  9000000000009142));\nvar_dump(sprintf('%.0f',  9000000000009177));\n<\/code><\/pre>\n<p>&#8230;<\/p>\n<pre><code>string(16) \"9000000000009051\"\nstring(16) \"9000000000009101\"\nstring(16) \"9000000000009124\"\nstring(16) \"9000000000009138\"\nstring(16) \"9000000000009142\"\nstring(16) \"9000000000009177\"\n<\/code><\/pre>\n<p>The output is always correct. Well. And what if we improve it by one zero digit?<\/p>\n<pre><code>var_dump(sprintf('%.0f',  900000000000009051));\nvar_dump(sprintf('%.0f',  900000000000009101));\nvar_dump(sprintf('%.0f',  900000000000009124));\nvar_dump(sprintf('%.0f',  900000000000009138));\nvar_dump(sprintf('%.0f',  900000000000009142));\nvar_dump(sprintf('%.0f',  900000000000009177));\n<\/code><\/pre>\n<p>&#8230;<\/p>\n<pre><code>string(18) \"900000000000009088\"\nstring(18) \"900000000000009088\"\nstring(18) \"900000000000009088\"\nstring(18) \"900000000000009088\"\nstring(18) \"900000000000009088\"\nstring(18) \"900000000000009216\"\n<\/code><\/pre>\n<p>Interestingly this are factors of 16 again &#8211; even more imprecise.<\/p>\n<p>And now lets solve this mystery:<\/p>\n<pre><code>var_dump(strlen(sprintf('%.0f',  9000000000009051)));\n<\/code><\/pre>\n<p>Guess what the result is:<\/p>\n<blockquote>\n<p>int(16)<\/p>\n<\/blockquote>\n<p>Wow. That means floating point numbers basically work precisly down to 16 digits in PHP (<em>if you exceed this magical numbers it is getting imprecise<\/em>).<\/p>\n<p>So what has happend to your calculation? <\/p>\n<blockquote>\n<p><strong>It was rounded to the next 16 at the end as you exceeded the number of 16 digits by one.<\/strong><\/p>\n<\/blockquote>\n<p>More information on floating point precision in PHP can be found in dozens of questions on Stackoverflow and of course in the PHP manual.<\/p>\n<p>To answer how to make your code work correctly:<\/p>\n<blockquote>\n<p><strong>If you want your numbers to be precise do not convert to float. With integers for instance higher calculations should be possible.<\/strong><\/p>\n<p><strong>If you exceed the number of 16 digits also simple arithmetic like -1 or +1 will fail (at least in the current version of PHP as it seems).<\/strong><\/p>\n<\/blockquote><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">3<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Do arithmetic operations on FLOAT numbers correctly in PHP <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Sure the problem of PHP floating point precision was a topic on SO quite often, still I cannot see why this questions albeits 18 downvotes as the questionare asked very precisly for a concrete problem with a concrete floating point number. I have solved this in a way that everybody should understand clearly what &#8230; <a title=\"[Solved] Do arithmetic operations on FLOAT numbers correctly in PHP\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/\" aria-label=\"More on [Solved] Do arithmetic operations on FLOAT numbers correctly in PHP\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[359,339],"class_list":["post-25544","post","type-post","status-publish","format-standard","hentry","category-solved","tag-floating-point","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Do arithmetic operations on FLOAT numbers correctly in PHP - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Do arithmetic operations on FLOAT numbers correctly in PHP - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Sure the problem of PHP floating point precision was a topic on SO quite often, still I cannot see why this questions albeits 18 downvotes as the questionare asked very precisly for a concrete problem with a concrete floating point number. I have solved this in a way that everybody should understand clearly what ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-10T18:33:31+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Do arithmetic operations on FLOAT numbers correctly in PHP\",\"datePublished\":\"2022-12-10T18:33:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/\"},\"wordCount\":400,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"floating-point\",\"php\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/\",\"name\":\"[Solved] Do arithmetic operations on FLOAT numbers correctly in PHP - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-10T18:33:31+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Do arithmetic operations on FLOAT numbers correctly in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Do arithmetic operations on FLOAT numbers correctly in PHP - JassWeb","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:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Do arithmetic operations on FLOAT numbers correctly in PHP - JassWeb","og_description":"[ad_1] Sure the problem of PHP floating point precision was a topic on SO quite often, still I cannot see why this questions albeits 18 downvotes as the questionare asked very precisly for a concrete problem with a concrete floating point number. I have solved this in a way that everybody should understand clearly what ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/","og_site_name":"JassWeb","article_published_time":"2022-12-10T18:33:31+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Do arithmetic operations on FLOAT numbers correctly in PHP","datePublished":"2022-12-10T18:33:31+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/"},"wordCount":400,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["floating-point","php"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/","url":"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/","name":"[Solved] Do arithmetic operations on FLOAT numbers correctly in PHP - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-10T18:33:31+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-do-arithmetic-operations-on-float-numbers-correctly-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Do arithmetic operations on FLOAT numbers correctly in PHP"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/25544","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=25544"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/25544\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=25544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=25544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=25544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}