Say you wanted a decimal answer. How do you get one? I know evalf() can be used but it counts ALL digits, not just after the decimal. For example evalf(19/4, 2) gives 4.8 but I'm looking for a function that would give 4.75
2 Answers
Your example of 19/4 doesn't make the question completely clear, I think. In the case of three figures, would you want 19/400 to become 0.0475 showing 3 figures after the right-most zero, or 0.048 as exactly 3 figures to the right of the decimal point (and rounded)? I suspect that you want the latter, but give you a few alternatives anyway.
Note that there is a difference between actually adjusting the quantities and merely changing the precision with which they are displayed.
Suppose that you wanted exactly d places retained for the positions representing base 10 to negative powers.
restart:
Q:=(x,d)->sscanf(sprintf(cat("%",sprintf(".%df",d)),x),"%a")[]:
Q(19/4, 2);
4.75
Q(-19/4, 2);
-4.75
Q(19/400, 3);
0.048
Q(-19/400, 5);
-0.04750
Q(123456789012345.6789, 2);
15
0.12345678901234568 10
sprintf("%a", %);
"123456789012345.68"
Suppose now that instead you wanted exactly d figures retained after any leading zeroes, for the positions representing base 10 to negative powers. Notice the difference between what's above and what's below, for the 4th and 5th examples.
restart:
T := proc(x,d::posint)
Digits:=max(Digits,d,length(x));
trunc(x) + evalf[d](frac(x));
end proc:
T(19/4, 2);
4.75
T(-19/4, 2);
-4.75
T(19/400, 3);
0.0475
T(19/400, 5);
0.047500
T(123456789012345.6789, 2);
15
0.12345678901234568 10
sprintf("%a", %);
"123456789012345.68"
All the above is about manipulation of the quantities. If instead all you care about is the display in Maple's User Interface, then that can be changed as an option. By line-printing with the lprint command below we can see that the actual results are stored with usual precision, but they are merely displayed with only two figures shown after the decimal point.
restart:
interface(displayprecision=2):
evalf( 19/4 );
4.75
lprint(%);
4.750000000
evalf( 19/400 );
0.05
lprint(%);
.4750000000e-1
- 5,293
-
Yes more towards your second point, I want max percision but sometimes want to see a decimal answer (even though I know it would be an approximation) so lprint sounds like a good option. – Celeritas Oct 09 '14 at 23:35
-
The point to using
displayprecisionis that it only affects what is displayed. The values still have their full precision, for any subsequent computation. I only usedlprintto demonstrate that fact -- the results ofevalfcould simply be assigned to names and then used. The special rounding only affects the printing. The earlier approaches usingTandQactually produce specially rounded values (which could also be assigned and used). Your choice. – acer Oct 10 '14 at 14:40
It also depends on what is easy for you. In some ways it is best to maintain high precision in calculations and just adjust the display. There is a slight learning curve but then you know it.
Something quick and dirty on the spot, if you want 3 places after the decimal point, is floor(x)+evalf(frac(x),3). That seemed to work in a test. It would need adjustment for negative numbers.
- 1,593
Digits:=2and then the expressionevalf(19/4, 2)nope that didn't work, that just assigns 2 to the variable digits. – Celeritas Oct 02 '14 at 08:57