Class: Hegel::Generators::FloatGenerator

Inherits:
Hegel::Generator show all
Defined in:
lib/hegel/generators.rb,
sig/hegel.rbs

Overview

Hegel::Syntax::Methods#floats. A double in [min_value, max_value], unbounded (the full finite range) by default. allow_nan and allow_infinity both default to false here, unlike hegel-rust's floats() (true when neither bound is set): this milestone exposes a plain, always-off-by-default surface and leaves smallest_nonzero_magnitude, and the allow_nan/allow_infinity/bounds interaction hegel-rust validates, unexposed. A caller who never asks for NaN never has to reason about it, and a keyword can be added to this list later without breaking anyone, where a default flipped from true to false would.

Constant Summary collapse

WIDTH =

hegel_generate_float's width; Ruby has one Float type, the 64-bit IEEE 754 double, so this is never anything else.

Returns:

  • (Integer)
64

Instance Method Summary collapse

Methods inherited from Hegel::Generator

#filter, #map

Constructor Details

#initialize(min_value:, max_value:, allow_nan:, allow_infinity:, exclude_min:, exclude_max:) ⇒ FloatGenerator

Returns a new instance of FloatGenerator.

Parameters:

  • min_value: (Float, nil)
  • max_value: (Float, nil)
  • allow_nan: (Boolean)
  • allow_infinity: (Boolean)
  • exclude_min: (Boolean)
  • exclude_max: (Boolean)


81
82
83
84
85
86
87
88
89
# File 'lib/hegel/generators.rb', line 81

def initialize(min_value:, max_value:, allow_nan:, allow_infinity:, exclude_min:, exclude_max:)
  super()
  @min_value = min_value
  @max_value = max_value
  @allow_nan = allow_nan
  @allow_infinity = allow_infinity
  @exclude_min = exclude_min
  @exclude_max = exclude_max
end

Instance Method Details

#do_draw(tc) ⇒ Float

Parameters:

  • tc (Object)

Returns:

  • (Float)

Raises:



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/hegel/generators.rb', line 91

def do_draw(tc)
  min_value = @min_value || -Float::INFINITY
  max_value = @max_value || Float::INFINITY
  raise Hegel::Error, "floats: max_value < min_value" if max_value < min_value

  tc.generate_float(
    WIDTH, min_value, max_value,
    allow_nan: @allow_nan, allow_infinity: @allow_infinity,
    exclude_min: @exclude_min, exclude_max: @exclude_max,
    smallest_nonzero_magnitude: LibHegel::HEGEL_FLOAT64_SMALLEST_NONZERO_MAGNITUDE_UNRESTRICTED
  )
end