Re: Executing function on Mathematica 8
- To: mathgroup at smc.vnet.net
- Subject: [mg124682] Re: Executing function on Mathematica 8
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Tue, 31 Jan 2012 05:37:33 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
On 1/30/12 at 5:07 AM, fadiabuamara at gmail.com (Fadi Mousa) wrote:
>I am new to mathematica and do not know how to enter the following
>function and then call it on mathematica 8:
>BinExp[num_,d_] := Module[{n,L},
>If[d > $MaxPrecision, $MaxPrecision = d];
>n = N[num,d];
>L = First[RealDigits[n,2]]
>];
You enter this function by simply typing what you posted into a
cell then doing shift-enter to evaluate that cell.
You would call this function by typing
BinExp[number, number]
in a cell then doing the same shift-enter to evaluate it.
Your function would be better coded as:
BinExp[num_,d_] := Module[{n},
n = N[num,d];
First[RealDigits[n,2]]
]
The If statement will evaluate as False (and consequently does
nothing) by default since $MaxPrecision is by default infinity.
There is no need for the local variable L since it is not used.
You don't need a ; whenever using SetDelayed (":=") since
SetDelayed returns nothing at the time the function is defined.
A value is only returned when the function is evaluated later.
Even more concise coding of this function would be done as:
BinExp[num_,d_] := First[RealDigits[N[num,d],2]]