MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Replace & Rationalize


On 18 Sep 2011, at 10:10, Chris Degnen wrote:

> Further to a question on Stack Overflow I noticed a puzzling
> evaluation.  Why does the second expression generate reals?
>
> (x \[Function]
>   If[IntegerQ[Rationalize[x]], Rationalize[x], x]) /@ {0., 1.}
>
> {0., 1.} /. x_ /; IntegerQ[Rationalize[x]] -> Rationalize[x]
>
>
>

Because you need RuleDelayed instead of Rule on the RHS, otherwise Rationalize[x] evaluates simply to x. Thus:

In[26]:= {0.,1.}/.x_/;IntegerQ[Rationalize[x]]:>Rationalize[x]
Out[26]= {0,1}






  • Prev by Date: Re: comments
  • Next by Date: Re: (x - 1) or (-1 + x)
  • Previous by thread: Replace & Rationalize
  • Next by thread: Re: Replace & Rationalize