feat: Add a configuration for an instance that can be set at creation time

And use it to control whether the result of a complex operation where the
   imaginary part comes out to 0 will be coerced back to a number.
This commit is contained in:
Glen Whitney 2022-03-25 13:43:37 -07:00
parent 1c1ba91e48
commit 4213ade4ba
3 changed files with 14 additions and 1 deletions

View file

@ -2,6 +2,7 @@ import { anyComplex } from './complex.js'
export default function create(pmath) {
const complex = pmath('complex')
const resolve = pmath.config.resolveComplex
return pmath('add', [anyComplex, // naive, but this is just a P-o-C
(...addends) => {
let sum = complex(addends[0])
@ -10,6 +11,7 @@ export default function create(pmath) {
sum.re += addend.re
sum.im += addend.im
}
if (resolve && Math.abs(sum.im/sum.re) < 1e-10) return sum.re
return sum
}])
}