diff options
author | xuri <xuri.me@gmail.com> | 2021-04-03 22:56:02 +0800 |
---|---|---|
committer | xuri <xuri.me@gmail.com> | 2021-04-03 22:56:02 +0800 |
commit | 89c262fc1d525f74bb0e2fb61ae7a3d10d07a12a (patch) | |
tree | 76883f69be3b44beeb18e7da66183e8d591df123 /calc.go | |
parent | 6e812a27c6e74a141e301c0f19484743ea437c52 (diff) |
Fixed #813, streaming data writer result missing after call normal API
#65 formula function: COMPLEX
Diffstat (limited to 'calc.go')
-rw-r--r-- | calc.go | 30 |
1 files changed, 30 insertions, 0 deletions
@@ -248,6 +248,7 @@ var tokenPriority = map[string]int{ // COLUMNS // COMBIN // COMBINA +// COMPLEX // CONCAT // CONCATENATE // COS @@ -1449,6 +1450,35 @@ func (fn *formulaFuncs) bitwise(name string, argsList *list.List) formulaArg { return newNumberFormulaArg(float64(bitwiseFunc(int(num1.Number), int(num2.Number)))) } +// COMPLEX function takes two arguments, representing the real and the +// imaginary coefficients of a complex number, and from these, creates a +// complex number. The syntax of the function is: +// +// COMPLEX(real_num,i_num,[suffix]) +// +func (fn *formulaFuncs) COMPLEX(argsList *list.List) formulaArg { + if argsList.Len() < 2 { + return newErrorFormulaArg(formulaErrorVALUE, "COMPLEX requires at least 2 arguments") + } + if argsList.Len() > 3 { + return newErrorFormulaArg(formulaErrorVALUE, "COMPLEX allows at most 3 arguments") + } + real, i, suffix := argsList.Front().Value.(formulaArg).ToNumber(), argsList.Front().Next().Value.(formulaArg).ToNumber(), "i" + if real.Type != ArgNumber { + return real + } + if i.Type != ArgNumber { + return i + } + if argsList.Len() == 3 { + if suffix = strings.ToLower(argsList.Back().Value.(formulaArg).Value()); suffix != "i" && suffix != "j" { + return newErrorFormulaArg(formulaErrorVALUE, formulaErrorVALUE) + } + } + r := strings.NewReplacer("(", "", ")", "", "0+", "", "+0i", "", "0+0i", "0", "i", suffix) + return newStringFormulaArg(r.Replace(fmt.Sprint(complex(real.Number, i.Number)))) +} + // DEC2BIN function converts a decimal number into a Binary (Base 2) number. // The syntax of the function is: // |