Question: How can we force evalf[n] to apple to all numerics?

Here is an example where evalf[n] doesn't operate on the argument of the undefined function f.

x := rand(0. .. 1.)()
                          0.2342493224
y := x+f(x):
evalf[4](y)
                    0.2342 + f(0.2342493224)

# but, as soon as f is a known function:
evalf[4](cos(x))
                             0.9727


Here is a way to force evalf[4](f(x)) to return f(0.2342)?

I found only two ways to do this:
First: declare interface(displayprecision=4) 

interface(displayprecision=4):
y;
                 0.2342 + f(0.2342)

Or: do this (which is relatively cumbersome)

Evalf := proc(expr, n)
  local i := [indets(evalf[n](expr), numeric)[]]:
  eval(expr, i =~ evalf[4](i))
end proc:

Evalf(y, 4)
                 0.2342 + f(0.2342)

Thanks in advance.

PS:  I do not like setting displayprecision to some value because its effect is remnant: if you execute again the same worksheet (begining with a restart), the value of displayprecision is not reset to 10 but keeps the value you gave it previously, somewhere in the worksheet.

Please Wait...