这是我当前的代码,但它很丑陋,我担心非常大或小的数字可能会出现边缘情况。有一个更好的方法吗?
real_to_int(n)={
if(n==floor(n),return(floor(n))); \\ If "n" is a whole number we're done
my(v=Vec(strprintf("%g",n))); \\ Convert "n" to a zero-padded character vector
my(d=sum(i=1,#v,i*(v[i]=="."))); \\ Find the decimal point
my(t=eval(concat(v[^d]))); \\ Delete the decimal point and reconvert to a number
my(z=valuation(t,10)); \\ Count trailing zeroes
t/=10^z; \\ Get rid of trailing zeroes
return(t)
}