Good point. Also, in Rupert's chop function a value of -%i*3000 would be
chopped when compared to eps=1e-8.
A better solution could be:
chop (x, eps) := if is (abs(imagpart(x)/realpart(x)) < eps) then
realpart (x) else x$
Jaime
On 02/26/2013 03:15 PM, Richard Fateman wrote:
> In general these do not avoid a small imaginary "error", but set
> the imaginary part to zero if it is less than a certain absolute value.
>
> You might consider that 10.0d-10 + %i* 9.0d0-5 should probably NOT
> be chopped to 10.0d-10, even though the imaginary part is less than
> eps=10.0d-5.
>
> You might prefer to use "relative error" for example.
>
> RJF
>
>
> On 2/26/2013 3:26 AM, Rupert Swarbrick wrote:
>> Pepe Sanchez<jose.sanchez at uv.es> writes:
>>>> I want to know general methods to avoid a small imaginary error.
>>> I guess that you need something similar to the "chop" operator in
>>> Mathematica.
>>>
>> The same idea as Pepe suggested, but maybe nicer than operating in place
>> on s:
>>
>> (%i1) fpprintprec: 5$
>>
>> (%i2) s: float(rectform(solve(x^3-3*x^2-2*x+1,x)))$
>>
>> (%i3) chop (x, eps) := if is (imagpart (x) < eps) then realpart (x) else x$
>>
>> (%i4) chopeq (eq, eps) := chop (lhs (eq), eps) = chop (rhs (eq), eps)$
>>
>> (%i5) map (lambda ([eq], chopeq (eq, 1e-8)), s);
>> (%o5) [x = .34338, x = - .83424, x = 3.4909]
>>