- 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
what's the difference between writing 1.0 and 1. or 2.0 and 2. ?
43 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
what's the difference between writing 1.0 and 1. or 2.0 and 2. ?
0 Commenti
Risposte (4)
Jan
il 18 Mag 2018
Modificato: James Tursa
il 18 Mag 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:
0 Commenti
KSSV
il 26 Mar 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 Commento
Stephen23
il 26 Mar 2016
Modificato: Stephen23
il 18 Mag 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
Muhammad Usman Saleem
il 26 Mar 2016
Modificato: Muhammad Usman Saleem
il 26 Mar 2016
only the difference is of more significant no. Both are same, but 1.0 has two significant figures than 1.
1 Commento
Jan
il 18 Mag 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.
Ced
il 26 Mar 2016
Modificato: Ced
il 26 Mar 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".
Vedere anche
Categorie
Scopri di più su Startup and Shutdown in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!