Why is it when I am adding a number to array, the single number overrides array
55 views (last 30 days)
Show older comments
Charles Bradford
on 10 Sep 2024 at 14:16
Currently trying to add a single number to an array, but despite the array initially outputting well, the moment I try to add to it, the entire array is replaced by the number I am adding. This can be best seen by running "lownutant" which outputs a 1x50 array, but adding 1 changes all values to 1.
% Modified Ramberg-Osgood Tension
E=10.3*10^6; %psi
Fty=68000; %psi
Ftu=76000; %psi
fnl=linspace(0,Ftu,250);
e0=0.09;
epu=e0-(Ftu/E);
nt=log(epu/.002)/log(Ftu/Fty);
enl=(fnl./E)+(0.002.*((fnl./Fty).^nt));
% Tangent Modulus
dEtan=linspace(.025,.03,50) %Domain for plotting Etan
lownutant=((.002.*E.*nt./Fty)).*((dEtan./Fty).^(nt-1))
a=1
b= a+lownutant
2 Comments
Stephen23
on 10 Sep 2024 at 14:53
Edited: Stephen23
on 10 Sep 2024 at 15:16
"...the entire array is replaced by the number I am adding"
Because the numbers you are adding are simply ginormously different in magnitude, certainly well beyond anything that any (commonly-used) numeric type can store in one number. Here are (approximately) two numbers that you are adding together:
fprintf('%.210f\n',1,0.0015e-205)
Do you expect MATLAB to add those two numbers together numerically and then show you their sum which has digits over a range spanning more than 200 orders of magnitude?
Even if it was possible, how would that be useful as the default display behavior? I mean, as a human I easily lose track after five or six digits (without counting), are you telling me that you can read any number and you can tell at a glance that it has exactly 205 zeros before the non-zero digits? Or is it 206? Or maybe 207...? Perhaps I lost count.
Accepted Answer
Les Beckham
on 10 Sep 2024 at 14:30
lownutant is so small (~10^207) that can't be represented in floating point after adding 1 to it.
Floating point resolution for numbers near 1 is only ~2*10^-16.
eps(1)
% Modified Ramberg-Osgood Tension
E=10.3*10^6; %psi
Fty=68000; %psi
Ftu=76000; %psi
fnl=linspace(0,Ftu,250);
e0=0.09;
epu=e0-(Ftu/E);
nt=log(epu/.002)/log(Ftu/Fty);
enl=(fnl./E)+(0.002.*((fnl./Fty).^nt));
% Tangent Modulus
dEtan=linspace(.025,.03,50) %Domain for plotting Etan
lownutant=((.002.*E.*nt./Fty)).*((dEtan./Fty).^(nt-1))
a=1
b= a+lownutant
0 Comments
More Answers (2)
Milan Bansal
on 10 Sep 2024 at 14:42
Hi Charles Bradford
This behavior is due to the limitations of floating-point precision in MATLAB . The elements in "lownutant" array are of the order 10e-205 .When you have numbers that are extremely small, like those on the order of (10^{-205}), and you add a much larger number, such as 1, the result is dominated by the larger number due to the precision limits of floating-point arithmetic.
In MATLAB, the default numeric type is double precision, which follows the IEEE 754 standard for double-precision floating-point numbers. This standard provides about 15 to 17 significant decimal digits of precision. When you add 1 to a very small number like (10^{-205}), the small number is effectively lost in the arithmetic operation because it is much smaller than the precision limit.
Please refer to the following documentation link to learn more:
Hope this helps!
0 Comments
John D'Errico
on 10 Sep 2024 at 14:53
Edited: John D'Errico
on 10 Sep 2024 at 14:54
E=10.3*10^6; %psi
Fty=68000; %psi
Ftu=76000; %psi
fnl=linspace(0,Ftu,250);
e0=0.09;
epu=e0-(Ftu/E);
nt=log(epu/.002)/log(Ftu/Fty);
enl=(fnl./E)+(0.002.*((fnl./Fty).^nt));
% Tangent Modulus
dEtan=linspace(.025,.03,50); %Domain for plotting Etan
lownutant=((.002.*E.*nt./Fty)).*((dEtan./Fty).^(nt-1))
Funny, but I doubt that is true. In fact, I know your claim is false. But at the same time, it is true too. You need to understand floating point arithmetic.
Look at the vector lownutant. Do you see that factor of 10^-205 at the top? That means those numbers have roughly 205 zeros at the BEGINNING of the number, before you see a non-zero digit.
Now add 1 to that number. Does MATLAB store more than 200 decimal digits in a number? OF COURSE NOT! MATLAB uses double precision arithmetic, at least in what you are doing.
Essentially small numbers smaller than the number eps
eps
when added to 1, will give 1 back, when working in double precision. And the numbers in lownutant are many, many orders of magnitude smaller than eps.
So MATLAB did not replace the number with 1. Instead, you added a number too small to make a difference, and it got rounded to . There is a big difference. You could have used symbolic computation to do your work, but it will be very much slower.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!