FloatDomainError
RubyERRORNotableMath
Float value outside mathematical domain
Quick Answer
Validate inputs to Math functions — sqrt requires non-negative, log requires positive values.
What this means
A subclass of RangeError raised specifically for floating-point domain errors, such as calling Math functions with values outside their domain (e.g., sqrt of a negative number).
Why it happens
- 1Calling Math.sqrt with a negative number
- 2Calling Math.log with zero or a negative number
- 3Domain violations in trigonometric or exponential functions
Fix
Validate input before math operations
Validate input before math operations
def safe_sqrt(n) raise ArgumentError, "sqrt requires non-negative input" if n < 0 Math.sqrt(n) end
Why this works
Pre-validation gives a meaningful error message before the C-level domain check fires.
Code examples
Math.sqrt with negativeruby
Math.sqrt(-1) # Math::DomainError: Numerical argument is out of domain - "sqrt"
Math.log with zeroruby
Math.log(0) # Math::DomainError: Numerical argument is out of domain - "log"
Using Complex for negativesruby
require 'complex' Complex(-1).sqrt # => (0+1.0i) — no error
Same error in other languages
Sources
Official documentation ↗
Ruby Core Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev