MATLAB Answers

About floor function problem.

Asked by C Zeng

C Zeng (view profile)

on 23 Oct 2012
Accepted Answer by Matt J

Matt J (view profile)

floor(1.999999999999)=1 floor(1.99999999999999999999999999)=2, why is that?

Floor should return the lower integer right? Thanks.

Products

No products are associated with this question.

2 Answers

Answer by Matt J

on 23 Oct 2012
Edited by Matt J

Matt J (view profile)

on 23 Oct 2012
Accepted answer

If that confuses you, this probably will too:

```>> isequal(1.99999999999999999999999999, 2)
```
```ans =
```
`       1`

Anyway, it has nothing to do with the FLOOR command. It's because your big long decimal can't be distinguished from 2 in floating point.

C Zeng

C Zeng (view profile)

on 23 Oct 2012

Thanks a lot Matt!

Would you please answer my previous question?

Matt J

Matt J (view profile)

on 23 Oct 2012

What was your "previous question"?

Matt J

Matt J (view profile)

on 23 Oct 2012

This one contains an overloaded floor function, if that's what you mean

http://www.mathworks.com/matlabcentral/fileexchange/6446-multiple-precision-toolbox-for-matlab

Azzi Abdelmalek (view profile)

Answer by Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 23 Oct 2012

Just try without floor

```a=1.99999999999999999999999999
```

Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 23 Oct 2012

I am not sur what you mean by converting to 2 digits, I think, with 2 digits, you will have four possible digital numbers, And you need a min and max value to be able to do this conversion. Can you explain, or post another question?

Walter Roberson

Walter Roberson (view profile)

on 23 Oct 2012

If you are starting with an integer, then dividing by a power of 2 can never result in this kind of round-off. Powers of 2 are represented exactly in binary floating point numbers, and dividing by a power of two effectively only changes the internal binary exponent without changing the mantissa. If you are running into this kind of round-off then either you are not starting with an integer or you are not dividing by a power of 2.

C Zeng

C Zeng (view profile)

on 26 Oct 2012

Thanks, Walter, though I do not understand your point. I am transferring an integer like N to 2-digits. I want to divide it by 2 to determine if the entry is 0 or 1. Floor function does not make sufficient proximity to this problem.

Join the 15-year community celebration.

Play games and win prizes!

Learn more

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!