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

조회 수: 41 (최근 30일)
Tariq Raza
Tariq Raza 2016년 3월 26일
편집: James Tursa 2018년 5월 18일
what's the difference between writing 1.0 and 1. or 2.0 and 2. ?

답변 (4개)

Jan
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:
  • 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 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
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
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
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
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".
  댓글 수: 1
Stephen23
Stephen23 2016년 3월 26일
편집: Stephen23 2018년 5월 18일
@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:

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by