Message Boards Message Boards


Avoid evaluation with HoldForm, ReleaseHold for InputField?

Posted 5 years ago
4 Replies
0 Total Likes

Hello, I am working on creating a CDF file for a calculation where I do high-precision evaluation of functions such as N[func,40]. I wrote a Module that takes user function and rationalize it, this way I create a function with user input accuracy. (Shown Below). When I don't use an inputfield my module works well but with inputfield I can't prevent it to be evaluated.

enter image description here

As you can see I need to avoid the evaluation so that I can evaluate the function in desired accuracy (number of digits). Any idea how I can do this?

Thank you


Panel[DynamicModule[{fun = 2.1 Sin[Pi z/2.1]}, Column[{

    Row[{Style["Function ", 12, Blue, Editable -> False], 
      InputField[HoldForm[Dynamic[fun]], FieldSize -> {55, 3}, 
       BaseStyle -> {12}]}, Spacer[1]]
    , Button[Style["Calculate", 14, Green, Editable -> False], 
     Res = upAC[fun]],
    Row[{Style["Result ", 12, Red, Editable -> False], 
      InputField[Dynamic[Res], FieldSize -> {52, 2}, 
       BaseStyle -> {12}]}, Spacer[5]]
  Initialization :> (upAC[in_] := 
       out = If[Accuracy[in] == \[Infinity], in, 
         ReleaseHold[Rationalize[in, 10^-Accuracy[in]]]];

Also asked in

4 Replies
Posted 5 years ago

As an alternative to the answer you already received on stackexchange:

  DynamicModule[{fun = HoldForm[2.1 Sin[Pi z/2.1]]},
        Style["Function ", 12, Blue, Editable -> False],
        InputField[Dynamic[fun], FieldSize -> {55, 3}, BaseStyle -> {12}]
      Button[Style["Calculate", 14, Green, Editable -> False], Res = upAC[ReleaseHold@fun]],
        Style["Result ", 12, Red, Editable -> False],
        InputField[Dynamic[Res], FieldSize -> {52, 2}, BaseStyle -> {12}]
    Initialization :>
        upAC[in_] :=
          out = If[Accuracy[in] == \[Infinity], in, Rationalize[in, 10^-Accuracy[in]]];

enter image description here

Posted 5 years ago

Thank you but it is not working the way it should. Once you ReleaseHold it will go into the function after it is evaluated.

In[5]:= N[81692243 /54607242, 40] - N[Pi/(21/10), 40]

Out[5]= -7.0657759444036854756171*10^-17

As you can see the result inside Sin can not anymore be evaluated with more than 16 Digits Accuracy.

Posted 5 years ago


Sorry, but I dont fully understand what you want to achieve.

However, if I declare Res as a Dynamic variable, outside of the DynamicModule itself, it returns the expected number of digits after pressing Calculate in the DynamicModule:

enter image description here

Posted 5 years ago

It is expected number of digits that is true but is it right? Forget about Sin. When I evaluate fg and fg2 with 40 digits fg2 has the right digits not fg . Because it is already evaluated before setting the accuracy after machine zero the digits are not right. My goal is to avoid that and evaluate fg when I need to evaluate with right digits.

In[190]:= fg = Pi/2.1;
fg2 = Pi/(21/10);
SetAccuracy[fg, 40]
SetAccuracy[fg2, 40]

During evaluation of In[190]:= 1.4959965017094252

During evaluation of In[190]:= (10 \[Pi])/21

Out[194]= 1.4959965017094252193174952481058426201344

Out[195]= 1.495996501709425351648877801561668040094
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract