- 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 次查看(过去 30 天)
显示 更早的评论
what's the difference between writing 1.0 and 1. or 2.0 and 2. ?
0 个评论
回答(4 个)
Jan
2018-5-18
编辑:James Tursa
2018-5-18
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 个评论
KSSV
2016-3-26
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 个评论
Stephen23
2016-3-26
编辑:Stephen23
2018-5-18
"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
2016-3-26
编辑:Muhammad Usman Saleem
2016-3-26
only the difference is of more significant no. Both are same, but 1.0 has two significant figures than 1.
1 个评论
Jan
2018-5-18
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
2016-3-26
编辑:Ced
2016-3-26
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".
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!