### Approximately and Essentially Equal

I stumbled upon some old CS text the other day and was reminded of two functions `approximatelyEqual(float, float, float)` and `essentiallyEqual(float, float, float)`. Since JavaScript uses a floating point system (IEEE) that is not exact, sometimes it returns numbers like 1.00000001 or 0.99999999, instead of 1. These functions can be used to allow floating point errors or when a margin of error is simply acceptable between two numbers.

### How to do it…

The code for the two functions:

```function approximatelyEqual(/* float */ a, /* float */ b, /* float */ epsilon) {
var A = Math.abs(a), B = Math.abs(b);
var Math.abs(A - B) <= (A < B ? B : A) * epsilon;
}

function essentiallyEqual(/* float */ a, /* float */ b, /* float */ epsilon) {
var A = Math.abs(a), B = Math.abs(b);
var Math.abs(A - B) <= (A > B ? B : A) * epsilon;
}```

An simple example:

```var a = 95.1, b = 100.0;
alert (approximatelyEqual(a, b, 0.05)); // this is true, 100 * 0.05 > 100 - 95.1
alert (essentiallyEqual(a, b, 0.05)); // this is false, 95.01 * 0.05 < 100 - 95.1 ```

### How it works…

The arguments required for both functions are three numbers: the first and second arguments can be either the calculated value, or the target comparison value, and the third is the precision. The approximatelyEqual function uses the larger of the two values and multiples it by epsilon to determine the margin of error. The essentiallyEqual function uses the smaller of the two values and multiples it by epsilon to determine the margin of error. Therefore, unless values A and B are equal, the essentiallyEqual function will always require the values to be more precise than the approximatelyEqual function.

### There’s more

Now that I understand git a lot better, I will be updating the Numbers package in the YUI 3 gallery with these two functions.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6890568,"math_prob":0.99791026,"size":1653,"snap":"2021-31-2021-39","text_gpt3_token_len":421,"char_repetition_ratio":0.14372347,"word_repetition_ratio":0.2578397,"special_character_ratio":0.30127043,"punctuation_ratio":0.18047337,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9993827,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-23T16:32:04Z\",\"WARC-Record-ID\":\"<urn:uuid:dcfd92fc-d748-41cf-b9f7-f20545bfcd06>\",\"Content-Length\":\"25601\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:47694ae2-ac01-4ad2-b978-338286793371>\",\"WARC-Concurrent-To\":\"<urn:uuid:bebd07fa-e273-476f-b339-f2084c8777ca>\",\"WARC-IP-Address\":\"172.67.128.206\",\"WARC-Target-URI\":\"https://mattsnider.com/approximately-and-essentially-equal/\",\"WARC-Payload-Digest\":\"sha1:WELBCT77UJV4W4UWPFEG4XERO7C6RORY\",\"WARC-Block-Digest\":\"sha1:O3UYOZADOC5ODHYAONAFFP72UZALOHKL\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046149929.88_warc_CC-MAIN-20210723143921-20210723173921-00612.warc.gz\"}"}