13
$\begingroup$
1 + f[1] // N 

gives

 1. + f[1.]  

I don't want the argument of f evaluated by N; I want to get

1. + f[1]

instead.

In general, there is a large algebraic expression containing some subexpressions of type f[__]. N should not be applied inside f. N should be applied only outside.

Mapping N at a certain level only is not OK, since the tree structure of expression is not know in advance.

One possible trick is

1 + f[1] /. x_f :> 1. x + 0. 

It would be good to know other options.

$\endgroup$
19
$\begingroup$

The best option then is to bestow the attribute NHold on the head f.

In[2]:= SetAttributes[f, NHoldAll]

In[3]:= 2 + f[Sqrt[2] + 1] // N

Out[3]= 2. + f[1 + Sqrt[2]]
$\endgroup$
  • $\begingroup$ In fact there is an example given in the documentation of N which is similar to this one :) $\endgroup$ – Leo Fang Jul 30 '13 at 22:29

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.