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

41 views (last 30 days)
what's the difference between writing 1.0 and 1. or 2.0 and 2. ?

Answers (4)

Jan
Jan on 18 May 2018
Edited: James Tursa on 18 May 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 on 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 Comment
Stephen23
Stephen23 on 26 Mar 2016
Edited: Stephen23 on 18 May 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

Sign in to comment.


Muhammad Usman Saleem
Muhammad Usman Saleem on 26 Mar 2016
Edited: Muhammad Usman Saleem on 26 Mar 2016
only the difference is of more significant no. Both are same, but 1.0 has two significant figures than 1.
  1 Comment
Jan
Jan on 18 May 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.

Sign in to comment.


Ced
Ced on 26 Mar 2016
Edited: Ced on 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".
  1 Comment
Stephen23
Stephen23 on 26 Mar 2016
Edited: Stephen23 on 18 May 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:

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!