About floor function problem.

floor(1.999999999999)=1 floor(1.99999999999999999999999999)=2, why is that?
Floor should return the lower integer right? Thanks.

 Respuesta aceptada

Matt J
Matt J el 23 de Oct. de 2012
Editada: Matt J el 23 de Oct. de 2012

0 votos

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.

6 comentarios

C Zeng
C Zeng el 23 de Oct. de 2012
Oh, yes, Matt. Then is there a way to adjust the precision in Matlab? Thanks.
Matt J
Matt J el 23 de Oct. de 2012
There are FEX submissions which support alternative precisions. Here is one example
I tend to agree with what the author says there, however: If you can't do it with ordinary double precision, you're probably doing something wrong.
Walter Roberson
Walter Roberson el 23 de Oct. de 2012
To get higher precision, use the Symbolic Toolbox, or the Fixed Point Toolbox, or one of the FEX contributions for higher precision numbers.
MATLAB itself is limited by its use of (hardware) IEEE 754 Double Precision numbers.
C Zeng
C Zeng el 23 de Oct. de 2012
Thanks a lot Matt!
Would you please answer my previous question?
Matt J
Matt J el 23 de Oct. de 2012
What was your "previous question"?
Matt J
Matt J el 23 de Oct. de 2012
This one contains an overloaded floor function, if that's what you mean

Iniciar sesión para comentar.

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 23 de Oct. de 2012

0 votos

Just try without floor
a=1.99999999999999999999999999

8 comentarios

C Zeng
C Zeng el 23 de Oct. de 2012
I have to floor it, Azzi. I want an integer.
Azzi Abdelmalek
Azzi Abdelmalek el 23 de Oct. de 2012
Editada: Azzi Abdelmalek el 23 de Oct. de 2012
I mean you don't need to floor it, you will find 2
C Zeng
C Zeng el 23 de Oct. de 2012
I still want to convert 1.9999999999999 to 1. How can I do it?
Azzi Abdelmalek
Azzi Abdelmalek el 23 de Oct. de 2012
Editada: Azzi Abdelmalek el 23 de Oct. de 2012
Zeng. from where did you get 1.999999999999999? knowing that matlab don't allow it
C Zeng
C Zeng el 23 de Oct. de 2012
Azzi, I see. I want to convert a number y to 2-digit vector. I use y/2^(i-1) iteratively to convert y to the 2-digit expression. To make sure it display correct 0 or 1, I use floor function. However if the division is very close to 1, floor() will show 1 not 0.
I think I can modify it by comparing who is greater, y and 2^(i-1).
Azzi Abdelmalek
Azzi Abdelmalek el 23 de Oct. de 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 el 23 de Oct. de 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 el 26 de Oct. de 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.

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 23 de Oct. de 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by