From a9ee264fcdc9ae17177694d6d54c7b23c4ac48c9 Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Fri, 19 Nov 2021 01:49:37 +0800 Subject: [PATCH] Add `BigRational#to_big_r` (#11462) --- spec/std/big/big_rational_spec.cr | 5 +++++ src/big/big_rational.cr | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/spec/std/big/big_rational_spec.cr b/spec/std/big/big_rational_spec.cr index 230f50e8cf51..5526d226b179 100644 --- a/spec/std/big/big_rational_spec.cr +++ b/spec/std/big/big_rational_spec.cr @@ -96,6 +96,11 @@ describe BigRational do r.to_big_f.should be_close(f, 0.001) end + it "#to_big_r" do + r = br(10, 3) + r.to_big_r.should eq(r) + end + it "Int#to_big_r" do 3.to_big_r.should eq(br(3, 1)) end diff --git a/src/big/big_rational.cr b/src/big/big_rational.cr index a2a3e884ede8..9c16d1577c69 100644 --- a/src/big/big_rational.cr +++ b/src/big/big_rational.cr @@ -233,6 +233,17 @@ struct BigRational < Number delegate to_i8, to_i16, to_i32, to_i64, to_u8, to_u16, to_u32, to_u64, to: to_f64 + # Returns `self`. + # + # ``` + # require "big" + # + # BigRational.new(4, 5).to_big_r # => 4/5 + # ``` + def to_big_r : BigRational + self + end + def to_big_f : BigFloat BigFloat.new { |mpf| LibGMP.mpf_set_q(mpf, mpq) } end