Skip to content

Commit

Permalink
add Float#round
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Somerville committed Jan 27, 2013
1 parent e06bec5 commit 73e1c6f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/lib/float.c
Expand Up @@ -81,6 +81,17 @@ sl_float_to_f(sl_vm_t* vm, SLVAL self)
return self;
}

static SLVAL
sl_float_round(sl_vm_t* vm, SLVAL self)
{
double d = round(sl_get_float(vm, self));
if(d < SL_MIN_INT || d > SL_MAX_INT) {
return sl_make_bignum_f(vm, d);
} else {
return sl_make_int(vm, (int)d);
}
}

void
sl_init_float(sl_vm_t* vm)
{
Expand All @@ -93,6 +104,7 @@ sl_init_float(sl_vm_t* vm)
sl_define_method(vm, vm->lib.Float, "succ", 0, sl_float_succ);
sl_define_method(vm, vm->lib.Float, "pred", 0, sl_float_pred);
sl_define_method(vm, vm->lib.Float, "negate", 0, sl_float_negate);
sl_define_method(vm, vm->lib.Float, "round", 0, sl_float_round);
sl_define_method(vm, vm->lib.Float, "+", 1, sl_float_add);
sl_define_method(vm, vm->lib.Float, "-", 1, sl_float_sub);
sl_define_method(vm, vm->lib.Float, "*", 1, sl_float_mul);
Expand Down
6 changes: 6 additions & 0 deletions test/core/float.sl
Expand Up @@ -91,6 +91,12 @@ class FloatTest extends Test {
assert_equal(1, Infinity <=> 9999999999999999999999999999999999999999);
assert_equal(-1, 0.5555 <=> 9999999999999999999999999999999999999999)
}
def test_round {
assert_equal(4, 4.4.round);
assert_equal(5, 4.5.round);
assert_equal(100000000000000000000, 99999999999999999999.5.round);
}
def test_class {
assert_is_a(Float, 123.0);
Expand Down

0 comments on commit 73e1c6f

Please sign in to comment.