1 require File.dirname(__FILE__) + '/../../spec_helper'
4 describe "BigDecimal#remainder" do
7 @zero = BigDecimal("0")
9 @mixed = BigDecimal("1.23456789")
10 @pos_int = BigDecimal("2E5555")
11 @neg_int = BigDecimal("-2E5555")
12 @pos_frac = BigDecimal("2E-9999")
13 @neg_frac = BigDecimal("-2E-9999")
14 @nan = BigDecimal("NaN")
15 @infinity = BigDecimal("Infinity")
16 @infinity_minus = BigDecimal("-Infinity")
17 @one_minus = BigDecimal("-1")
18 @frac_1 = BigDecimal("1E-99999")
19 @frac_2 = BigDecimal("0.9E-99999")
22 it "if both values are of same sign it equals modulo" do
23 BigDecimal('1234567890123456789012345679').remainder(BigDecimal('1')).should == @zero
24 BigDecimal('123456789').remainder(BigDecimal('333333333333333333333333333E-50')).should == BigDecimal('0.12233333333333333333345679E-24')
26 @mixed.remainder(@pos_frac).should == @mixed % @pos_frac
27 @pos_int.remainder(@pos_frac).should == @pos_int % @pos_frac
28 @neg_frac.remainder(@neg_int).should == @neg_frac % @neg_int
29 @neg_int.remainder(@neg_frac).should == @neg_int % @neg_frac
32 it "Otherwise, it is the modulus minus the value divided by" do
33 @mixed.remainder(@neg_frac).should == (@mixed % @neg_frac) * -1
34 @pos_int.remainder(@neg_frac).should == (@pos_int % @neg_frac) * -1
35 @neg_frac.remainder(@pos_int).should == @neg_frac % (@pos_int * -1)
36 @neg_int.remainder(@pos_frac).should == (@neg_int % @pos_frac) * -1
39 it "returns NaN used with zero" do
40 @mixed.remainder(@zero).nan?.should == true
41 @zero.remainder(@zero).nan?.should == true
44 it "returns zero if used on zero" do
45 @zero.remainder(@mixed).should == @zero
48 it "returns NaN if NaN is involved" do
49 @nan.remainder(@nan).nan?.should == true
50 @nan.remainder(@one).nan?.should == true
51 @one.remainder(@nan).nan?.should == true
52 @infinity.remainder(@nan).nan?.should == true
53 @nan.remainder(@infinity).nan?.should == true
56 it "returns NaN if Infinity is involved" do
57 @infinity.remainder(@infinity).nan?.should == true
58 @infinity.remainder(@one).nan?.should == true
59 @infinity.remainder(@mixed).nan?.should == true
60 @infinity.remainder(@one_minus).nan?.should == true
61 @infinity.remainder(@frac_1).nan?.should == true
62 @one.remainder(@infinity).nan?.should == true
64 @infinity_minus.remainder(@infinity_minus).nan?.should == true
65 @infinity_minus.remainder(@one).nan?.should == true
66 @one.remainder(@infinity_minus).nan?.should == true
67 @frac_2.remainder(@infinity_minus).nan?.should == true
69 @infinity.remainder(@infinity_minus).nan?.should == true
70 @infinity_minus.remainder(@infinity).nan?.should == true
73 it "raises TypeError if the argument cannot be coerced to BigDecimal" do
76 }.should raise_error(TypeError)