what's the difference between writing 1.0 and 1. or 2.0 and 2. ?

41 visualizaciones (últimos 30 días)
Tariq Raza
Tariq Raza el 26 de Mzo. de 2016
Editada: James Tursa el 18 de Mayo de 2018
what's the difference between writing 1.0 and 1. or 2.0 and 2. ?

Respuestas (4)

Jan
Jan el 18 de Mayo de 2018
Editada: James Tursa el 18 de Mayo de 2018
While 1. and 1.0 create completely equal values of type double, the abbreviated notation is not documented. I asked MathWorks' technical support some years ago, if 1. and .1 is guaranteed to work. The answer was, that only the formats, which can be created by sprintf, are documented to be accepted.
The abbreviated formats 1. and .1 worked in all Matlab releases (testes it since R4.1) and under Windows, Linux, MacOS 7 to 9 and MacOS X, but this is not documented. In addition it is less clear, most of all when the elementwise operators are used without surrounding spaces like in 2.*x or .2.*x . Fortunately this does not influence the output, because it is a multiplication with a scalar in both cases. But typing a space or a 0 is very cheap and I do not see any reason to omit them. We are not writing code for 1kB machines anymore, where saving a byte matters.
When writing C code, "1" and "1.0" create an integer or double constant, but in Matlab both are doubles.
I recommend to write code with the highest possible clarity to improve the readability:
  • If you want to emphasize that a constant is a floating point value, write "1.0" instead of "1" or "1."
  • Add a leading 0 before the decimal point: "0.1" instead of ".1"
  • Add spaces around operators, especially around the elementwise ".*" and "./" when there is a leading numerical constant

KSSV
KSSV el 26 de Mzo. de 2016
There is no difference both (1.0 or 1.) are same in mathematics. Both are real numbers with same magnitude. In case of memory, 1.0 may occupy few bytes extra.
  1 comentario
Stephen23
Stephen23 el 26 de Mzo. de 2016
Editada: Stephen23 el 18 de Mayo de 2018
"In case of memory, 1.0 may occupy few bytes extra."
MATLAB uses IEC 754 floating point numbers, and the default number type is double. This means these two numbers will use exactly the same amount of memory: eight bytes. This is also easy to test:
>> A = 1.;
>> B = 1.0;
>> whos A B
Name Size Bytes Class Attributes
A 1x1 8 double
B 1x1 8 double

Iniciar sesión para comentar.


Muhammad Usman Saleem
Muhammad Usman Saleem el 26 de Mzo. de 2016
Editada: Muhammad Usman Saleem el 26 de Mzo. de 2016
only the difference is of more significant no. Both are same, but 1.0 has two significant figures than 1.
  1 comentario
Jan
Jan el 18 de Mayo de 2018
No, both are absolutely identical:
a = 1
b = 1.0
typecast(a, 'uint8')
typecast(b, 'uint8')
Both are IEEE754 floating point variables of type double.

Iniciar sesión para comentar.


Ced
Ced el 26 de Mzo. de 2016
Editada: Ced el 26 de Mzo. de 2016
There is not difference in matlab. Sometimes, people like to write "1." instead of 1 to make it easier to port to/from other programming languages later, as it is a sign that the number should be a double (or float), as opposed to an integer or bool. But that's purely for readability, there is no programmatic reason within matlab.
They also use the same amount of memory, since both 1 and 1.0 use 8 Bytes (is it 4 Bytes on a 32Bit version of Matlab...? not sure). Matlab even evaluates 1.0 as "true".
  1 comentario
Stephen23
Stephen23 el 26 de Mzo. de 2016
Editada: Stephen23 el 18 de Mayo de 2018
@Ced: why would the size change for 32 bit versions? A double is always 8 bytes, regardless of the OS. The size of a double is explained in the documentation:

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by