Skip to content

Commit

Permalink
add Float#floor and Float#ceil
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Somerville committed Jan 27, 2013
1 parent 1305ea4 commit 40ca4e9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/lib/float.c
Expand Up @@ -92,6 +92,18 @@ sl_float_round(sl_vm_t* vm, SLVAL self)
}
}

static SLVAL
sl_float_floor(sl_vm_t* vm, SLVAL self)
{
return sl_make_float(vm, floor(sl_get_float(vm, self)));
}

static SLVAL
sl_float_ceil(sl_vm_t* vm, SLVAL self)
{
return sl_make_float(vm, ceil(sl_get_float(vm, self)));
}

void
sl_init_float(sl_vm_t* vm)
{
Expand All @@ -105,6 +117,8 @@ sl_init_float(sl_vm_t* vm)
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, "floor", 0, sl_float_floor);
sl_define_method(vm, vm->lib.Float, "ceil", 0, sl_float_ceil);
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
12 changes: 12 additions & 0 deletions test/core/float.sl
Expand Up @@ -99,6 +99,18 @@ class FloatTest extends Test {
assert_equal(5, 4.5.round);
assert_equal(100000000000000000000, 99999999999999999999.5.round);
}
def test_floor {
assert_equal(4, 4.4.floor);
assert_equal(4, 4.5.floor);
assert_equal(5, 5.0.floor);
}
def test_ceil {
assert_equal(5, 4.4.ceil);
assert_equal(5, 4.5.ceil);
assert_equal(5, 5.0.ceil);
}
def test_class {
assert_is_a(Float, 123.0);
Expand Down

0 comments on commit 40ca4e9

Please sign in to comment.