Problem adding the large number with small number

Hello everyone, I have a problem with adding numbers.
I have two numbers;
a=6.3276e+16 and b=17.3304, when adding these numbers, the result is a, that is, no addition is made.

 Accepted Answer

The addition is made. It is necessary to display the appropriate precision to see it —
a=6.3276e+16
a = 6.3276e+16
b=17.3304
b = 17.3304
a_plus_b = a + b
a_plus_b = 6.3276e+16
fprintf('%33.25E',a)
6.3276000000000000000000000E+16
fprintf('%33.25E',a_plus_b)
6.3276000000000016000000000E+16
fprintf('%33.25E',a_plus_b - a)
1.6000000000000000000000000E+01
Floating-point numbers aren’t as accurate as necessary in that respect, however thye do their best!
.

5 Comments

thanks to answer. well, if this addition is done, how can I use the result in the program? that is, is there any change I need to make in the current settings in matlab?
No changes are necessary. MATLAB maintains full internal precision regardless of the display precision. The format setting or the fprintf or sprintf format descriptor string simply determine how the values are displayed.
It could be worthwhile to use the Symbolic Math Toolbox if a large number of values with such different magnitudes are used routinely, because it has (if I remember correctly) 50-digit internal precision (that may be expanded). The result would then be returned to the MATLAB numeric workspace using the double function, with double-precision (not Symbolic Math Toolbox precision) representation, however the interim calculations done in symbolic variables would result in all of them being represented in the final double values, even though the full extended precision would be lost with the conversion to double.
.
There will come a point when the one number is so big that when the smaller number is converted to the same exponent (the larger one, which is required so that they can be added together), there won't be enough digits in the computer to hold the smaller number and the result is the smaller number will be zero and when adding it you'll end up with exactly the bigger number no matter how many decimal places you print out:
a=6.3276e+36
a = 6.3276e+36
b=17.3304
b = 17.3304
a_plus_b = a + b
a_plus_b = 6.3276e+36
fprintf('a = %.75E\n',a)
a = 6.327600000000000206026557892920868864000000000000000000000000000000000000000E+36
fprintf('a_plus_b = %.75E\n',a_plus_b)
a_plus_b = 6.327600000000000206026557892920868864000000000000000000000000000000000000000E+36
fprintf('a_plus_b - a = %.75E\n',a_plus_b - a)
a_plus_b - a = 0.000000000000000000000000000000000000000000000000000000000000000000000000000E+00
This is called "truncation error" (if you'd have taken a linear algebra or numerical analysis course, you should have learned about that.)
As always, my pleasure!
Espanding on this with the Symbolic Math Toolbox —
a = vpa(sym(6.3276e+16))
a = 
63276000000000000.0
b = vpa(sym(17.3304))
b = 
17.3304
a_plus_b = vpa(a + b, 30)
a_plus_b = 
63276000000000017.3304
So to retain full precision in interim calculations, use symbolic variables.
format long
a_plus_b = double(a_plus_b)
a_plus_b =
6.327600000000002e+16
fprintf('%33.25E', a_plus_b)
6.3276000000000016000000000E+16
The double conversion reverts to double-precision representation. Note that the representation is not the exact value, as computed using symbolic variables.
.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!