# Floating point errors can kill you!

**URL:** https://fortran-lang.discourse.group/t/floating-point-errors-can-kill-you/7748
**Category:** Humor
**Created:** [April 1, 2024, 9:16pm UTC](https://fortran-lang.discourse.group/t/floating-point-errors-can-kill-you/7748 "2024-04-01T21:16:14Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)
#### Post date: [April 1, 2024, 9:16pm UTC](https://fortran-lang.discourse.group/t/floating-point-errors-can-kill-you/7748/1 "2024-04-01T21:16:14Z")

</div>

[![](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/1/1ef347a00e552fe7e669347aa0bf2b71fcff254f.jpeg "The Minecraft boat-drop mystery") ](https://www.youtube.com/watch?v=ei58gGM9Z8k)

---

<div class="post-metadata">

### Author: ![Machalot](https://avatars.discourse-cdn.com/v4/letter/m/ebca7d/32.png) [@Machalot](https://fortran-lang.discourse.group/u/Machalot)
#### Post date: [April 2, 2024, 6:03am UTC](https://fortran-lang.discourse.group/t/floating-point-errors-can-kill-you/7748/2 "2024-04-02T06:03:52Z")

</div>

**Root cause:** accumulation of floating point rounding error combined with a tolerance on the equality check  
**Lesson:** use exact equality checks with floating point numbers

---

<div class="post-metadata">

### Author: ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)
#### Post date: [April 2, 2024, 2:16pm UTC](https://fortran-lang.discourse.group/t/floating-point-errors-can-kill-you/7748/3 "2024-04-02T14:16:49Z")

</div>

> [@Machalot](#):
>
> **Lesson:** use exact equality checks with floating point numbers

Oh please don’t. This will bite you far more often than the example in the video. You’ll be totally scratching your head wondering why

```auto
x = 0.1
y = 0.2
z = 0.3
if (x + y == z) print *, "Success!"

```

doesn’t seem to work.

---

<div class="post-metadata">

### Author: ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)
#### Post date: [April 2, 2024, 2:23pm UTC](https://fortran-lang.discourse.group/t/floating-point-errors-can-kill-you/7748/4 "2024-04-02T14:23:34Z")

</div>

0.1 is not 0.1,  
but 0.125 is 0.125. 🤖

---

<div class="post-metadata">

### Author: ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)
#### Post date: [April 2, 2024, 2:26pm UTC](https://fortran-lang.discourse.group/t/floating-point-errors-can-kill-you/7748/5 "2024-04-02T14:26:26Z")

</div>

If you watch the video, the real problem seems to be a form of race condition. There are two flags, one that is set with a tolerance and one that is not.

---

<div class="post-metadata">

### Author: ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)
#### Post date: [April 2, 2024, 2:28pm UTC](https://fortran-lang.discourse.group/t/floating-point-errors-can-kill-you/7748/6 "2024-04-02T14:28:46Z")

</div>

Oh the finitely countable joys of floating point numbers!

---

<div class="post-metadata">

### Author: ![Machalot](https://avatars.discourse-cdn.com/v4/letter/m/ebca7d/32.png) [@Machalot](https://fortran-lang.discourse.group/u/Machalot)
#### Post date: [April 2, 2024, 4:23pm UTC](https://fortran-lang.discourse.group/t/floating-point-errors-can-kill-you/7748/7 "2024-04-02T16:23:18Z")

</div>

> [@everythingfunctional](#):
>
> Oh please don’t. This will bite you far more often than the example in the video. You’ll be totally scratching your head wondering why

Note that I posted that on April 1st (at least in my timezone). 😆

---

<div class="post-metadata">

### Author: ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)
#### Post date: [April 2, 2024, 4:38pm UTC](https://fortran-lang.discourse.group/t/floating-point-errors-can-kill-you/7748/8 "2024-04-02T16:38:01Z")

</div>

> [@Machalot](#):
>
> Note that I posted that on April 1st (at least in my timezone). 😆

phew 😅 I didn’t read it till after, so wasn’t sure.

---

<div class="post-metadata">

### Author: ![rwmsu](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@rwmsu](https://fortran-lang.discourse.group/u/rwmsu)
#### Post date: [April 2, 2024, 8:39pm UTC](https://fortran-lang.discourse.group/t/floating-point-errors-can-kill-you/7748/9 "2024-04-02T20:39:19Z")

</div>

Yes writing sophisticated code like this

```auto
real(REAL64) :: a
real :: b
If ((a-b) == 0.0) then
  a = 1.0
  b = 2
End If

```

is best left to us experienced programmers 😄

Surprisingly, I still see nonsense like this in codes written in the last few years by people who should know better.

---

<div class="post-metadata">

### Author: ![conradoat](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/conradoat/32/1537_2.png) [@conradoat](https://fortran-lang.discourse.group/u/conradoat)
#### Post date: [April 3, 2024, 1:48pm UTC](https://fortran-lang.discourse.group/t/floating-point-errors-can-kill-you/7748/10 "2024-04-03T13:48:02Z")

</div>

Isn’t this a result of the fact tha 0.125 can be exactly represented in binary but 0.1 don’t?

---

<div class="post-metadata">

### Author: ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)
#### Post date: [April 3, 2024, 1:58pm UTC](https://fortran-lang.discourse.group/t/floating-point-errors-can-kill-you/7748/11 "2024-04-03T13:58:13Z")

</div>

Yes :

- 0.125 = 1/8 = 0b0.001
- 0.1 = 1/10 = 0b0.0001100110011001101…

A number with a finite number of digits in a base can have an infinite number of digits in another base.

But if there is a finite number of digits in binary, there should always be a finite number in decimal, as it is a sum of 1/2^n. But in other bases?

---

<div class="post-metadata">

### Author: ![conradoat](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/conradoat/32/1537_2.png) [@conradoat](https://fortran-lang.discourse.group/u/conradoat)
#### Post date: [April 3, 2024, 2:13pm UTC](https://fortran-lang.discourse.group/t/floating-point-errors-can-kill-you/7748/12 "2024-04-03T14:13:18Z")

</div>

Dont know the answer, but I think with larger bases there are more number that can be represented exactly.

---

<div class="post-metadata">

### Author: ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)
#### Post date: [April 3, 2024, 3:19pm UTC](https://fortran-lang.discourse.group/t/floating-point-errors-can-kill-you/7748/13 "2024-04-03T15:19:00Z")

</div>

> [@conradoat](#):
>
> I think with larger bases there are more number that can be represented exactly.

It sounds reasonable.

---

<div class="post-metadata">

### Author: ![Carltoffel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/carltoffel/32/1680_2.png) [@Carltoffel](https://fortran-lang.discourse.group/u/Carltoffel)
#### Post date: [April 4, 2024, 12:30pm UTC](https://fortran-lang.discourse.group/t/floating-point-errors-can-kill-you/7748/14 "2024-04-04T12:30:07Z")

</div>

I guess it’s a matter of prime factors in the base. Base two has only one prime factor and therefore can only exactly represent numbers which can be represented as the sum \sum\_{k=m}^{n} a\_k2^k.  
Base ten has two prime factors: 2 and 5. Therefore, every number, that can be represented exactly with a finite amount of digits with base two, can be represented exactly with base 10, but not the other way around.

---

<div class="post-metadata">

### Author: ![conradoat](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/conradoat/32/1537_2.png) [@conradoat](https://fortran-lang.discourse.group/u/conradoat)
#### Post date: [April 4, 2024, 3:17pm UTC](https://fortran-lang.discourse.group/t/floating-point-errors-can-kill-you/7748/15 "2024-04-04T15:17:03Z")

</div>

Makes a lot of sense
