# Why does it happen?

**URL:** <https://discourse.processing.org/t/why-does-it-happen/19772>\
**Category:** Beginners\
**Created:** [April 14, 2020, 2:05pm UTC](https://discourse.processing.org/t/why-does-it-happen/19772 "2020-04-14T14:05:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Orlando](https://avatars.discourse-cdn.com/v4/letter/o/c6cbf5/32.png) [@Orlando](https://discourse.processing.org/u/Orlando)\
**Post date:** [April 14, 2020, 2:05pm UTC](https://discourse.processing.org/t/why-does-it-happen/19772/1 "2020-04-14T14:05:49Z")

</div>

Hello. Why my program doesn’t return the sequel 0.0, 0.1, 0.2, 0.3, 0.4, 0.5… and so on ??? Take a look:

```auto
var c;
function setup() {
  createCanvas(400, 400);
  c = 0;
}
function draw() {
   c += 0.1
   print(c);
}

```

And the numbers in console are:

- 0.1
- 0.2
- 0.30000000000000004
- 0.4
- 0.5
- 0.6
- 0.7
- 0.7999999999999999
- 0.8999999999999999
- 0.9999999999999999
- 1.0999999999999999
- 1.2

… and so on.

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [April 14, 2020, 2:41pm UTC](https://discourse.processing.org/t/why-does-it-happen/19772/2 "2020-04-14T14:41:42Z")

</div>

I recently read about this issue in the **Nyhoffs’ book “Processing: An Introduction to Programming”.**  
Basically they write that “computers sometimes produce inaccurate results when fractions are involved.”  
They explain this is because **computers do not use a decimal (base-10) system, but rather they use a binary system (base-2).** If you have access to their book, see pp 61-62 for full explanation.

Or check out this explanation of conversion from decimal to binary:

> **[Converting from decimal to binary (video) | Khan Academy](https://www.khanacademy.org/math/algebra-home/alg-intro-to-algebra/algebra-alternate-number-bases/v/decimal-to-binary)**
>
> Learn for free about math, art, computer programming, economics, physics, chemistry, biology, medicine, finance, history, and more. Khan Academy is a nonprofit with the mission of providing a free, world-class education for anyone, anywhere.

And if you’re _very_ curious, there’s a long wikipedia on the topic:

> **[Binary-coded decimal](https://en.wikipedia.org/wiki/Binary-coded_decimal)**
>
> In computing and electronic systems, binary-coded decimal (BCD) is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of bits, usually four or eight. Special bit patterns are sometimes used for a sign or for other indications (e.g., error or overflow).
> In byte-oriented systems (i.e. most modern computers), the term unpacked BCD usually implies a full byte for each digit (often including a sign), whereas packed BCD typically encodes two deci...

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [April 14, 2020, 5:37pm UTC](https://discourse.processing.org/t/why-does-it-happen/19772/3 "2020-04-14T17:37:55Z")

</div>

Of course, you do know that you can format them?

```auto
var c;

function setup() {
  frameRate(2);
  createCanvas(400, 400);
  c = 0;
}

function draw() {
  background(255);
  c += 0.1
  text(nf(c, 0, 1), 10, 10);
}

```
