mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-24 20:45:06 +02:00
Better bignum support for number->string
This commit is contained in:
parent
53a9e2613f
commit
46c41be356
1 changed files with 35 additions and 31 deletions
22
runtime.c
22
runtime.c
|
@ -1691,7 +1691,7 @@ char *int_to_binary(char *b, int x)
|
||||||
object Cyc_number2string2(void *data, object cont, int argc, object n, ...)
|
object Cyc_number2string2(void *data, object cont, int argc, object n, ...)
|
||||||
{
|
{
|
||||||
object base = NULL;
|
object base = NULL;
|
||||||
int base_num = 10, val;
|
int base_num = 10, val, sz;
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
va_list ap;
|
va_list ap;
|
||||||
va_start(ap, n);
|
va_start(ap, n);
|
||||||
|
@ -1705,6 +1705,17 @@ object Cyc_number2string2(void *data, object cont, int argc, object n, ...)
|
||||||
base_num = unbox_number(base);
|
base_num = unbox_number(base);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is_object_type(n) && type_of(n) == bignum_tag) {
|
||||||
|
if (base_num > 64 || base_num < 2) {
|
||||||
|
Cyc_rt_raise2(data, "number->string - invalid radix for bignum", base);
|
||||||
|
}
|
||||||
|
mp_radix_size(&bignum_value(n), base_num, &sz);
|
||||||
|
if (sz > 1024) {
|
||||||
|
// TODO: just temporary, need to handle this better
|
||||||
|
Cyc_rt_raise2(data, "number->string - bignum is too large to convert", n);
|
||||||
|
}
|
||||||
|
mp_toradix(&bignum_value(n), buffer, base_num);
|
||||||
|
} else {
|
||||||
if (base_num == 2) {
|
if (base_num == 2) {
|
||||||
val = obj_is_int(n) ?
|
val = obj_is_int(n) ?
|
||||||
obj_obj2int(n) :
|
obj_obj2int(n) :
|
||||||
|
@ -1727,18 +1738,11 @@ object Cyc_number2string2(void *data, object cont, int argc, object n, ...)
|
||||||
snprintf(buffer, 1024, "%d", ((integer_type *) n)->value);
|
snprintf(buffer, 1024, "%d", ((integer_type *) n)->value);
|
||||||
} else if (type_of(n) == double_tag) {
|
} else if (type_of(n) == double_tag) {
|
||||||
double2buffer(buffer, 1024, ((double_type *) n)->value);
|
double2buffer(buffer, 1024, ((double_type *) n)->value);
|
||||||
} else if (type_of(n) == bignum_tag) {
|
|
||||||
int sz;
|
|
||||||
mp_radix_size(&bignum_value(n), 10, &sz);
|
|
||||||
if (sz > 1024) {
|
|
||||||
// TODO: just temporary, need to handle this better
|
|
||||||
Cyc_rt_raise(data, "number->string - bignum is too large to print");
|
|
||||||
}
|
|
||||||
mp_toradix(&bignum_value(n), buffer, 10);
|
|
||||||
} else {
|
} else {
|
||||||
Cyc_rt_raise2(data, "number->string - Unexpected object", n);
|
Cyc_rt_raise2(data, "number->string - Unexpected object", n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
make_string(str, buffer);
|
make_string(str, buffer);
|
||||||
_return_closcall1(data, cont, &str);
|
_return_closcall1(data, cont, &str);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue