Matlab gives wrong answer on mod(), isequal(), floor() function with a vector parameter

Matlab gives wrong answer on mod(), isequal(), floor() function with a vector parameter

What seems to be an integral 1 coming from linprog, actually is a floating point number very close to 1, but not equal to it. Its just that you (by default) cannot tell the difference by outputting the number in MATLAB. See this Question about comparing floating point values. Also, see the MATLAB manual page about display format of numbers.

By default, MATLAB displays numbers using format short:

format sets the display of floating-point numeric values to the default display format, which is the short fixed decimal format. This format displays 5-digit scaled, fixed-point values.

So, 1 – 1e–10 would be displayed as 1 even though it is not equal to 1. Likewise, mod(1 – 1e–10, 1) would be displayed as 1, even though it really is 1 – 1e–10.

If you manually set x to its displayed representation, all the less significant digits are truncated, “strangely” giving you the expected results.

Check x after typing the command format long.

Matlab gives wrong answer on mod(), isequal(), floor() function with a vector parameter

Leave a Reply

Your email address will not be published. Required fields are marked *