0

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?

Maddy
  • 2,520
  • 14
  • 44
  • 64
  • 1
    Off the top of my head, the mantissa used by `double` has 52 bits. This gives about `log10(2^52)` or `15` decimal digits of precision. See also the `eps` function – Luis Mendo Dec 12 '14 at 23:19
  • 1
    `eps(123456789012345.678)` will tell you exactly the precision you have available at that magnitude. – Peter Dec 12 '14 at 23:58
  • Related and possibly duplicate: [biggest integer that can be stored in a double](http://stackoverflow.com/questions/1848700/biggest-integer-that-can-be-stored-in-a-double). I reccomend looking at the [`flintmax`](http://www.mathworks.com/help/matlab/ref/flintmax.html) function and The MathWorks' [page on floating-point](http://www.mathworks.com/help/matlab/matlab_prog/floating-point-numbers.html) too. – horchler Dec 13 '14 at 02:11

3 Answers3

2

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).

KQS
  • 1,547
  • 10
  • 21
0

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.

Thomas
  • 725
  • 4
  • 14
0

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.

Edric
  • 23,676
  • 2
  • 38
  • 40