tags:

views:

511

answers:

3

I'm a MATLAB beginner. Here's the problem:

>> a = floor(7/2.5)

a =

      2.00

>> b = rem(7,2.5)

b =

      2.00

>> c = floor(b/2)

c =

         0

c should be 1, right? Why is it 0???

It is different when b = 2 is entered directly as follows:

>> b = 2

b =

      2.00

>> c = floor(b/2)

c =

      1.00
+6  A: 

In two words: truncation errors.

You're right, c should be 1.0 in exact arithmetic. However, since you used a float in the arguments of rem, you get the answer as a float. Apparently, b is not exactly 2, but 2.0, which means that it is a double very close to 2. Therefore, b/2 becomes the double 1.0, apparently in this case its value is slightly less than one, giving you a 0 as the integer value. If you want to prevent this, use both floor and ceil, and compare the values.

If you want to convert the answer to integer, just use round instead of floor.

Martijn
Thanks a lot. I converted the result of rem function into single. Then, at least my problem has been solved.
convert the result of rem to an int. It always will be (just like floor, ceil, and round are always whole numbers)
KitsuneYMG
Careful with "convert to integer" terminology: round does not convert to an integer type, but rather returns the nearest integer value, still as a double. This is usually what you want. The actual Matlab integer types like int32 have higher precedence than double, and will cause all subsequent operations on them to be done with integer arithmetic.
Andrew Janke
+3  A: 

If you add the line

d = b-a

to your example you'll see the result

    d =

 -4.4409e-016

meaning Matlab calculated a number close to, but not exactly, 2 for b. This comes up quite a bit in working with floating point numbers. Try

help eps

for more information.

mtrw
Thank you for the tip.
+1  A: 

Numerical issues of this sort are also dealt with in the MATLAB FAQ

Edric

related questions