Floating point

Here is one of the best questions I had from one of my students (from the first part of the course):


x <- seq(-10, 10, length = 30)

How does it comes that:

> sum(x)
[1] 1.953993e-14

It should be zero right?

Well it happens because R, just like most computer system, uses floating points. It’s an approximation.

If you feel uncomfortable about it, just think that 1.953993e-14 equals 0.00000000000001953993. That is, if your units where trillions of USD, sum(x) is less than 2 cents.

By the way, if you try perform the same calculation with MS Excel, you’ll find 1.438849e-13 which is (around) 7.4 times larger.

Aucun commentaire:

Enregistrer un commentaire