Class: BigDecimal

Inherits:
Object show all
Defined in:
lib/propr/random/bigdecimal.rb,
lib/propr/shrink/bigdecimal.rb

Constant Summary collapse

INF =
BigDecimal("Infinity")
NAN =
BigDecimal("NaN")

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.random(options = {}, m = Propr::Random) ⇒ BigDecimal

Returns:



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
# File 'lib/propr/random/bigdecimal.rb', line 8

def random(options = {}, m = Propr::Random)
  min = BigDecimal(options[:min] || -INF)
  max = BigDecimal(options[:max] ||  INF)

  min_ = if min.finite? then min else BigDecimal(-Float::MAX, 0) end
  max_ = if max.finite? then max else BigDecimal( Float::MAX, 0) end

  range  = max_ - min_
  center = options.fetch(:center, :mid)
  center =
    case center
    when :mid then min_ + (max_ - min_).div(2)
    when :min then min_
    when :max then max_
    when Numeric
      raise ArgumentError,
        "center < min" if center < min
      raise ArgumentError,
        "center > max" if center > max
      center
    else raise ArgumentError,
      "center must be :min, :mid, :max, or min <= Integer <= max"
    end

  # @todo: -INF, +INF, -0.0, NAN
  m.bind(m.rand(range)) do |a|
    m.bind(m.rand(max_ - min_)) do |b|
      c  = BigDecimal(a) + BigDecimal(min_, 0)
      c += c / BigDecimal(b) # not 0..1
      c  = max if c > max
      c  = min if c < min
      m.scale(c, range, center)
    end
  end
end

Instance Method Details

#shrinkArray<BigDecimal>

Returns:



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/propr/shrink/bigdecimal.rb', line 5

def shrink
  limit = 10

  Array.unfold(self) do |seed|
    limit -= 1
    zero   = 0
    seed_  = zero + (seed - zero) / 2

    (limit > 0 && (seed - seed_).abs > 1e-5)
      .maybe([self + zero - seed, seed_])
  end
end