1 require File.dirname(__FILE__) + '/../../spec_helper'
2 require File.dirname(__FILE__) + '/shared/quo'
5 describe "BigDecimal#div with precision set to 0" do
6 it_behaves_like(:bigdecimal_quo, :div, 0)
9 describe "BigDecimal#div" do
12 @one = BigDecimal("1")
13 @zero = BigDecimal("0")
14 @zero_plus = BigDecimal("+0")
15 @zero_minus = BigDecimal("-0")
16 @two = BigDecimal("2")
17 @three = BigDecimal("3")
18 @nan = BigDecimal("NaN")
19 @infinity = BigDecimal("Infinity")
20 @infinity_minus = BigDecimal("-Infinity")
21 @one_minus = BigDecimal("-1")
22 @frac_1 = BigDecimal("1E-99999")
23 @frac_2 = BigDecimal("0.9E-99999")
26 it "returns a / b with optional precision" do
27 @two.div(@one).should == @two
28 @one.div(@two).should == @zero
29 # ^^ is this really intended for a class with arbitrary precision?
30 @one.div(@two, 1).should == BigDecimal("0.5")
31 @one.div(@one_minus).should == @one_minus
32 @one_minus.div(@one_minus).should == @one
33 @frac_2.div(@frac_1, 1).should == BigDecimal("0.9")
34 @frac_1.div(@frac_1).should == @one
36 res = "0." + "3" * 1000
38 @one.div(@three, idx).to_s("F").should == "0." + res[2, idx]
42 it "returns NaN if NaN is involved" do
43 @one.div(@nan).nan?.should == true
44 @nan.div(@one).nan?.should == true
47 it "returns NaN if divided by Infinity and no precision given" do
48 @zero.div(@infinity).nan?.should == true
49 @frac_2.div(@infinity).nan?.should == true
52 it "returns 0 if divided by Infinity with given precision" do
53 @zero.div(@infinity, 0).should == 0
54 @frac_2.div(@infinity, 1).should == 0
55 @zero.div(@infinity, 100000).should == 0
56 @frac_2.div(@infinity, 100000).should == 0
59 it "returns NaN if divided by zero and no precision given" do
60 @one.div(@zero).nan?.should == true
61 @one.div(@zero_plus).nan?.should == true
62 @one.div(@zero_minus).nan?.should == true
65 it "returns NaN if zero is divided by zero" do
66 @zero.div(@zero).nan?.should == true
67 @zero_minus.div(@zero_plus).nan?.should == true
68 @zero_plus.div(@zero_minus).nan?.should == true
70 @zero.div(@zero, 0).nan?.should == true
71 @zero_minus.div(@zero_plus, 0).nan?.should == true
72 @zero_plus.div(@zero_minus, 0).nan?.should == true
74 @zero.div(@zero, 10).nan?.should == true
75 @zero_minus.div(@zero_plus, 10).nan?.should == true
76 @zero_plus.div(@zero_minus, 10).nan?.should == true
79 it "returns NaN if (+|-) Infinity divided by 1 and no precision given" do
80 @infinity_minus.div(@one).nan?.should == true
81 @infinity.div(@one).nan?.should == true
82 @infinity_minus.div(@one_minus).nan?.should == true
85 it "returns (+|-)Infinity if (+|-)Infinity by 1 and precision given" do
86 @infinity_minus.div(@one, 0).should == @infinity_minus
87 @infinity.div(@one, 0).should == @infinity
88 @infinity_minus.div(@one_minus, 0).should == @infinity
91 it "returns NaN if Infinity / ((+|-) Infinity)" do
92 @infinity.div(@infinity_minus, 100000).nan?.should == true
93 @infinity_minus.div(@infinity, 1).nan?.should == true