Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Number#round specs #10360

Merged
Merged
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
70 changes: 54 additions & 16 deletions spec/std/number_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,44 +41,67 @@ describe "Number" do
end
end

describe "round" do
it "rounds to 0 digits with base 10 from default" do
describe "#round" do
it "rounds to nearest integer" do
5.5.round.should eq(6)
0.4.round.should eq(0)
-2.8.round.should eq(-3)
0.0.round.should eq(0)
0.49999999999999994.round.should eq(0)
-1763.116.round.should eq(-1763)
753.155.round.should eq(753)
15.151.round.should eq(15)
end

it "10 base" do
it "infinity" do
Float64::INFINITY.round.infinite?.should eq(1)
Float32::INFINITY.round.infinite?.should eq(1)
(-Float64::INFINITY).round.infinite?.should eq(-1)
(-Float32::INFINITY).round.infinite?.should eq(-1)
end

it "nan" do
Float64::NAN.round.nan?.should be_true
Float32::NAN.round.nan?.should be_true
end

it "rounds to digits" do
5.5.round(0).should eq(6)
5.7.round(1).should eq(5.7)
1.2345678.round(2).should eq(1.23)
123456.78.round(-2).should eq(123500) # rounded up
-123456.78.round(-2).should eq(-123500)

-1763.116.round(2).should eq(-1763.12)
753.155.round(2).should eq(753.16)
15.151.round(2).should eq(15.15)
end

it "2 base" do
-1763.116.round(2, base: 2).should eq(-1763.0)
753.155.round(2, base: 2).should eq(753.25)
15.159.round(2, base: 2).should eq(15.25)
0.8346268.round(-1).should eq(0)
end

it "8 base" do
-1763.116.round(2, base: 8).should eq(-1763.109375)
753.155.round(1, base: 8).should eq(753.125)
15.159.round(0, base: 8).should eq(15.0)
it { 42.0.round(300).should eq(42.0) }

pending "edge cases" do
42.0.round(308).should eq(42.0)
42.0.round(309).should eq(42.0)

1.0e307.round(2).should eq(1.0e307)
0.42.round(2**30).should eq(0.42)
end

it "preserves type" do
123.round(2).should eq(123)
123.round(2).should be_a(Int32)
1.round.should be_a(Int32)
1_u8.round.should be_a(UInt8)
1.0_f32.round.should be_a(Float32)
end

it "accepts negative precision" do
it "negative digits" do
123.round(-2).should eq(100)
123.round(-3).should eq(0)
523.round(-3).should eq(1000)

123.456.round(-2).should eq(100)
123_456.123456.round(-5).should eq(100_000)
753.155.round(-5, base: 2).should eq(768)
end

it "accepts unsigned precision" do
Expand All @@ -92,6 +115,21 @@ describe "Number" do
1.098765432109876543210987654321.round(21).should eq(1.098765432109876543211)
6543210987654321.0.round(-15).should eq(7000000000000000.0)
end

describe "base" do
it "2" do
-1763.116.round(2, base: 2).should eq(-1763.0)
753.155.round(2, base: 2).should eq(753.25)
15.159.round(2, base: 2).should eq(15.25)
753.155.round(-5, base: 2).should eq(768)
end

it "8" do
-1763.116.round(2, base: 8).should eq(-1763.109375)
753.155.round(1, base: 8).should eq(753.125)
15.159.round(0, base: 8).should eq(15.0)
end
end
end

it "gives the absolute value" do
Expand Down