mirror of
https://gitlab.com/embeddable-common-lisp/ecl.git
synced 2026-01-28 07:22:27 -08:00
More accurate ratio->float conversion which does not fail if the denominator is too large to fit a float
This commit is contained in:
parent
6a4308353b
commit
53c751a89f
2 changed files with 21 additions and 4 deletions
|
|
@ -214,10 +214,24 @@ number_to_double(cl_object x)
|
|||
case t_bignum:
|
||||
return(big_to_double(x));
|
||||
|
||||
case t_ratio:
|
||||
return(number_to_double(x->ratio.num) /
|
||||
number_to_double(x->ratio.den));
|
||||
|
||||
case t_ratio: {
|
||||
double output;
|
||||
mpq_t aux;
|
||||
mpq_init(aux);
|
||||
if (FIXNUMP(x->ratio.num)) {
|
||||
mpz_set_si(mpq_numref(aux), fix(x->ratio.num));
|
||||
} else {
|
||||
mpz_set(mpq_numref(aux), x->ratio.num->big.big_num);
|
||||
}
|
||||
if (FIXNUMP(x->ratio.den)) {
|
||||
mpz_set_si(mpq_denref(aux), fix(x->ratio.den));
|
||||
} else {
|
||||
mpz_set(mpq_denref(aux), x->ratio.den->big.big_num);
|
||||
}
|
||||
output = mpq_get_d(aux);
|
||||
mpq_clear(aux);
|
||||
return output;
|
||||
}
|
||||
case t_shortfloat:
|
||||
return((double)(sf(x)));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue