Change the exponent precision

16 ビュー (過去 30 日間)
student187
student187 2015 年 10 月 2 日
編集済み: Star Strider 2021 年 5 月 4 日
I need to display, using fprintf, an exponent with this precision 1.7800e-005. I can set the precision for the 1.7800 part but do not know how to set the precision for the 005 part. I don't want the default 05, I need 005. Thanks

回答 (1 件)

Star Strider
Star Strider 2015 年 10 月 2 日
編集済み: Star Strider 2021 年 5 月 4 日
I didn’t realise MATLAB no longer printed out three-digit exponents by default. This may seem a long way round, but ‘expstr’ works for both +ve and -ve exponents:
Q1 = pi/10000;
expstr = @(x) [x*10.^floor(1-log10(abs(x))) floor(log10(abs(x)))];
Result = sprintf('%.4fe%+04d', expstr(Q1))
Result =
3.1416e-004
ans = 3.1416e-04
EDIT — Added abs(x) to log10 arguments in the ‘expstr’ function.
—————
EDIT — (4 May 2021 at 16:38)
Correcting my ‘expstr’ function to not crash on a 0 argument turned out to be surprisingly straightforward —
expstr = @(x) [x(:).*10.^ceil(-log10(abs(x(:)+(x==0)))) floor(log10(abs(x(:)+(x==0))))]; % Updated: 2021 05 04
Given:
Q1 = [-pi*1000; 0; pi/1000];
Result = sprintf('%8.4fe%+04d\n', expstr(Q1).')
Result =
' -3.1416e+003 0.0000e+000 3.1416e-003 '
.
  3 件のコメント
Star Strider
Star Strider 2015 年 10 月 3 日
When I first began using MATLAB 20+ years ago (always on Windows), a three-digit exponent was standard. I don’t remember when it changed (I have other things to keep track of), but in R2015b, a two-digit exponent is standard. Thus the reason for the specific three-digit exponent format here.
Thomas
Thomas 2021 年 5 月 4 日
編集済み: Thomas 2021 年 5 月 4 日
Yes, 2015 is log ago...
I have the same problem as mentioned above. Unfortunately, the suggested solution does not work if Q1 = 0.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by