Why is the 100*0.07 equal to 6.9999....? [duplicate] - matlab

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Closed 5 years ago.
One of my friends wrote the following in Matlab and the outputs are little weird:
for p=0.01:0.01:0.1
100*p
end
The following was the output:
1
2
3
4
5
6.000000000000001
6.999999999999999
8
9
10
I'd like to know why there is a slight error? Does this mean that, the accuracy in the general case is also as poor as it is in this case?
EDIT:
We compared the numbers -- 7==6.999999999999999 and the output was 0. So, Matlab contradicts itself!

The problem is that 0.01 cannot be exactly represented in binary floating-point. Neither can 0.07.

Looks like a floating point precision "issue": http://www.lahey.com/float.htm

Try
x = ((0.07+1)*100) - 100
;-)

Related

Matlab Rounding Computation Error [duplicate]

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Closed 5 years ago.
Under Matlab 2014b, when making:
round((0.1:0.2:1)/0.2)
I obtain:
1 2 3 3 5
Instead of
1 2 3 4 5
Is there a way to fix such weird computation? Why this happens? Why so weird? This repeats for other values, not only n=0.2.
EDIT: I checked the duplicate but, do the scenario changes when using ceil so the floating point threshold is not in the half integer (0.5, 1.5, 2.5) but in the integer (1.0, 2.0, 3.0)? What if i use some other representation, like single? Or any other more predictable?
since the question got reopened, I'll briefly mention that adding 1.3+0.2 with floats can result in 2.49999999999 or in 2.5000000001 etc. the round will act according to the floating point error.

Why 0.09==(0.1-0.01) gives false as an answer in MATLAB? [duplicate]

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Closed 5 years ago.
The expression 0.09==0.1-0.01 gives false, but for example 0.08==0.1-0.02 is true and 0.19==0.2-0.01 is also true. What is the problem?
This is a classical rounding error. Usually if you compare floats you use an Epsilon, which defines how close two floating point numbers must be to each other to assume they are the same number.
See:
http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-0.2-0.1.28or_similar.29_not_equal_to_zero.3F

Matlab function find does not work on floating point [duplicate]

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Closed 5 years ago.
tfinal = 10;
dt_fine = 0.0000001;
tvec_fine = [0:dt_fine:tfinal];
find(tvec_fine==0.1)
ans =
Empty matrix: 1-by-0
Why does the above code not find the index where tvec_fine has the entry 0.1. It clearly has the entry based on the definition of tvec_fine.
This is because floating point has a narrow precision, and 0.1 could end up being 0.10000001 vs 0.1000000002 for example.
This is a very rough description just to give you an idea, as pointed in the comments more developed answers and solutions are already available on SO.

MATLAB: fast creation of vector of indices [duplicate]

This question already has answers here:
Run-length decoding in MATLAB
(4 answers)
Closed 6 years ago.
I have a variable distr=[0 3 1 0 2];, and I have a variable full which should contrain distr(i) times i, for all i.
In this example, i want:
full=[2 2 2 3 5 5];
because distr(2)=3, therefore 3x 2, and so on.
Of course I can do it in a for-loop:
full=zeros([1,sum(distr)]);
cc=1;
for i=1:length(distr)
curr=distr(i);
full(cc:cc+curr-1)=i*ones([1,curr]);
cc=cc+curr;
end
but that is very slow. Do you know of a fast way, using MATLAB's awesome array-oriented style? Thanks!
Not sure, but maybe this will work. I can't check it since I currently don't have MATLAB:
full_tmp = arrayfun(#(i,n) i*ones(1,n),1:length(distr),distr,'uniformoutput',false);
full = cat(2,full_tmp{:});

Matlab precision [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 have a question about the precision in Matlab. I searched the questions here and they mention that there are precision problems when using very large or very small numbers and other posts looking at the comparison of numbers. My case is a bit different
I do not have that, my numbers have only 4 decimal places and I want them to add up to one.
Example:
Initial data:
aeb =
0.231215677537590 0.470472652172102 0.203009018534716
0.087759104877338 0.007588684370711
Then I rounded it to get 4 decimals:
aeb = round(aeb*10000)/10000
0.231200000000000 0.470500000000000 0.203000000000000
0.087800000000000 0.007600000000000
Then I identify the largest number and replace it by the difference with one
[~, idx]=max(aeb);
aeb(idx)=0;
aeb(idx)= 1 - sum(aeb);
But when I then do:
1 - sum(aeb)
1.110223024625157e-16
Does anyone know how I can make them add to one ? I just want 3-5 decimal places.
Their sum is within machine epsilon of the number one. The short answer is that any difference that small is indistinguishable from zero.
A practical point to be aware of....
In a lot of computing contexts, you don't want to test for equality, you want to test if the difference is within a certain tolerance. Eg.
abs(x - y) < tol