Skip to content

Difficulties in z.log for im(z) >> re (z) #40

@gwhitney

Description

@gwhitney

(From investigating josdejong/mathjs#2503)

Consider the following program using complex.js (v2.0.15):

import Complex from 'complex.js'

const reals = [0,-1,1]
const ims = [1e10, 1e15, 1e17, 1e18, 1e19, 1e30]
for (const r of reals) {
    for (const im of ims) {
        const c = new Complex(r, im)
        console.log(r, im, ':', c.log())
    }
}

It produces output:

0 10000000000 : { re: 23.025850929940457, im: 1.5707963267948966 }
0 1000000000000000 : { re: 34.538776394910684, im: 1.5707963267948966 }
0 100000000000000000 : { re: 39.14394658089878, im: 1.5707963267948966 }
0 1000000000000000000 : { re: 41.44653167389282, im: 1.5707963267948966 }
0 10000000000000000000 : { re: 43.74911676688687, im: 1.5707963267948966 }
0 1e+30 : { re: 69.07755278982137, im: 1.5707963267948966 }
-1 10000000000 : { re: 23.025849239078866, im: 1.5707963268948968 }
-1 1000000000000000 : { re: 34.490947945738256, im: 1.5707963267948977 }
-1 100000000000000000 : { re: 36.3662940453822, im: 1.5707963267948968 }
-1 1000000000000000000 : { re: 36.3662940453822, im: 1.5707963267948968 }
-1 10000000000000000000 : { re: NaN, im: 1.5707963267948966 }
-1 1e+30 : { re: NaN, im: 1.5707963267948966 }
1 10000000000 : { re: 23.025850234876927, im: 1.5707963266948965 }
1 1000000000000000 : { re: 34.59069013472681, im: 1.5707963267948957 }
1 100000000000000000 : { re: 37.33185619326892, im: 1.5707963267948966 }
1 1000000000000000000 : { re: 37.33185619326892, im: 1.5707963267948966 }
1 10000000000000000000 : { re: 37.33185619326892, im: 1.5707963267948966 }
1 1e+30 : { re: 37.33185619326892, im: 1.5707963267948966 }

So the re = 0 values look plausible, but the re = 1 values have the real part of the log bog down in a way that leaves them vastly off, and the degradation in the re = -1 values is even worse, eventually yielding NaN. Although of course with floats it's impossible to capture the miniscule differences from tau/4 in the theta of these various numbers, the complex module should be able to capture their magnitude and thereby return .log() values with reasonable real parts. This issue is a blocker for a contributor working on an implementation of lngamma for mathjs, so thank you for any attention you're able to give it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions