72 lines
1.9 KiB
Plaintext
72 lines
1.9 KiB
Plaintext
//
|
|
// Arithmetic/Complex
|
|
// Using FutureBasic 7.0.34
|
|
// August 2025, R.W
|
|
|
|
include "Tlbx complex.incl"
|
|
// through this include file, FB supports the following
|
|
// double complex functions (as of August 2025).
|
|
// Float and long double complex might be
|
|
// available in later versions
|
|
|
|
//fn creal, cimag, cabs, carg, conj, cproj
|
|
//fn csqrt, cexp, clog, cpow
|
|
//fn csin, ccos, ctan
|
|
//fn casin, cacos, catan
|
|
//fn csinh, ccosh, ctanh
|
|
//fn casinh,cacosh,catanh
|
|
|
|
// Below is redefining "I" to Cj since I don't want to lose the
|
|
// ability to use "i" as my favorite FOR NEXT / loop variable
|
|
BeginCDeclaration
|
|
#define Cj I
|
|
EndC
|
|
system double Cj
|
|
|
|
local fn PrintComplexNumber( z as double_complex )
|
|
double y
|
|
print @fn creal( z ) " ";
|
|
y = fn cimag( z )
|
|
if ( y >= 0.0 ) then print @"+"; : else print @"-";
|
|
print @abs( y ) "i"
|
|
end fn
|
|
|
|
window 1,@"Arithmetic/Complex"
|
|
|
|
double_complex z1, z2, z3, z4, z5, z6
|
|
double_complex z7, z8, z9, zs, zn
|
|
|
|
z1 = 1.5 + 3 * Cj //1.5 + 3i
|
|
z2 = 1.5 + 1.5 * Cj //1.5 + 1.5i
|
|
|
|
// Operations using C99 operators
|
|
z3 = z1 + z2 // addition
|
|
z4 = z1 - z2 // subtraction
|
|
z5 = z1 * z2 // multiplication
|
|
z6 = z1 / z2 // division
|
|
//----------------
|
|
z7 = fn conj(z1) // conjugate
|
|
z8 = fn cpow(z1,z2) // power ^
|
|
z9 = fn cabs(z1) // norm | magnitude
|
|
zs = fn csqrt(z1) // square root
|
|
zn = -(z1) // negate
|
|
|
|
// Extract real/imag parts using creal/cimag
|
|
print @
|
|
print @" z1 + z2 = "; fn creal(z3); " + "; fn cimag(z3); "i"
|
|
print @" z1 - z2 = "; fn creal(z4); " + "; fn cimag(z4); "i"
|
|
print @" z1 * z2 = "; fn creal(z5); " + "; fn cimag(z5); "i"
|
|
print @" z1 / z2 = "; fn creal(z6); " + "; fn cimag(z6); "i"
|
|
print @" Conj z1 = "; fn creal(z7); " "; fn cimag(z7); "i"
|
|
print @" Abs z1 = "; fn creal(z9)
|
|
print @" z1 ^ z2 = ";
|
|
fn PrintComplexNumber(z8)
|
|
print @" cSqrt z1= ";
|
|
fn PrintComplexNumber(zs)
|
|
print @" -(z1) = ";
|
|
fn PrintComplexNumber(zn)
|
|
print @" z1 real = "; fn creal(z1)
|
|
print @" z1 imag = "; fn cimag(z1)
|
|
|
|
HandleEvents
|