Project

General

Profile

Actions

Bug #18677

open

BigDecimal#power (**) returns FloatDomainError when passing an infinite parameter

Added by dorianmariefr (Dorian MariƩ) over 2 years ago. Updated over 2 years ago.

Status:
Assigned
Target version:
-
ruby -v:
ruby 3.1.1p18 (2022-02-18 revision 53f5fc4236) [x86_64-darwin21]
[ruby-core:<unknown>]

Description

Example:

> BigDecimal(10) ** BigDecimal("Infinity")
FloatDomainError: Computation results in 'Infinity'

Maybe:

require "bigdecimal/util"

class BigDecimal < Numeric
  def **(other)
    if other.infinite? == 1
      if self > 1
        BigDecimal::INFINITY
      elsif self == 1
        self
      elsif self >= 0
        BigDecimal(0)
      else
        power(other)
      end
    else
      power(other)
    end
  end
end

def puts_and_eval(string)
  puts string
  p eval(string)
end

puts_and_eval "10 ** BigDecimal::INFINITY"
puts_and_eval "1 ** BigDecimal::INFINITY"
puts_and_eval "0.1 ** BigDecimal::INFINITY"
puts_and_eval "0 ** BigDecimal::INFINITY"
puts_and_eval "-1 ** BigDecimal::INFINITY"

Seems like ruby is doing very different things from math though

Actions

Also available in: Atom PDF

Like0
Like0Like0