add some number-theoretic division functions

This commit is contained in:
Yuichi Nishiwaki 2013-10-22 18:29:55 +09:00
parent 828606aed5
commit 76cba3fad2
1 changed files with 46 additions and 0 deletions

View File

@ -78,6 +78,46 @@ pic_number_abs(pic_state *pic)
return pic_float_value(fabs(f)); return pic_float_value(fabs(f));
} }
static pic_value
pic_number_floor_quotient(pic_state *pic)
{
double f,g;
pic_get_args(pic, "ff", &f, &g);
return pic_float_value(floor(f/g));
}
static pic_value
pic_number_floor_remainder(pic_state *pic)
{
double f,g,q;
pic_get_args(pic, "ff", &f, &g);
q = floor(f/g);
return pic_float_value(f - g * q);
}
static pic_value
pic_number_truncate_quotient(pic_state *pic)
{
double f,g;
pic_get_args(pic, "ff", &f, &g);
return pic_float_value(trunc(f/g));
}
static pic_value
pic_number_truncate_remainder(pic_state *pic)
{
double f,g,q;
pic_get_args(pic, "ff", &f, &g);
q = trunc(f/g);
return pic_float_value(f - g * q);
}
static pic_value static pic_value
pic_number_floor(pic_state *pic) pic_number_floor(pic_state *pic)
{ {
@ -257,6 +297,12 @@ pic_init_number(pic_state *pic)
pic_defun(pic, "abs", pic_number_abs); pic_defun(pic, "abs", pic_number_abs);
pic_defun(pic, "floor-quotient", pic_number_floor_quotient);
pic_defun(pic, "floor-remainder", pic_number_floor_remainder);
pic_defun(pic, "truncate-quotient", pic_number_truncate_quotient);
pic_defun(pic, "truncate-remainder", pic_number_truncate_remainder);
pic_gc_arena_restore(pic, ai);
pic_defun(pic, "floor", pic_number_floor); pic_defun(pic, "floor", pic_number_floor);
pic_defun(pic, "ceiling", pic_number_ceiling); pic_defun(pic, "ceiling", pic_number_ceiling);
pic_defun(pic, "truncate", pic_number_truncate); pic_defun(pic, "truncate", pic_number_truncate);