summaryrefslogtreecommitdiff
path: root/spec/ruby/core/integer/fdiv_spec.rb
blob: d9ea2fdf8d57eab6efa6364e3a4763355d66d8e8 (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
require_relative '../../spec_helper'

describe "Integer#fdiv" do
  it "performs floating-point division between self and a fixnum" do
    8.fdiv(7).should be_close(1.14285714285714, TOLERANCE)
  end

  it "performs floating-point division between self and a bignum" do
    8.fdiv(bignum_value).should be_close(8.673617379884035e-19, TOLERANCE)
  end

  it "performs floating-point division between self bignum and a bignum" do
    num = 1000000000000000000000000000000000048148248609680896326399448564623182963452541226153892315137780403285956264146010000000000000000000000000000000000048148248609680896326399448564623182963452541226153892315137780403285956264146010000000000000000000000000000000000048148248609680896326399448564623182963452541226153892315137780403285956264146009
    den = 2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    num.fdiv(den).should == 500.0
  end

  it "rounds to the correct value for bignums" do
    den = 9 * 10**342

    num = 1 * 10**344
    num.fdiv(den).should == 11.11111111111111

    num = 1 * 10**343
    num.fdiv(den).should == 1.1111111111111112

    num = 1 * 10**342
    num.fdiv(den).should == 0.1111111111111111

    num = 2 * 10**342
    num.fdiv(den).should == 0.2222222222222222

    num = 3 * 10**342
    num.fdiv(den).should == 0.3333333333333333

    num = 4 * 10**342
    num.fdiv(den).should == 0.4444444444444444

    num = 5 * 10**342
    num.fdiv(den).should == 0.5555555555555556

    num = 6 * 10**342
    num.fdiv(den).should == 0.6666666666666666

    num = 7 * 10**342
    num.fdiv(den).should == 0.7777777777777778

    num = 8 * 10**342
    num.fdiv(den).should == 0.8888888888888888

    num = 9 * 10**342
    num.fdiv(den).should == 1.0

    num = -5 * 10**342
    num.fdiv(den).should == -0.5555555555555556
  end

  it "rounds to the correct float for bignum denominators" do
    1.fdiv(10**324).should == 0.0
    1.fdiv(10**323).should == 1.0e-323
  end

  it "performs floating-point division between self and a Float" do
    8.fdiv(9.0).should be_close(0.888888888888889, TOLERANCE)
  end

  it "returns NaN when the argument is NaN" do
    -1.fdiv(nan_value).nan?.should be_true
    1.fdiv(nan_value).nan?.should be_true
  end

  it "returns Infinity when the argument is 0" do
    1.fdiv(0).infinite?.should == 1
  end

  it "returns -Infinity when the argument is 0 and self is negative" do
    -1.fdiv(0).infinite?.should == -1
  end

  it "returns Infinity when the argument is 0.0" do
    1.fdiv(0.0).infinite?.should == 1
  end

  it "returns -Infinity when the argument is 0.0 and self is negative" do
    -1.fdiv(0.0).infinite?.should == -1
  end

  it "raises a TypeError when argument isn't numeric" do
    -> { 1.fdiv(mock('non-numeric')) }.should raise_error(TypeError)
  end

  it "raises an ArgumentError when passed multiple arguments" do
    -> { 1.fdiv(6,0.2) }.should raise_error(ArgumentError)
  end

  it "follows the coercion protocol" do
    (obj = mock('10')).should_receive(:coerce).with(1).and_return([1, 10])
    1.fdiv(obj).should == 0.1
  end
end