0

I am performing operations in matrix multiplication where I have floating point numbers. Due to the precision in MATLAB I am getting incorrect output. For example, in the below

a = 1+1e-18
a = 1

a is rounding to 1 but I want all of the decimals places to be kept for my calculation such that it does not round to one. How can I get MATLAB to keep all of the decimal places when performing my calculations.

  • 1
    Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Mad Physicist Jan 09 '19 at 03:56
  • [Related](https://stackoverflow.com/questions/26358785/matlab-float-accuracy): If you have the symbolic toolbox, you can use [vpa](https://www.mathworks.com/help/symbolic/vpa.html) (variable-precision arithmetic). (also @MadPhysicist) – beaker Jan 09 '19 at 17:33

1 Answers1

0

MATLAB does not natively support rational data types or extended floating point beyond double. Functions such as rat and rats look promising at first, but don't offer the functionality to work with the result out of the box.

You could get some mileage by retaining the numerator and denominator of your numbers separately. If you then implement the operators that you need for fractions, you'd have much higher precision in your final result.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264