James Tursa,

Thank you! I understand now. This coverter also helped me confirm the info you provided (that fprint is printing the entire ieee 754 double.

ultimatesolver.com/en/ieee-754

Regards

Joe

16 views (last 30 days)

Show older comments

As shown in this code, matlab will print incorrect digits to more than 16 places of precision. Where do the numbers for the "extra" digits come from?

clear;clc

a=1/6;

fprintf('\n58digits\t%60.58f\n',a)

%result

%58digits 0.1666666666666666574148081281236954964697360992431640625000

%%%question

%by default matlab is double precision for all numbers

%what is the source of the "extra" digits 574148081281236954964697360992431640625

%after the 0625, all digits will be zero

James Tursa
on 11 Mar 2024

Edited: James Tursa
on 11 Mar 2024

The "extra" digits that look incorrect to you are simply the result of doing an exact conversion of the actual floating point binary bit pattern that is stored to decimal. They are meaningful in that sense. But the values are only significant to about 16 decimal digits. E.g.,

a=1/6;

fprintf('\n58digits\t%60.58f\n',a-eps(a))

fprintf('\n58digits\t%60.58f\n',a)

fprintf('\n58digits\t%60.58f\n',a+eps(a))

eps(a)

The three numbers printed are all exact conversions, so the digits mean something in that sense. Those long decimal digits are the equivalent of what is actually stored internally for the number in binary. 1/6 cannot be represented exactly in IEEE double precision, so the closest number to it is picked for the internal representation and what is displayed with fprintf() is an exact conversion of that number. The two closest numbers to a that can be represented in IEEE double precision format are about 2.7756e-17 away from it. I.e., there is only about 16 decimal digits of precision in that number.

Some numbers can be represented exactly, e.g.,

fprintf('\n58digits\t%60.58f\n',1/8)

But others cannot, such as 1/6.

CAVEAT: Windows MATLAB R2017a and earlier used a different background library for fprintf( ) and related functions. In those versions, you would not get these extra exact conversion digits printed. It is only on later Windows MATLAB versions that use an updated library where you get these digits printed. To my knowledge, the non-Windows MATLAB versions always used libraries that printed these digits. For these earlier Windows MATLAB versions, you would need to use a different function to get those digits, e.g.,

Florian Bidaud
on 11 Mar 2024

Edited: Florian Bidaud
on 11 Mar 2024

By default, MATLAB uses 16 digits of precision. The rest can come from intermediate format conversion errors inside fprintf, due to the difference between the asked precision and the format of the number.

This is described here:

for example, intermediate operations and conversions within the single function:

format long

x = 0.2

y = single(0.2)

z = x - y

If you need more than 16 digits of precision, you can use:

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

Start Hunting!