From 0aa45ce0aeef89a4381ea362882cd84596bd42be Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sun, 24 Oct 2021 14:50:35 +0200 Subject: [PATCH] Fix `Number.significant` to return `0` as is, not as `Float64` (#11321) --- spec/std/number_spec.cr | 1 + src/number.cr | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/spec/std/number_spec.cr b/spec/std/number_spec.cr index 55f4e51e93c1..51f6cfc9e933 100644 --- a/spec/std/number_spec.cr +++ b/spec/std/number_spec.cr @@ -42,6 +42,7 @@ describe "Number" do it "preserves type" do 123.significant(2).should eq(120) 123.significant(2).should be_a(Int32) + 0.significant(1).should be_a(Int32) end end diff --git a/src/number.cr b/src/number.cr index 333c884185d8..e6c3736e00a7 100644 --- a/src/number.cr +++ b/src/number.cr @@ -223,13 +223,10 @@ struct Number if digits < 0 raise ArgumentError.new "digits should be non-negative" end + return self if zero? x = self.to_f - if x == 0 - return x - end - if base == 10 log = Math.log10(self.abs) elsif base == 2