Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab basic - how to get around floating point?

Tags:
  1. Aug 30, 2014 #1
    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. jcsd
  3. Aug 30, 2014 #2

    marcusl

    User Avatar
    Science Advisor
    Gold Member

    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.
     
  4. Aug 30, 2014 #3

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

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

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab basic - how to get around floating point?
  1. Matlab basics (Replies: 2)

  2. Basic MATLAB (Replies: 10)

Loading...