Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

clip

2 views
Skip to first unread message

oliver

unread,
Jan 14, 2011, 6:19:14 AM1/14/11
to
Just add a few more 0 to the argument and the accuracy will increase.
If you still suspicious, build your own Clip function.
Clip1[x_]:=Sign[x]*Min[Abs[x],1]
will do and refers to basic functions and works better.
Strangely enough, Clip1 has no problem with your example, but
it has also hick-ups with numbers smaller than 1, if not more 0 are
given. Try this out:

Clip[1.00000000000000036]
Clip[1.000000000000000360000]
Clip1[x_]:=Sign[x]*Min[Abs[x],1]
Clip1[1.00000000000000036]
Clip1[0.9999999999999966]
Clip1[0.9999999999999966000]

But there are some fundamental problems with accuracy. In the second
last example, it should
render the result at least in the same accuracy. All involved function
Sign, Min and Abs are
unproblematic with respect to accuracy.


Alexey

unread,
Jan 19, 2011, 5:30:15 AM1/19/11
to
Hello,
There is a better solution:

clip[x_] := Min[Max[x, -1], 1]
clip[x_, {min_, max_}] := Min[Max[x, min], max]
SetAttributes[clip, {Listable, NumericFunction}]

You can compare it with the standard Clip:

list = {"1.00000000000000036", "-1.00000000000000036",
"1.000000000000000360000", "0.9999999999999966",
"0.9999999999999966000"};
TableForm[
InputForm /@ {#, v = ToExpression[#], Clip[v], clip[v]} & /@ list,
TableHeadings -> {None, {"Input", "%//InputForm", "Clip//InputForm",
"clip//InputForm"}}]

But it is 10 times slower:

In[4]:= l = Table[10^5*x, {x, -1., 1., 10^-6}];
l1 = clip[l]; // Timing
l1 = clip[#] & /@ l; // Timing
l1 = Thread[clip[l]]; // Timing
l2 = Clip[l]; // Timing
l1 == l2
l1 === l2

Out[5]= {9.954, Null}

Out[6]= {11.337, Null}

Out[7]= {10.054, Null}

Out[8]= {0.721, Null}

Out[9]= True

Out[10]= True

Is there a way to make it faster?

0 new messages