Why does MATLAB sprintf('%.15g',-1*0) print '-0'? [duplicate] - matlab

This question already has answers here:
Small negative number maintains sign after rounding
(2 answers)
Closed 3 years ago.
Why am I getting the following in MATLAB? Tried in 2015b and 2019a and same result.
sprintf('%.15g',-1*0)
ans =
'-0'

format hex shows you what's going on - IEEE double precision numbers can have negative zero.
>> format hex
>> 0
ans =
0000000000000000
>> -0
ans =
8000000000000000

Related

Matlab Number (Figure) Representation [duplicate]

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I am confused with how numbers are represented in Matlab.
Considering this sequence of computations, carried out using 64-bit arithmetic in
Matlab:
format longg
i = 0:8;
a = sin(i*(pi/2))
When doing these calculations by hand, I would expect to see the results a = 0, 1, 0, –1, 0,
etc. But that is not what Matlab shows. Why not? Is it because pi is not in machine number?
In addition,
format long
a = 2^(60);
b = 20000;
c = a + b;
d = c – a
When doing these calculations by hand, I would expect to see the result d = 20000. But that is not what Matlab returns. Why not?

Why if `t(n)==0.06`, in my command window cannot displaying 0.06? [duplicate]

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I have MATLAB code
clear all;
clc;
t=0:0.001:0.1;
N=length(t);
for n=1:N
if t(n)==0||t(n)==0.02||t(n)==0.04||t(n)==0.06||t(n)==0.1
fprintf('%5.5f\n',t(n));
end
end
The result is
Why if t(n)==0.06, in my command window cannot displaying 0.06?
Does my MATLAB have an error? I'm using MATLAB R2014b
It's a numerical error because the 0.001 increments will not be precisely 0.001 under the hood.
The same thing happens in 2020b:
>> t = 0:0.001:0.1;
>> t(61)
ans =
0.0600
>> t(61) == 0.06
ans =
logical
0
If you want to compare floats, use some kind of tolerance like eps instead of ==:
>> (t(61) - 0.06) < eps
ans =
logical
1

How to round symbolic expressions to N digits in Matlab [duplicate]

This question already has an answer here:
Rounding to n significant digits
(1 answer)
Closed 5 years ago.
When you try rounding a symbolic expression to N digits you get the error messages below:
>> format long
>> syms x;
>> round(x, 10)
Error using sym/round
Too many input arguments.
>> round(vpa(pi), 10)
Error using sym/round
Too many input arguments.
So how do you make this work ?
Here it is how you do
>> syms x; N = 6;
>> round(pi*10^N)/vpa(10^N)
ans =
3.141593
>> round(x*10^N)/vpa(10^N)
ans =
0.000001*round(1000000*x)

Error about Subscript indices of sum function for float matrix matlab [duplicate]

This question already has an answer here:
matlab strange behaviour of function sum()
(1 answer)
Closed 7 years ago.
I have a matrix 202x141x3 that called M matrix. I want to perform the code
sum(M,3);
However, I got error such as
Subscript indices must either be real positive integers or logicals.
I debugged and saw the content in M matrix, it has not problem. But I cannot use the above function. Could you see my M.mat matrix at here and let me know how can I use above function for my M.mat matrix
You did set a variable under the name sum
a = [1 2 3 ; 4 -5 6; 7 8 9]
sum = 1;
>> sum(a)
Subscript indices must either be real positive integers or logicals.
clear sum;
>> sum(a)
ans =
12 5 18

Value does not change for any obvious reason - Matlab [duplicate]

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Closed 7 years ago.
I wrote the following code. The problem is that at the specific value of 0.288,
T turns to zero for no obvious reason. Any explanation? The weird thing is that when I change it to 0.28 it works fine.
time=(0:0.002:0.560);
time_f=reshape(time,281,1);
time1=0;
time2=0;
for i=1:1
for j=1:281
T=time_f(j,i);
if (i==1) && (T==0.288);
time1=T;
end
end
end
If you test the code you will find that the time1 value will be zero and if you change T to 0.28 it will work.
The answer is simple
>> time_f(time_f == 0.288)
ans =
Empty matrix: 0-by-1
Your matrix doesn't contain the value 0.288
This is due to float precision, so instead of being 0.288, your value is 0.287999999 for example.
Just use roundn, which round to a specified number of significant digits
>> time_f = roundn(time_f,-3);
>> find(time_f == 0.288)
ans =
145
If you don't have Mapping toolbox you can do
>> time_f = round(time_f * 1000) / 1000
You are using floating point arithmetic, which has certain precision errors. Your matrix does not contain exactly the value 0.288.