Project

General

Profile

Actions

Bug #18677

open

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

Added by dorianmariefr (Dorian MariƩ) 8 months ago. Updated 7 months ago.

Status:
Assigned
Priority:
Normal
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

Updated by nobu (Nobuyoshi Nakada) 7 months ago

  • Status changed from Open to Assigned
  • Assignee set to mrkn (Kenta Murata)

Updated by mrkn (Kenta Murata) 7 months ago

I made a patch at https://github.com/ruby/bigdecimal/pull/227.

Note that the last case -1 ** BigDecimal::INFINITY should cause Math::DomainError.

Actions

Also available in: Atom PDF

Like0
Like0Like0