Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #11100
| From | Bill Rowe <readnews@sbcglobal.net> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Putting a Test or Condition on the Right-Hand Side of a Function |
| Date | 2012-07-27 09:01 +0000 |
| Message-ID | <jutle7$cj5$1@smc.vnet.net> (permalink) |
| Organization | Time-Warner Telecom |
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 comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
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
csiph-web