# Matlab basic - how to get around floating point?

Tags:
1. Aug 30, 2014

i'm trying to learn matlab and as such i stumbled here while looking something up

http://www.mathworks.com/help/matlab/matlab_prog/floating-point-numbers.html

so the questions is

how does someone avoid something like

e = 1 - 3*(4/3 - 1) (this is example 1 in the webpage above)

not being zero

or this not being zero

sin(pi) (this is in example 1 again)

or this being zero
sqrt(1e-16 + 1) - 1 (in example 2)

thank you very much!

2. Aug 30, 2014

### marcusl

Matlab is a numerical solver. If having a result like 1e-16 is not close enough to zero for you, you can increase the numerical accuracy that Matlab provides, at the cost of slowing computations down. If you really want exactly zero, use a symbolic calculation code like Mathematica or Maple. They do algebraic manipulations and evaluations the way you would do them in school.

3. Aug 30, 2014

### AlephZero

Last edited by a moderator: May 6, 2017
4. Aug 30, 2014

### FactChecker

This is a common problem. Just don't program any logic decisions based on equality of two real numbers. Always allow some tolerance when comparing two reals and apply the tolerance so that the safe decision will be made if the reals are within the tolerance.