float_to_round function?



A variant that uses rat:

(%i45) round_some_floats(e) := scanmap(lambda([s], block([ratprint: 
false], if floatnump(s) and close_enough(s, rat(s)) then rat(s) else s)), 
e)$
(%i47) close_enough(a,b) := 10^6 * abs(a-b) < max(abs(a), abs(b))$


(%i46) round_some_floats(bessel(1.4,x) * bessel(42.0, 12) + 
sqrt(33.0000001 * x + y) - bessel_j(1/2, 12));
(%o46) sqrt(y+33*x)+bessel(7/5,x)*bessel(42,12)-bessel_j(1/2,12)

Is this  mostly a workaround for a Maxima bug / weakness? All the rounding 
makes me nervous. 

--Barton

maxima-bounces at math.utexas.edu wrote on 11/03/2011 03:32:53 PM:


> On Nov. 3, 2011, Barton Willis wrote:
> ------------------------------
> Maybe something like:
> 
>    (%i24) round_some_floats(e) := scanmap(lambda([s], if floatnump(s) 
and 
>                   abs(s-round(s)) * 10^6 < 1 then round(s) else s), e)$
> 
>    (%i26) round_some_floats(bessel(1.4,x) * bessel(42.0, 12) + 
>                sqrt(33.000001 * x + y) - bessel_j(1/2, 12));
> 
>    (%o26) sqrt(y+33*x)+bessel(1.4,x)*bessel(42,12)-bessel_j(1/2,12)
> ----------------------------------------------------
> Yes, that seems to work better than using rat, but not sure which 
>         would be better in practice:
> 
>  (%i83) round_some_floats(e) := scanmap(lambda([s], if floatnump(s)
>         then rat(s) else s), e)$
> 
> (%i84) round_some_floats(
>          bessel(1.4,x) * bessel(42.0, 12) + 
>                 sqrt(33.000001 * x + y) - bessel_j(1/2, 12));
> 
> 
> (%o84) sqrt(1000000*y+33000001*x)/1000+bessel(7/5,x)*bessel(42,12)
>                                       -bessel_j(1/2,12)
> 
> ----------------------------
> Ted
> 
> 
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima