Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions spec/core/rational/floor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@
@rational.floor(2).should eql Rational(7857, 25)
@rational.floor(3).should eql Rational(62857, 200)

NATFIXME 'it should return Rational instead of Integer', exception: SpecFailedException do
Rational(100, 2).floor(1).should eql Rational(50, 1)
Rational(100, 2).floor(2).should eql Rational(50, 1)
Rational(-100, 2).floor(1).should eql Rational(-50, 1)
Rational(-100, 2).floor(2).should eql Rational(-50, 1)
end
Rational(100, 2).floor(1).should eql Rational(50, 1)
Rational(100, 2).floor(2).should eql Rational(50, 1)
Rational(-100, 2).floor(1).should eql Rational(-50, 1)
Rational(-100, 2).floor(2).should eql Rational(-50, 1)
end
end
end
5 changes: 3 additions & 2 deletions src/rational_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ bool RationalObject::eq(Env *env, Value other) {
}

Value RationalObject::floor(Env *env, Optional<Value> precision_arg) {
if (m_denominator == 1)
return IntegerMethods::floor(env, m_numerator, precision_arg);

nat_int_t precision = 0;
if (precision_arg)
Expand All @@ -149,6 +147,9 @@ Value RationalObject::floor(Env *env, Optional<Value> precision_arg) {
if (precision == 0)
return to_f(env).as_float()->floor(env, precision_arg);

if (m_denominator == 1)
return create(IntegerMethods::floor(env, m_numerator, precision_arg), Value::integer(1));

auto powered = Natalie::pow(10, precision);
auto numerator = mul(env, powered).as_rational()->floor(env).integer();

Expand Down
Loading