From f1fc09429dec8a212d0aed26439fbd0ccb209d97 Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Wed, 21 May 2025 16:30:07 +0800 Subject: [PATCH] Add `BigRational#to_i` --- spec/std/big/big_rational_spec.cr | 8 ++++++++ src/big/big_rational.cr | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/spec/std/big/big_rational_spec.cr b/spec/std/big/big_rational_spec.cr index f2175e1273ae..28b9d7d20df9 100644 --- a/spec/std/big/big_rational_spec.cr +++ b/spec/std/big/big_rational_spec.cr @@ -84,6 +84,14 @@ describe BigRational do r.to_s(36).should eq("4woiz/9b3djm") end + it "#to_i" do + br(10, 3).to_i.should eq(3) + br(90, 3).to_i.should eq(30) + br(1, 98).to_i.should eq(0) + br(-10, 3).to_i.should eq(-3) + expect_raises(OverflowError) { br(Int64::MAX, 1).to_i } + end + it "#to_f64" do r = br(10, 3) f = 10.to_f64 / 3.to_f64 diff --git a/src/big/big_rational.cr b/src/big/big_rational.cr index 1c25adf9434d..38298f0fa3b1 100644 --- a/src/big/big_rational.cr +++ b/src/big/big_rational.cr @@ -310,6 +310,10 @@ struct BigRational < Number to_f64! end + def to_i : Int32 + to_i32 + end + delegate to_i8, to_i16, to_i32, to_i64, to_u8, to_u16, to_u32, to_u64, to: to_f64 # Returns `self`.