4
$\begingroup$

Backtick notation uses

1.0``20

to give 1 with 20 places of accuracy after the decimal point. But what I want is

acc=20
1.0``acc

so that I can easily change the accuracy of all my calculations. Is this possible?

$\endgroup$
10
  • $\begingroup$ How about using a simple factor, as in acc=1``20;12*acc? $\endgroup$
    – Jens
    Aug 3, 2015 at 18:16
  • 1
    $\begingroup$ SetAccuracy? $\endgroup$
    – Michael E2
    Aug 3, 2015 at 18:23
  • 2
    $\begingroup$ @Jens I've wanted the same thing at times, but I don't know of any easy way to do it, since the back ticks are not operators. It would be like setting fraction = 23456 and wanting 1.fraction to translate to 1.23456. $\endgroup$
    – Michael E2
    Aug 3, 2015 at 18:53
  • 1
    $\begingroup$ You could write an awful function like ack[acc_][x_]:=ToExpression[ToString@x<>"``"<>ToString@acc] and do 1.00//ack[20] $\endgroup$
    – N.J.Evans
    Aug 3, 2015 at 21:26
  • 1
    $\begingroup$ @N.J.Evans Compare 0.200000000000001 // ack[20] and 0.200000000000001``20. -- As I said, I have yet to figure out how to do this without the actual input string to the FE (i.e., via $PreRead). $\endgroup$
    – Michael E2
    Aug 4, 2015 at 0:51

2 Answers 2

1
$\begingroup$

[Personally, I would be satisfied with SetAccuracy, at least in the use-cases in which I imagine I would need it. It just seems easier to me to learn how to work with the system instead of around it. Nonetheless, it seems to be possible....]

Here's an idea of what I was talking about with $PreRead in a comment. On a syntax error, it might fail spectacularly, but it seems to work on correct input.

$PreRead = # //. 
   RowBox[{x___, s_String /; StringMatchQ[s, __ ~~ "``"], a_, y___}] :>
    RowBox[{x, s <> ToString@ToExpression@a, y}] &

It evaluates the box form representing the accuracy and appends its value after the `` in the input.

Then

acc = 10;
1.23``(2 acc)
(*  1.2300000000000000000  *)

Accuracy[%]
(*  20.  *)

And

10.23``acc + 4.56``(2 acc)
(*  14.790000000  *)

Accuracy[%]
(*  10.  *)

The standard accuracy input syntax works:

1.23``20
(*  1.2300000000000000000  *)
$\endgroup$
0
$\begingroup$

Perhaps you can use the Notation package to help.

<< Notation`

enter image description here

Notation[a_ \[ScriptA] b_\[DoubleLongLeftRightArrow] N[Rationalize[a_,0],{Infinity,b_}]]

I used a picture of a cell here to make it clear that this has been entered via the Notation palette

enter image description here

Now you can use

list=Table[10.75\[ScriptA] k,{k,1,10}]
(* {11.,10.8,10.75,10.750,10.7500,10.75000,10.750000,10.7500000,10.75000000,10.750000000} *)

or

k=10;

Accuracy /@ 
 NestList[(1.25 \[ScriptA] k) (# - 0.2 \[ScriptA] k) &, 
  1 \[ScriptA] k, 50]
(* {10., 9.48149, 9.20936, 9.01016, 8.84585, 8.70167, 8.57032, \
8.44771, 8.33128, 8.21936, 8.1108, 8.00479, 7.90074, 7.7982, 7.69684, \
7.5964, 7.49669, 7.39755, 7.29886, 7.20054, 7.1025, 7.00469, 6.90706, \
6.80957, 6.7122, 6.61493, 6.51772, 6.42058, 6.32348, 6.22642, \
6.12939, 6.03239, 5.9354, 5.83843, 5.74147, 5.64452, 5.54758, \
5.45064, 5.35371, 5.25679, 5.15986, 5.06294, 4.96602, 4.86911, \
4.77219, 4.67528, 4.57837, 4.48145, 4.38454, 4.28763, 4.19072} *)

vs

Accuracy /@ NestList[(1.25``10) (# - 0.2``10) &, 1.0``10, 50]
(* {10., 9.48149, 9.20936, 9.01016, 8.84585, 8.70167, 8.57032, \
8.44771, 8.33128, 8.21936, 8.1108, 8.00479, 7.90074, 7.7982, 7.69684, \
7.5964, 7.49669, 7.39755, 7.29886, 7.20054, 7.1025, 7.00469, 6.90706, \
6.80957, 6.7122, 6.61493, 6.51772, 6.42058, 6.32348, 6.22642, \
6.12939, 6.03239, 5.9354, 5.83843, 5.74147, 5.64452, 5.54758, \
5.45064, 5.35371, 5.25679, 5.15986, 5.06294, 4.96602, 4.86911, \
4.77219, 4.67528, 4.57837, 4.48145, 4.38454, 4.28763, 4.19072} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.