Double precision: Why does fprintf print "extra" wrong non zero digits?
Ältere Kommentare anzeigen
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
1 Kommentar
Joseph Ragan
am 11 Mär. 2024
Akzeptierte Antwort
Weitere Antworten (1)
Florian Bidaud
am 11 Mär. 2024
Bearbeitet: Florian Bidaud
am 11 Mär. 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:
1 Kommentar
Joseph Ragan
am 11 Mär. 2024
Verschoben: Dyuman Joshi
am 11 Mär. 2024
Kategorien
Mehr zu Data Type Identification finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!