Project

General

Profile

Actions

Bug #20881

open

Complex#** returns discontinuous results in Fixnum and Bignum exponents

Added by mame (Yusuke Endoh) 10 days ago. Updated 6 days ago.

Status:
Assigned
Target version:
-
ruby -v:
ruby 3.4.0dev (2024-10-18T02:35:00Z master 9a98b70a50) +PRISM [x86_64-linux]
[ruby-core:119839]

Description

Complex#** performs the straightforward calculation when the exponent is Fixnum, but performs the computation in the polar coordinate system when the exponent is Bignum. Therefore, errors accumulate in the former case and not in the latter.

(Complex.polar(1, 1) ** 100000000000000).abs        #=> 1.0033483420677842
(Complex.polar(1, 1) ** 1000000000000000).abs       #=> 1.0339924646874608
(Complex.polar(1, 1) ** 10000000000000000).abs      #=> 1.3969270854305682
(Complex.polar(1, 1) ** 100000000000000000).abs     #=> 28.29680363011009
(Complex.polar(1, 1) ** 1000000000000000000).abs    #=> 329134804659039.0
# ↑ Fixnum
# ↓ Bignum
(Complex.polar(1, 1) ** 10000000000000000000).abs   #=> 1.0 (w/ warning: in a**b, b may be too big)
(Complex.polar(1, 1) ** 100000000000000000000).abs  #=> 0.9999999999999999 (w/ warning: in a**b, b may be too big)
(Complex.polar(1, 1) ** 1000000000000000000000).abs #=> 1.0 (w/ warning: in a**b, b may be too big)

It may be good to always calculate them in the polar coordinate. I found this issue when addressing #20811.

Updated by tompng (tomoya ishida) 10 days ago

I slightly expect (1.0+2.0i)**5 to be exactly 41.0-38.0i instead of 40.99999999999999-38.00000000000003i calculated in polar coordinate.

Precise value of Complex.polar(1, 1).abs is 1.00000000000000002423 (rounded to 1.0).
(Complex.polar(1, 1.11) ** 1000000000000000000000).abs is 0.0.
Complex.polar(1, 1.12).abs2 is 1.0000000000000002 (abs is rounded to 1.0)
In this case, straightforward calculation and polar coordinate calculation is both equally inaccurate. (https://gist.github.com/tompng/9e3570063ba803d0f54b04b9bd2d2387)

I think the only problem is the discontinuous gap.

Actions #2

Updated by hsbt (Hiroshi SHIBATA) 6 days ago

  • Status changed from Open to Assigned
Actions

Also available in: Atom PDF

Like0
Like0Like0