1
$\begingroup$

For a project of mine it is preferrable to set notebook precision globally. I have done so by using the answer in Global precision setting.

The idea is to dynamically create a Range from variables created with this $PreRead, but a peculiarity occurs. The below code illustrates this: a rather trivial function with input variables is SetDelayed. However, on a fresh kernel asdf does not evaluate. The global variant outside Module however works fine. Is there any way to create the below functionality using this $PreRead? Or is there an alternative to the $PreRead method that can be used interchangeably?

ClearAll["Global`*"];

myGlobalPrecision = 2 $MachinePrecision;
$PreRead = (# /. 
     s_String /; 
       StringMatchQ[s, NumberString] && 
        Precision@ToExpression@s == MachinePrecision :> 
      s <> "`" <> ToString@Floor@myGlobalPrecision <> "." &);

asdf[PTsNo_, {xLBound_Real, xUBound_Real}] := Module[{xIncrements},
  xIncrements = 
   Range[xLBound, xUBound, (xUBound - xLBound)/(PTsNo - 1)]
  ]

ListPlotResolution = 20;
SpaceLowerBound = 0;
SpaceUpperBound = 10.;
xLBoundGlobal = SpaceLowerBound;
xUBoundGlobal = SpaceUpperBound;
PTsNoGlobal = 20;

asdf[ListPlotResolution, {SpaceLowerBound/1., SpaceUpperBound/1.}]

xIncrements = 
 Range[xLBoundGlobal, 
  xUBoundGlobal, (xUBoundGlobal - xLBoundGlobal)/(PTsNoGlobal - 1)]
$\endgroup$
  • $\begingroup$ Note that zero in arbitrary precision arithmetic is the Integer zero when computed thus: 0/1.`20. Hence the second argument to asdf is not a list of Real. $\endgroup$ – Michael E2 Nov 26 '19 at 14:03
  • $\begingroup$ @MichaelE2 Interesting. That indeed does improve the situation... $\endgroup$ – 1010011010 Nov 26 '19 at 14:05
  • $\begingroup$ @MichaelE2 How does one enforce Realness of 0/1.``20? $\endgroup$ – 1010011010 Nov 26 '19 at 14:06
  • $\begingroup$ It's a good question. 0``20 represents an underlfow less than $10^{-20}$. It computes as zero plus/minus an uncertainty in arbitrary precision arithmetic. The only true (?) zero in arbitrary precision is the integer 0. So as far as argument patterns go, you could use xLBound: 0 | _Real. There is also xLBound_?Internal`RealValuedNumberQ but it will allow Integer and Rational arguments as well. $\endgroup$ – Michael E2 Nov 26 '19 at 14:13
  • 1
    $\begingroup$ @MichaelE2 It seems as if removing the /1. and using your Internal`RealValuedNumberQ trick solved the problem. $\endgroup$ – 1010011010 Nov 26 '19 at 15:10

Your Answer

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

Browse other questions tagged or ask your own question.