Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #11100 > unrolled thread
| Started by | Bill Rowe <readnews@sbcglobal.net> |
|---|---|
| First post | 2012-07-27 09:01 +0000 |
| Last post | 2012-07-27 09:01 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.soft-sys.math.mathematica
Re: Putting a Test or Condition on the Right-Hand Side of a Function Bill Rowe <readnews@sbcglobal.net> - 2012-07-27 09:01 +0000
| From | Bill Rowe <readnews@sbcglobal.net> |
|---|---|
| Date | 2012-07-27 09:01 +0000 |
| Subject | Re: Putting a Test or Condition on the Right-Hand Side of a Function |
| Message-ID | <jutle7$cj5$1@smc.vnet.net> |
On 7/26/12 at 3:35 AM, gregory.lypny@videotron.ca (Gregory Lypny) wrote: >I have a simple function that displays the integer part of a real >number if the fraction part is less than a millionth. >dropDecimal[x_] := If[FractionalPart[x] < .000001, IntegerPart[x], >x] >In trying to learn more about functional programming, I was wondering >whether there is a way to put the condition or test on the right-hand >side of the function. You could do this as follows: In[6]:= dropDecimal[x_] := IntegerPart[x] /; FractionalPart[x] < .000001 dropDecimal[x_] := x /; FractionalPart[x] > .000001 In[8]:= dropDecimal[N[56 + 10^(-6)]] Out[8]= 56 In[9]:= dropDecimal[N[56 + 10^(-4)]] Out[9]= 56.0001 But I think this would be a much better definition for this function: In[10]:= Clear[dropDecimal]; dropDecimal[x_] := Round[x, .000001] In[12]:= dropDecimal[N[56 + 10^(-6)]] Out[12]= 56. In[13]:= dropDecimal[N[56 + 10^(-4)]] Out[13]= 56.0001 However, this isn't quite the same since the output isn't an exact value and the use of Round means something just slightly less than the threshold gets rounded up rather than truncated.
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web