Matlab Precision is only 8 digits? - matlab

Quoting from: https://www.mathworks.com/help/symbolic/increase-precision-of-numeric-calculations.html
By default, MATLAB® uses 16 digits of precision.
But why when I write 900000000+2 (8 zeros after 9) it returns 900000002 but writing 900000000+2 (9 zeros after 9)returns 9.0000e+09
isn't this an 8 digit precision?

you use the format command to control how many digits to be printed. help formatto see more details. Try format long g and rerun your command to see more digits.
By default, MATLAB® uses 16 digits of precision.
this refers to computation precision, not the printing precision. By default, MATLAB defines variables as double, which usually is accurate up to 16 digits. But you can print such double precision number in lower previsions (controlled by the format command)

Related

MATLAB's num2str is inconsistent

I want to receive the string representation of a number, 2 points after the dot.
I'm using MATLAB R2015a, and noticed that num2str function returns inconsistent results:
for 0.511 I get the required result (0.51):
num2str(0.511,2)
ans =
0.51
for 1.711 I get 1.7 instead of 1.71:
num2str(1.511,2)
ans =
1.5
Anyone knows why?
the 'precision' scalar input to num2str is the number of significant digits. if you want to have 2 figures after the decimal point use the 'formatSpec' string argument:
num2str(0.511,'%.2f')
0.51
num2str(1.511,'%.2f')
1.51
From the documentation for num2str():
s = num2str(A,precision) returns a character array that represents the numbers with the maximum number of significant digits specified by precision.
In other words, the second parameter controls the total number of significant figures, not the number of decimal places.
If you want to round to two decimal places, then use the round() function. You can try rounding the number before calling num2str():
num2str(round(1.511,2))

Machine Epsilon: MatLab vs Maple

I'm learning about machine epsilon in single and double precision and comparing values from different programs. For example, in matlab the following code:
>> format long
>> eps
gives 2.220446049250313e-16. But the following code in Maple:
> readlib(Maple_floats);
> evalhf(DBL_EPSILON);
> quit;
gives -15
.2220446049250314 10 (where -15 is exponent).
There is a slight difference in output between the two programs. Maple appears to round up from 3 to 4. What is the reason for this difference?
Note that Maple (and Matlab) are showing you a radix-10 representation of a hardware double precision floating point number.
So perhaps you should be more concerned with the underlying hardware double precision value.
> restart:
> kernelopts(version);
Maple 2015.0, X86 64 LINUX, Feb 17 2015, Build ID 1022128
> X:=Vector(1,datatype=float[8]): # double precision container
> p:=proc(x) x[1]:=DBL_EPSILON; end proc:
> evalhf(p(X)):
> lprint(X[1]);
HFloat(.222044604925031308e-15)
> printf("%Y\n", X[1]);
3CB0000000000000
That last result is "formatted in byte-order-independent IEEE hex dump format (16 characters wide)", according to the documentation.
So, what does Matlab give you when you printf its eps in the equivalent format? A quick web-search seems to reveal that it'll give 3CB0000000000000 alongside that 2.220446049250313e-16 you saw.
In other words: the hardware double precision representation is the same in both systems. They are representing it differently in base 10. Note that the
base 10 value displayed by Maple has 18 decimal places. The digits past the 15th are artefacts of a sort, stored so that in general internally stored numbers can round-trip correctly for repeated conversion both ways. Note that hardware double precision relates to something between 15 and 16 decimal places. So if you want to compare between the two systems you could (and likely should) compare the stored hardware double precision values and not the base 10 representations past the 15th place.

Maximum double value (float) possible in MATLAB (64-bit)

I'm aware that double is the default data-type in MATLAB.
When you compare two double numbers that have no floating part, MATLAB is accurate upto the 17th digit place in my testing.
a=12345678901234567 ; b=12345678901234567; isequal(a,b) --> TRUE
a=123456789012345671; b=123456789012345672; isequal(a,b) --> printed as TRUE
I have found a conservative estimate to be use numbers (non-floating) upto only 13th digit as other functions can become unreliable after it (such as ismember, or the MEX functions ismembc etc).
Is there a similar cutoff for floating values? E.g., if I use shares-outstanding for a company which can be very very large with decimal places, when do I start losing decimal accuracy?
a = 1234567.89012345678 ; b = 1234567.89012345679 ; isequal(a,b) --> printed as TRUE
a = 123456789012345.678 ; b = 123456789012345.677 ; isequal(a,b) --> printed as TRUE
isequal may not be right tool to use for comparing such numbers. I'm more concerned about up to how many places should I trust my decimal values once the integer part of a number starts growing?
It's usually not a good idea to test the equality of floating-point numbers. The behavior of binary floating-point numbers can differ drastically from what you may expect from base-10 decimals. Consider the example:
>> isequal(0.1, 0.3/3)
ans =
0
Ultimately, you have 53 bits of precision. This means that integers can be represented exactly (with no loss in accuracy) up to the number 253 (which is a little over 9 x 1015). After that, well:
>> (2^53 + 1) - 2^53
ans =
0
>> 2^53 + (1 - 2^53)
ans =
1
For non-integers, you are almost never going to be representing them exactly, even for simple-looking decimals such as 0.1 (as shown in that first example). However, it still guarantees you at least 15 significant figures of precision.
This means that if you take any number and round it to the nearest number representable as a double-precision floating point, then this new number will match your original number at least up to the first 15 digits (regardless of where these digits are with respect to the decimal point).
You might want to use variable precision arithmetics (VPA) in matlab. It computes expressions exactly up to a given digit count, which may be quite large. See here.
Check out the MATLAB function flintmax which tells you the maximum consecutive integers that can be stored in either double or single precision. From that page:
flintmax returns the largest consecutive integer in IEEE® double
precision, which is 2^53. Above this value, double-precision format
does not have integer precision, and not all integers can be
represented exactly.

how I must use digits function in matlab

i have code and use double function several time to convert sym to double.to increase precision , I want to use digits function.
I want to know it is enough that I write digits in the top of code or I must write digits in above of every double function.
digits set's the precision until it is changed again. Calling digits() without any input you get the precision to verify it's set correct.
In many cases digis has absoluetly no influence on symbolic variables because an analytical solution is found. This means there are no precision errors unless you convert to double. When convertig, digits should be set to at least 16 because this matches double precision.

how to remove last zero from number in matlab

If I set a variable in Matlab, say var1 = 2.111, after running the script, Matlab returns var1 = 2.1110. I want Matlab to return the original number, with no trailing zero. Anyone know how to do this. Thanks in advance.
By default Matlab displays results in Short fixed decimal format, with 4 digits after the decimal point.
You can change that to various other format such as:
long
Long fixed decimal format, with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.
3.141592653589793
shortE
Short scientific notation, with 4 digits after the decimal point.
Integer-valued floating-point numbers with a maximum of 9 digits do not display in scientific notation.
3.1416e+00
longE
Long scientific notation, with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.
Integer-valued floating-point numbers with a maximum of 9 digits do not display in scientific notation.
3.141592653589793e+00
shortG
The more compact of short fixed decimal or scientific notation, with 5 digits.
3.1416
longG
The more compact of long fixed decimal or scientific notation, with 15 digits for double values, and 7 digits for single values.
3.14159265358979
shortEng
Short engineering notation, with 4 digits after the decimal point, and an exponent that is a multiple of 3.
3.1416e+000
longEng
Long engineering notation, with 15 significant digits, and an exponent that is a multiple of 3.
3.14159265358979e+000
However I don't think other options are available. If you absolutely want to remove those zeros you would have to cast you result in a string and remove the trailing 0 characters and then display your result as a string and not a number.