1 require File.dirname(__FILE__) + '/../../spec_helper'
4 describe "BigDecimal#ceil" do
6 @zero = BigDecimal("0")
8 @three = BigDecimal("3")
9 @four = BigDecimal("4")
10 @mixed = BigDecimal("1.23456789")
11 @mixed_big = BigDecimal("1.23456789E100")
12 @pos_int = BigDecimal("2E5555")
13 @neg_int = BigDecimal("-2E5555")
14 @pos_frac = BigDecimal("2E-9999")
15 @neg_frac = BigDecimal("-2E-9999")
17 @infinity = BigDecimal("Infinity")
18 @infinity_neg = BigDecimal("-Infinity")
19 @nan = BigDecimal("NaN")
20 @zero_pos = BigDecimal("+0")
21 @zero_neg = BigDecimal("-0")
24 it "returns a BigDecimal" do
25 @mixed.ceil.kind_of?(BigDecimal).should == true
26 @pos_int.ceil(2).kind_of?(BigDecimal).should == true
29 it "returns the smallest integer greater or equal to self, if n is unspecified" do
30 @pos_int.ceil.should == @pos_int
31 @neg_int.ceil.should == @neg_int
32 @pos_frac.ceil.should == BigDecimal("1")
33 @neg_frac.ceil.should == @zero
34 @infinity.ceil.should == @infinity
35 @infinity_neg.ceil.should == @infinity_neg
36 @nan.ceil.nan?.should == true
37 @zero.ceil.should == 0
38 @zero_pos.ceil.should == @zero_pos
39 @zero_neg.ceil.should == @zero_neg
42 BigDecimal('2.3').ceil.should == 3
43 BigDecimal('2.5').ceil.should == 3
44 BigDecimal('2.9999').ceil.should == 3
45 BigDecimal('-2.3').ceil.should == -2
46 BigDecimal('-2.5').ceil.should == -2
47 BigDecimal('-2.9999').ceil.should == -2
50 it "returns n digits right of the decimal point if given n > 0" do
51 @mixed.ceil(1).should == BigDecimal("1.3")
52 @mixed.ceil(5).should == BigDecimal("1.23457")
54 BigDecimal("-0.03").ceil(1).should == BigDecimal("0")
55 BigDecimal("0.03").ceil(1).should == BigDecimal("0.1")
57 BigDecimal("23.45").ceil(0).should == BigDecimal('24')
58 BigDecimal("23.45").ceil(1).should == BigDecimal('23.5')
59 BigDecimal("23.45").ceil(2).should == BigDecimal('23.45')
61 BigDecimal("-23.45").ceil(0).should == BigDecimal('-23')
62 BigDecimal("-23.45").ceil(1).should == BigDecimal('-23.4')
63 BigDecimal("-23.45").ceil(2).should == BigDecimal('-23.45')
65 BigDecimal("2E-10").ceil(0).should == @one
66 BigDecimal("2E-10").ceil(9).should == BigDecimal('1E-9')
67 BigDecimal("2E-10").ceil(10).should == BigDecimal('2E-10')
68 BigDecimal("2E-10").ceil(11).should == BigDecimal('2E-10')
71 # 0.4, 0.34, 0.334, etc.
72 (@one/@three).ceil(n).should == BigDecimal("0.#{'3'*(n-1)}4")
73 # 1.4, 1.34, 1.334, etc.
74 (@four/@three).ceil(n).should == BigDecimal("1.#{'3'*(n-1)}4")
75 (BigDecimal('31')/@three).ceil(n).should == BigDecimal("10.#{'3'*(n-1)}4")
78 # -0.4, -0.34, -0.334, etc.
79 (-@one/@three).ceil(n).should == BigDecimal("-0.#{'3'* n}")
82 (@three/@one).ceil(n).should == @three
85 (-@three/@one).ceil(n).should == -@three
89 it "sets n digits left of the decimal point to 0, if given n < 0" do
90 BigDecimal("13345.234").ceil(-2).should == BigDecimal("13400.0")
91 @mixed_big.ceil(-99).should == BigDecimal("0.13E101")
92 @mixed_big.ceil(-100).should == BigDecimal("0.2E101")
93 @mixed_big.ceil(-95).should == BigDecimal("0.123457E101")
94 BigDecimal("1E10").ceil(-30).should == BigDecimal('1E30')
95 BigDecimal("-1E10").ceil(-30).should == @zero