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.
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?