{"id":2341,"date":"2021-12-30T13:22:03","date_gmt":"2021-12-30T13:22:03","guid":{"rendered":"https:\/\/blog.embold.io\/?p=2341"},"modified":"2021-12-30T13:22:03","modified_gmt":"2021-12-30T13:22:03","slug":"how-to-check-if-two-values-are-nearly-or-approximately-equal","status":"publish","type":"post","link":"https:\/\/blog.embold.io\/es\/how-to-check-if-two-values-are-nearly-or-approximately-equal\/","title":{"rendered":"How to check if two values are nearly or approximately equal?"},"content":{"rendered":"\n<p>As a JavaScript developer, there will be instances where you need to compare that the output of a computation is approximate to an expected value, to determine its correctness, especially when working with floating-point numbers. Working with floating-point numbers in JavaScript is can be weird, and what you expect isn\u2019t always&nbsp;<a target=\"_blank\" href=\"https:\/\/indepth.dev\/posts\/1139\/here-is-what-you-need-to-know-about-javascripts-number-type\" rel=\"noreferrer noopener\">what you get<\/a>.&nbsp;<\/p>\n\n\n\n<p>To check that two numbers are approximate to each other:\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const approximatelyEqual = (v1, v2, tolerance) =&gt; {\n  return Math.abs(v1 - v2) &lt; tolerance;\n}<\/code><\/pre>\n\n\n\n<p>The <code>approximatelyEqual <\/code>function takes in three arguments: expected output, real output, and tolerance. The tolerance is the permissible difference between both numbers \u2014 how close you need the two numbers to be before you consider them equal enough. For example, if the difference between both numbers needs to be less than or equal to 0.001 for it to be considered correct, then 0.001 is the tolerance. <\/p>\n\n\n\n<p>So:  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u200b\u200bapproximatelyEqual(10, -10.1, 0.1)\n\/\/output: false;\n \n\u200b\u200bapproximatelyEqual(10, 10.1, 0.1)\n\/\/output: true\n <\/code><\/pre>\n\n\n<p><span style=\"color: #ff0000\">Blog Credits: Linda Ikechukwu<\/span><\/p>\n<div>\u201d Linda Ikechukwu is a Frontend Developer turned Technical Content Consultant. She enjoys strategizing on and creating content that helps tech startups connect with their target audience. \u201c<\/div>\n<div><img decoding=\"async\" loading=\"lazy\" class=\"\" src=\"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/Linda-headshot.jpeg\" width=\"152\" height=\"213\" \/><\/div>","protected":false},"excerpt":{"rendered":"<p>As a JavaScript developer, there will be instances where you need to compare that the output of a computation is approximate to an expected value, to determine its correctness, especially&hellip;<\/p>\n","protected":false},"author":10,"featured_media":2342,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[281,283,284,280,221,282],"better_featured_image":{"id":2342,"alt_text":"","caption":"","description":"","media_type":"image","media_details":{"width":1800,"height":923,"file":"2021\/12\/equal.jpg","sizes":{"thumbnail":{"file":"equal-150x150.jpg","width":150,"height":150,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/12\/equal-150x150.jpg"},"medium_large":{"file":"equal-768x394.jpg","width":768,"height":394,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/12\/equal-768x394.jpg"},"penci-full-thumb":{"file":"equal-1170x600.jpg","width":1170,"height":600,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/12\/equal-1170x600.jpg"},"penci-slider-thumb":{"file":"equal-1170x663.jpg","width":1170,"height":663,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/12\/equal-1170x663.jpg"},"penci-magazine-slider":{"file":"equal-780x516.jpg","width":780,"height":516,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/12\/equal-780x516.jpg"},"penci-slider-full-thumb":{"file":"equal-1800x800.jpg","width":1800,"height":800,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/12\/equal-1800x800.jpg"},"penci-thumb":{"file":"equal-585x390.jpg","width":585,"height":390,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/12\/equal-585x390.jpg"},"penci-masonry-thumb":{"file":"equal-585x300.jpg","width":585,"height":300,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/12\/equal-585x300.jpg"},"penci-thumb-square":{"file":"equal-585x585.jpg","width":585,"height":585,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/12\/equal-585x585.jpg"},"penci-thumb-vertical":{"file":"equal-480x650.jpg","width":480,"height":650,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/12\/equal-480x650.jpg"},"penci-thumb-small":{"file":"equal-263x175.jpg","width":263,"height":175,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/12\/equal-263x175.jpg"},"jr_insta_square":{"file":"equal-640x640.jpg","width":640,"height":640,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/12\/equal-640x640.jpg"}},"image_meta":{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0","keywords":[]}},"post":2341,"source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/12\/equal.jpg"},"translation":{"provider":"WPGlobus","version":"2.10.8","language":"es","enabled_languages":["en","es","de","fr","ru"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"es":{"title":false,"content":false,"excerpt":false},"de":{"title":false,"content":false,"excerpt":false},"fr":{"title":false,"content":false,"excerpt":false},"ru":{"title":false,"content":false,"excerpt":false}}},"_links":{"self":[{"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/posts\/2341"}],"collection":[{"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/comments?post=2341"}],"version-history":[{"count":1,"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/posts\/2341\/revisions"}],"predecessor-version":[{"id":2343,"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/posts\/2341\/revisions\/2343"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/media\/2342"}],"wp:attachment":[{"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/media?parent=2341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/categories?post=2341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/tags?post=2341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}