RangeError
RubyERRORNotableMath

Numeric value out of valid range

Quick Answer

Validate numeric inputs against expected bounds before passing to range-sensitive operations.

What this means

Raised when a numeric value is valid in type but falls outside the range accepted by an operation. For example, creating a Bignum that exceeds platform limits, or passing an out-of-range value to a C-level function.

Why it happens
  1. 1Passing an excessively large integer to an operation that requires a machine-word-size integer
  2. 2Converting a Float::INFINITY or NaN to an Integer

Fix

Clamp values to valid range

Clamp values to valid range
value = raw_input.to_f
clamped = value.clamp(0.0, 1.0)   # always in [0, 1]

Why this works

Numeric#clamp constrains a value to a range without raising.

Code examples
Converting Infinity to Integerruby
Float::INFINITY.to_i
# RangeError: Infinity
Large float to integerruby
(10**400).to_i   # works — Ruby Bignum is arbitrary precision
# But platform APIs have limits
Rescue RangeErrorruby
begin
  result = huge_float.to_i
rescue RangeError => e
  result = Float::INFINITY
end
Sources
Official documentation ↗

Ruby Core Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Ruby errors