Skip to content

Commit

Permalink
Call factor_powers_of_ten within BigDecimal#ceil
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Feb 12, 2020
1 parent a2bba01 commit 5596c23
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 7 additions & 0 deletions spec/std/big/big_decimal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -337,36 +337,43 @@ describe BigDecimal do
bd2 = BigDecimal.new(-123, 5)
bd3 = BigDecimal.new(123, 0)
bd4 = BigDecimal.new(-123, 0)
bd5 = "-123.000".to_big_d

bd1.to_i.should eq 0
bd2.to_i.should eq 0
bd3.to_i.should eq 123
bd4.to_i.should eq -123
bd5.to_i.should eq -123

bd1.to_u.should eq 0
bd2.to_u.should eq 0
bd3.to_u.should eq 123
bd4.to_u.should eq 123
bd5.to_u.should eq 123

bd1.to_f.should eq 0.00123
bd2.to_f.should eq -0.00123
bd3.to_f.should eq 123.0
bd4.to_f.should eq -123.0
bd5.to_f.should eq -123.0

bd1.to_i!.should eq 0
bd2.to_i!.should eq 0
bd3.to_i!.should eq 123
bd4.to_i!.should eq -123
bd5.to_i!.should eq -123

bd1.to_u!.should eq 0
bd2.to_u!.should eq 0
bd3.to_u!.should eq 123
bd4.to_u!.should eq 123
bd5.to_u!.should eq 123

bd1.to_f!.should eq 0.00123
bd2.to_f!.should eq -0.00123
bd3.to_f!.should eq 123.0
bd4.to_f!.should eq -123.0
bd5.to_f!.should eq -123.0
end

it "hashes" do
Expand Down
8 changes: 3 additions & 5 deletions src/big/big_decimal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ struct BigDecimal < Number
end

def ceil : BigDecimal
factor_powers_of_ten

mask = power_ten_to(@scale)
diff = (mask - @value % mask) % mask
(self + BigDecimal.new(diff, @scale))
Expand Down Expand Up @@ -331,11 +333,7 @@ struct BigDecimal < Number

# Converts to `BigInt`. Truncates anything on the right side of the decimal point.
def to_big_i
if self >= 0
self.floor.value
else
self.ceil.value
end
(self >= 0 ? floor : ceil).value
end

# Converts to `BigFloat`.
Expand Down

0 comments on commit 5596c23

Please sign in to comment.