The day I almost made a library on a bakery
Tags: beginners, floatingpoint, javascript
Published: 2018-06-29 16:40:06

---
title: The day I almost made a library on a bakery
published: true
description: You don't know where you can find your muse.
tags: beginner, floating-point, javascript
cover_image: https://s3.amazonaws.com/btoimage/prism-thumbnails/articles/20170818-2048-SudForno17.jpg-resize_then_crop-_frame_bg_color_FFF-h_1365-gravity_center-q_70-preserve_ratio_true-w_2048_.jpg
---

First thing first, I am not an expert in this area. I was just guessing that maybe this way I could avoid installing a library for a simple operation with small numbers. If you are looking for answers I recommend you this cool [floating-point guide](https://floating-point-gui.de/). But I still wrote this, hoping you'd laugh the same way employeers did.

Sometimes coding is so easy as walk through a flower garden, sometimes is challenging, but there are times that it's just:

```python
Python 3.6.3 (default, Oct 24 2017, 14:48:20) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 0.1 + 0.2
0.30000000000000004
```

I wasn't expecting that. Well, I'm lying, I really expected that. Because this "buggy" answer is just a common result of floating-point arithmetics.

First time, I encountered myself searching [why is this happening in Python](https://docs.python.org/3/tutorial/floatingpoint.html) and how should I solve it (`Decimal` package is your friend). But then, Node.js needs an answer too:

```
node v9.4.0
> 0.1 + 0.2
0.30000000000000004
```

I'm pretty sure there are libraries for JavaScript, but I don't want to add any other dependency to my package.json for a simple operation. There should be a tricky answer for this, I guess so. I grab my laptop and said *goodbye* for now.

Then I went to the bakery shop.

Buy groceries is relaxing, but with the bread is unique; the small backery at the corner has at least half of my summer playlist, being played as background music. Some clients and employers use to sing or at least whisper the songs. I am one of those. But that day was so full, so I decided to wait for my partner in one of the outside tables.

A friend of mine lent me *Fictions* from Jorge Luis Borges, I started to read with this relaxing music. Some lines of the second chapter of *Tlön, Uqbar, Orbis Tertius* mention about duodecimal and sexagesimal conversions. At this point, an idea passed through my inspired mind. I stopped and closed the book.

*What if just, instead of process this numbers by default (binary fraction), transform numbers into integers?*

*Computers are better calculating integers, and then turning back the result to decimals...*

*Eureka!*

I didn't realize I was thinking out loud. Everyone turned his head to me, surprised, of course. In reaction, I started to sing and dance after "that's my song!". Well, the show must continue. And then surprise turned into laughs.

Thank you, Shakira.

Back to my sit, and embarrassed for my scene, I sketch what I will code in the next lines:

We need to know how many decimals has this number:

``` javascript

function getExponential(num) {
  if (!!(num % 1)) {
    return num.toString().split('.')[1].length;
  }
  // just for integers
  return 0;
}

```

And then we can make a function to calculate the addition of two numbers like:

``` javascript

function add(a, b) {
  // we use the greater exponent
  const exp = Math.max(getExponential(a), getExponential(b));
  const intConversor = Math.pow(10, exp);
  return (a * intConversor + b * intConversor) / intConversor;
}

```

With subtraction is almost the same:

``` javascript

function subtract(a, b) {
  // we use the greater exponent
  const exp = Math.max(getExponential(a), getExponential(b));
  const intConversor = Math.pow(10, exp);
  return (a * intConversor - b * intConversor) / intConversor;
}

```

The number of decimals for `a` times `b`, is result of the sum of decimal's length for both numbers. Both numbers should be the minimum expresion of integers they could be:

``` javascript

function multiply(a, b) {
  const expA = getExponential(a);
  const expB = getExponential(b);
  const floatConversor = Math.pow(10, expA + expB);
  return (a * Math.pow(10, expA)) * (b * Math.pow(10, expB)) / floatConversor;
}

```

I'm thinking about decimals with division:

``` javascript

function divide(a, b) {
  // we use the greater exponent
  const exp = Math.max(getExponential(a), getExponential(b));
  const intConversor = Math.pow(10, exp);
  return a * intConversor / (b * intConversor);
}

```

Let's test it out:

```
node v9.4.0
> add(0.1, 0.2)
0.3
> subtract(0.1, 0.2)
-0.1
> multiply(0.1, 0.2)
0.02
> divide(0.1, 2)
0.05
```

Done! Finally, I can make operations without using any other external library. Mission accomplished. Time to get a reward, coffee! But, of course, so far bakery shop.


*Lead photo by Jesse Milns at Sud Forno.*