Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.
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.

share|improve this question

1 Answer 1

up vote 15 down vote accepted

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]]
share|improve this answer
    
In fact there is an example given in the documentation of N which is similar to this one :) –  Leo Fang Jul 30 '13 at 22:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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