Remember the quadratic function?

Dec 12, 2015 15:02

Here it is in OopenOffice Basic.

Function Quadratic(a,b,c)
Dim OutMat(2), Disc as double, Coeff as double, Coeff2 as double
Dim StrBuild as String
'REM Quadratic takes three numbers, the coefficients of a quadratic equation
'and returns an array of two numbers (or strings if a result is complex)
'representing the roots of the equation. It uses the quadratic formula to
'calculate the result.
Disc =b^2-(4*a*c)
Coeff=-b/(2*a)
If Disc>=0 then
    OutMat(1)=Coeff+(Sqr(Disc)/(2*a))
    OutMat(2)=Coeff-(Sqr(Disc)/(2*a))
Else
    Coeff2=Sqr(Abs(Disc))/(2*a)
    StrBuild=Str(Coeff)+"+"+Coeff2+"i"
    OutMat(1)=StrBuild
    StrBuild=Str(Coeff)+"-"+Coeff2+"i"
    OutMat(2)=StrBuild
end if
Quadratic=OutMat
End Function

Nothing particularly unusual here, but note that, if there is an imaginary output, you have to distinguish between that and a real number and output a string. That's what the If....Then....Else branch does. Both OpenOffice and Office uses strings to represent complex numbers.

openoffice basic, quadratic function, complex numbers

Previous post Next post
Up