Why does MATLAB change the values while taking data from a file?

5 vues (au cours des 30 derniers jours)
Sanam Limbu
Sanam Limbu le 12 Sep 2018
Hi, there! I am trying to write a code to change the missing values in a data (numbers with all digits 9) to 1111. This code works for most of those numbers like 999, 9999, 99999, but not for few numbers (99.99, 0.9999, 0.999999, 0.0000999, 0.99999999, 0.000000999, 0.000000000999) which I have kept on the file 'Anomalous numbers.txt'. So when I take data from that file, for instance, the first number 99.99 is displayed as 9.998999999999999e+01. Some numbers are changed while taking input like that, while others change later on after certain operations, which are meant to only change the order of the numbers. Code:
format longE
file = load('Anomalous numbers.txt');
[m, n] = size(file);
for j = 1:n
c = file(:, j);
disp(c);
absValue = abs(c); % ---- finding the order of numbers
logValue = log10(absValue);
order = floor(logValue);
divide = power(10, order);
for i = 1:m
A(i) = absValue(i)/divide(i); % ---- changing numbers to order of 1
x = A.';
x = transpose(A);
if(x(i) > 9) % ---- missing data changed to 1111
if(x(i) > 9.9)
if(x(i)==9.99||x(i)==9.999||x(i)==9.9999||x(i)==9.99999||x(i)==9.999999||x(i)==9.9999999||x(i)==9.99999999||x(i)==9.999999999||x(i)==9.9999999999)
c(i) = 1111;
end
end
end
end
file(:, j) = c;
end
disp(x);
disp(file);
save -ascii 'processedData.lst' 'file'
I have no idea how I can solve this problem, so any help would be highly appreciated. Please pardon me if I have not asked the question properly, as I am new to this platform, but I have done all to find an answer to this. Thanks!
  2 commentaires
Rik
Rik le 12 Sep 2018
You can solve this problem by not using a modern computer. Computers generally use floats when not using integers, which have rounding errors when there is not a perfect binary representation. I doubt there is a way to solve this problem without keeping the numbers as text.
Stephen23
Stephen23 le 12 Sep 2018
"I doubt there is a way to solve this problem without keeping the numbers as text."
Decimal floating point numbers, as defined in IEEE 754-2008, would probably allow this.

Connectez-vous pour commenter.

Réponses (1)

Stephen23
Stephen23 le 12 Sep 2018
"Why does MATLAB change the values while taking data from a file?"
Because those exact values cannot be stored as binary floating point numbers, in exactly the same way that you cannot write 1/3 as a decimal number with a finite number of digits. This is not specific to MATLAB, it is because of how all contemporary computers store decimal values as binary floating point numbers. You will have to change your algorithm to take into account the floating point error: you should never test for equality of binary floating point numbers, always compare the difference of floating point numbers against a tolerance:
abs(A-B)<=tol
The values that you are trying to compare cannot be exactly represented using binary floating point numbers. What you see printed in the command window is the closest representation to 5 or 16 significant digits, depending on your current format setting. To see the "real" value download James Tursa's FEX submission:
Use James Tursa's num2strexact and you will see that none of those values really have the exact value 99.99. All you are looking at is a representation of those floating point numbers displayed in the command window, to the precision defined by your format setting. Just because you see 99.99 is displayed tells you nothing about the "real" floating point number's value.
Note that you can change how the value is displayed by changing the format.
You need to learn about the limits of floating point numbers. Start by reading these:
This is worth reading as well:

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by