In the following, why does N
not produce the expected decimal output, while Round
does?
Cases[{ArcSec[-(3/Sqrt[5])]}, ArcSec[a_] -> {a, 2 a, N[a], Round[a,.01]}]
{{-(3/Sqrt[5]), -(6/Sqrt[5]), -(3/Sqrt[5]), -1.34}}
Using RuleDelayed (:>)
instead of Rule (->)
gives the desired result:
Cases[{ArcSec[-(3/Sqrt[5])]}, ArcSec[a_] :> {a, 2 a, N[a], Round[a, .01]}]
{{-(3/Sqrt[5]), -(6/Sqrt[5]), -1.3416407864998738, -1.34}}
From RuleDelayed >> Properties and Relations:
- -> evaluates when it is first entered; :> when it is used
With Rule
the right-hand-side N[a]
is evaluated to a
before replacement occurs:
ArcSec[a_] -> N[a]
ArcSec[a_] -> a
ArcSec[a_] :> N[a]
ArcSec[a_] :> N[a]
Compare the Trace
s of the two cases:
Cases[{ArcSec[-(3/Sqrt[5])]}, ArcSec[a_] -> N[a]] // Trace // Column
Cases[{ArcSec[-(3/Sqrt[5])]}, ArcSec[a_] :> N[a]] // Trace // Column
For Round
, both Rule
and RuleDelayed
give the same result, because Round[foo, .1]
does not evaluate for non-numeric input foo
(it "evaluates" to Round[foo, .1]
) whereas N[foo]
evaluates to foo
.
RuleDelayed (:>)
instead ofRule (->)
? $\endgroup$Rule
N[a]
is evaluated toa
before the value ofa
gets injected: inspectCases[{ArcSec[-(3/Sqrt[5])]}, ArcSec[a_] -> N[a]] // Trace // Column
$\endgroup$