MATLAB Answers

numbers to the left of the dot in tab of matlab

29 views (last 30 days)
Chiara
Chiara on 12 Jan 2021
Commented: Chiara on 14 Jan 2021
Hi everyone, I'm new and I'm not sure how this format works, I don't know if I asked the question correctly but I hope you can help me. I have to calculate iter error sol res in the matlab tab with decimal and integer digits, in fixed and floating point format. how can i know how many digits to the left of the dot I have to put?
example
fprintf('%3d %24.5e %12.2e\n' ,tab')

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Jan 2021
The %e format always outputs exactly one digit before the decimal place, and outputs a number of decimal places after that according to the format you give, such as 5 decimal places for the %24.5e format. So far that is 1 leading + 5 after decimal [in this case] + decimal place itself, subtotal 7 so far. It always always has a sign character for the exponent, so subtotal 8 now.
If the value is negative, then it will use one place for that. If your leading width such as the 24 in %24.5e is not wide enough to accomodate everything including the leading - then it will use an extra character .
Now we get to the question of how many digits of exponent there are. If I recall correctly, the rule is:
  • if you are on MS Windows, or abs() of the exponent is 100 or greater, then 3 digits of exponent are used
  • if you are on Mac or Linux and abs() of the exponent is 99 or less, then 2 digits of exponent are used
So take the subtotal of 8, add 3 potential digits for exponent, add 1 potential place for sign, for a total of 12 in this case (7 plus number of digits you specified after the period in the %.e format). No value will require more than that.

  3 Comments

Chiara
Chiara on 12 Jan 2021
In% 24.5e how do I know what to add from subtotal 8 to 24?
and for these other two examples which methods should I use?
1) fprintf('%3d %11.3e %11.3e\n',tab')
2) printf('%2d %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %9.1e %9.1e\n',tab')
Walter Roberson
Walter Roberson on 12 Jan 2021
In %N.5e you will get one of the following
123456789ABCD
x.xxxxxe+xx %positive number, small exponent, linux or mac
x.xxxxxe+xxx %positive number, large exponent, or any time on pc
-x.xxxxxe+xx %negative number, small exponent, linux or mac
-x.xxxxxe+xxx %negative number, large exponent, or any time on pc
That could be 11, 12, or 13 characters depending on the value range and the platform.
Now, for %N.5e if N > number of characters required according to the above, then the whole thing will be written at the right hand side of N character positions, so N minus however many blanks proceeding it -- 13, 12, or 11 leading blanks according to the above.
If N < number of characters required according to the above, then N will effectively be ignored, and all of the characters defined above will be inserted.
%Nd is an integer filling the right hand side of a mininum of N spaces. If the input is not an integer, a floating point number will be output, messing up the formatting.
%N.5f uses exactly 5 digits after the decimal point, plus the decimal point, plus a minimum of one leading digit, plus a leading negative sign if required, for a minimum of 7 characters, such as
1234567
0.31415
If the value is 10 or greater, more digits will be used to the left. The number will not be converted into exponential format -- so for example pi*10^50 would output 50 digits before the decimal place (many of them zero.) Again, the value will be placed on the right hand side of a field of blanks of minimum width N.

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!

Translated by