summaryrefslogtreecommitdiff
path: root/spec/ruby/library/bigdecimal/round_spec.rb
blob: 6c1987c5d8d6a3704665ace69819d5a02d3159ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
require File.expand_path('../../../spec_helper', __FILE__)
require 'bigdecimal'

describe "BigDecimal#round" do
  before :each do
    @one   = BigDecimal("1")
    @two   = BigDecimal("2")
    @three = BigDecimal("3")

    @neg_one   = BigDecimal("-1")
    @neg_two   = BigDecimal("-2")
    @neg_three = BigDecimal("-3")

    @p1_50 = BigDecimal("1.50")
    @p1_51 = BigDecimal("1.51")
    @p1_49 = BigDecimal("1.49")
    @n1_50 = BigDecimal("-1.50")
    @n1_51 = BigDecimal("-1.51")
    @n1_49 = BigDecimal("-1.49")

    @p2_50 = BigDecimal("2.50")
    @p2_51 = BigDecimal("2.51")
    @p2_49 = BigDecimal("2.49")
    @n2_50 = BigDecimal("-2.50")
    @n2_51 = BigDecimal("-2.51")
    @n2_49 = BigDecimal("-2.49")
  end

  after :each do
    BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_HALF_UP)
  end

  it "uses default rounding method unless given" do
    @p1_50.round(0).should == @two
    @p1_51.round(0).should == @two
    @p1_49.round(0).should == @one
    @n1_50.round(0).should == @neg_two
    @n1_51.round(0).should == @neg_two
    @n1_49.round(0).should == @neg_one

    @p2_50.round(0).should == @three
    @p2_51.round(0).should == @three
    @p2_49.round(0).should == @two
    @n2_50.round(0).should == @neg_three
    @n2_51.round(0).should == @neg_three
    @n2_49.round(0).should == @neg_two

    BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_DOWN)

    @p1_50.round(0).should == @one
    @p1_51.round(0).should == @one
    @p1_49.round(0).should == @one
    @n1_50.round(0).should == @neg_one
    @n1_51.round(0).should == @neg_one
    @n1_49.round(0).should == @neg_one

    @p2_50.round(0).should == @two
    @p2_51.round(0).should == @two
    @p2_49.round(0).should == @two
    @n2_50.round(0).should == @neg_two
    @n2_51.round(0).should == @neg_two
    @n2_49.round(0).should == @neg_two
  end

  describe "BigDecimal::ROUND_UP" do
    it "rounds values away from zero" do
      @p1_50.round(0, BigDecimal::ROUND_UP).should == @two
      @p1_51.round(0, BigDecimal::ROUND_UP).should == @two
      @p1_49.round(0, BigDecimal::ROUND_UP).should == @two
      @n1_50.round(0, BigDecimal::ROUND_UP).should == @neg_two
      @n1_51.round(0, BigDecimal::ROUND_UP).should == @neg_two
      @n1_49.round(0, BigDecimal::ROUND_UP).should == @neg_two

      @p2_50.round(0, BigDecimal::ROUND_UP).should == @three
      @p2_51.round(0, BigDecimal::ROUND_UP).should == @three
      @p2_49.round(0, BigDecimal::ROUND_UP).should == @three
      @n2_50.round(0, BigDecimal::ROUND_UP).should == @neg_three
      @n2_51.round(0, BigDecimal::ROUND_UP).should == @neg_three
      @n2_49.round(0, BigDecimal::ROUND_UP).should == @neg_three
    end
  end

  describe "BigDecimal::ROUND_DOWN" do
    it "rounds values towards zero" do
      @p1_50.round(0, BigDecimal::ROUND_DOWN).should == @one
      @p1_51.round(0, BigDecimal::ROUND_DOWN).should == @one
      @p1_49.round(0, BigDecimal::ROUND_DOWN).should == @one
      @n1_50.round(0, BigDecimal::ROUND_DOWN).should == @neg_one
      @n1_51.round(0, BigDecimal::ROUND_DOWN).should == @neg_one
      @n1_49.round(0, BigDecimal::ROUND_DOWN).should == @neg_one

      @p2_50.round(0, BigDecimal::ROUND_DOWN).should == @two
      @p2_51.round(0, BigDecimal::ROUND_DOWN).should == @two
      @p2_49.round(0, BigDecimal::ROUND_DOWN).should == @two
      @n2_50.round(0, BigDecimal::ROUND_DOWN).should == @neg_two
      @n2_51.round(0, BigDecimal::ROUND_DOWN).should == @neg_two
      @n2_49.round(0, BigDecimal::ROUND_DOWN).should == @neg_two
    end
  end

  describe "BigDecimal::ROUND_HALF_UP" do
    it "rounds values >= 5 up, otherwise down" do
      @p1_50.round(0, BigDecimal::ROUND_HALF_UP).should == @two
      @p1_51.round(0, BigDecimal::ROUND_HALF_UP).should == @two
      @p1_49.round(0, BigDecimal::ROUND_HALF_UP).should == @one
      @n1_50.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_two
      @n1_51.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_two
      @n1_49.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_one

      @p2_50.round(0, BigDecimal::ROUND_HALF_UP).should == @three
      @p2_51.round(0, BigDecimal::ROUND_HALF_UP).should == @three
      @p2_49.round(0, BigDecimal::ROUND_HALF_UP).should == @two
      @n2_50.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_three
      @n2_51.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_three
      @n2_49.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_two
    end
  end

  describe "BigDecimal::ROUND_HALF_DOWN" do
    it "rounds values > 5 up, otherwise down" do
      @p1_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @one
      @p1_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @two
      @p1_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @one
      @n1_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_one
      @n1_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_two
      @n1_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_one

      @p2_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @two
      @p2_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @three
      @p2_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @two
      @n2_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_two
      @n2_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_three
      @n2_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_two
    end
  end

  describe "BigDecimal::ROUND_CEILING" do
    it "rounds values towards +infinity" do
      @p1_50.round(0, BigDecimal::ROUND_CEILING).should == @two
      @p1_51.round(0, BigDecimal::ROUND_CEILING).should == @two
      @p1_49.round(0, BigDecimal::ROUND_CEILING).should == @two
      @n1_50.round(0, BigDecimal::ROUND_CEILING).should == @neg_one
      @n1_51.round(0, BigDecimal::ROUND_CEILING).should == @neg_one
      @n1_49.round(0, BigDecimal::ROUND_CEILING).should == @neg_one

      @p2_50.round(0, BigDecimal::ROUND_CEILING).should == @three
      @p2_51.round(0, BigDecimal::ROUND_CEILING).should == @three
      @p2_49.round(0, BigDecimal::ROUND_CEILING).should == @three
      @n2_50.round(0, BigDecimal::ROUND_CEILING).should == @neg_two
      @n2_51.round(0, BigDecimal::ROUND_CEILING).should == @neg_two
      @n2_49.round(0, BigDecimal::ROUND_CEILING).should == @neg_two
    end
  end

  describe "BigDecimal::ROUND_FLOOR" do
    it "rounds values towards -infinity" do
      @p1_50.round(0, BigDecimal::ROUND_FLOOR).should == @one
      @p1_51.round(0, BigDecimal::ROUND_FLOOR).should == @one
      @p1_49.round(0, BigDecimal::ROUND_FLOOR).should == @one
      @n1_50.round(0, BigDecimal::ROUND_FLOOR).should == @neg_two
      @n1_51.round(0, BigDecimal::ROUND_FLOOR).should == @neg_two
      @n1_49.round(0, BigDecimal::ROUND_FLOOR).should == @neg_two

      @p2_50.round(0, BigDecimal::ROUND_FLOOR).should == @two
      @p2_51.round(0, BigDecimal::ROUND_FLOOR).should == @two
      @p2_49.round(0, BigDecimal::ROUND_FLOOR).should == @two
      @n2_50.round(0, BigDecimal::ROUND_FLOOR).should == @neg_three
      @n2_51.round(0, BigDecimal::ROUND_FLOOR).should == @neg_three
      @n2_49.round(0, BigDecimal::ROUND_FLOOR).should == @neg_three
    end
  end

  describe "BigDecimal::ROUND_HALF_EVEN" do
    it "rounds values > 5 up, < 5 down and == 5 towards even neighbor" do
      @p1_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two
      @p1_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two
      @p1_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @one
      @n1_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two
      @n1_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two
      @n1_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_one

      @p2_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two
      @p2_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @three
      @p2_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two
      @n2_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two
      @n2_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_three
      @n2_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two
    end
  end

  it 'raise exception, if self is special value' do
    lambda { BigDecimal('NaN').round }.should raise_error(FloatDomainError)
    lambda { BigDecimal('Infinity').round }.should raise_error(FloatDomainError)
    lambda { BigDecimal('-Infinity').round }.should raise_error(FloatDomainError)
  end

  it 'do not raise exception, if self is special value and precision is given' do
    lambda { BigDecimal('NaN').round(2) }.should_not raise_error(FloatDomainError)
    lambda { BigDecimal('Infinity').round(2) }.should_not raise_error(FloatDomainError)
    lambda { BigDecimal('-Infinity').round(2) }.should_not raise_error(FloatDomainError)
  end
end