Arithmetic/Complex: Difference between revisions
→Pascal: no need to reinvent the wheel and define a custom record type since `complex` is already standardized in ISO 10206, besides there were multiple mistakes; move Free Pascal implementation under dedicated section →Free Pascal
(→{{header|jq}}: + multiply/0) |
(→Pascal: no need to reinvent the wheel and define a custom record type since `complex` is already standardized in ISO 10206, besides there were multiple mistakes; move Free Pascal implementation under dedicated section →Free Pascal) |
||
Line 1,924:
x* = 1-3j
</pre>
=={{header|Free Pascal}}==
FreePascal has a complex units. Example of usage:
<lang Pascal>Program ComplexDemo;
uses
ucomplex;
var
a, b, absum, abprod, aneg, ainv, acong: complex;
function complex(const re, im: real): ucomplex.complex; overload;
begin
complex.re := re;
complex.im := im;
end;
begin
a := complex(5, 3);
b := complex(0.5, 6.0);
absum := a + b;
writeln ('(5 + i3) + (0.5 + i6.0): ', absum.re:3:1, ' + i', absum.im:3:1);
abprod := a * b;
writeln ('(5 + i3) * (0.5 + i6.0): ', abprod.re:5:1, ' + i', abprod.im:4:1);
aneg := -a;
writeln ('-(5 + i3): ', aneg.re:3:1, ' + i', aneg.im:3:1);
ainv := 1.0 / a;
writeln ('1/(5 + i3): ', ainv.re:3:1, ' + i', ainv.im:3:1);
acong := cong(a);
writeln ('conj(5 + i3): ', acong.re:3:1, ' + i', acong.im:3:1);
end.
</lang>
=={{header|Frink}}==
Line 2,286 ⟶ 2,318:
Conjugate:1.0 :+ (-2.0)</pre>
=={{header|Icon}} and {{header|Unicon}}==
{{improve|Unicon|This could be better implemented as an object i n Unicon. Note, however, that Unicon doesn't allow for operator overloading at the current time.}}
Icon doesn't provide native support for complex numbers. Support is included in the IPL.
<lang Icon>procedure main()
Line 3,788 ⟶ 3,819:
=={{header|Pascal}}==
{{works with|Extended Pascal}}
The simple data type <tt>complex</tt> is part of Extended Pascal, ISO standard 10206.
<lang pascal>program complexDemo(output);
const
{ I experienced some hiccups with -1.0 using GPC (GNU Pascal Compiler) }
negativeOne = -1.0;
type
line = string(80);
{ as per task requirements wrap arithmetic operations into separate functions }
begin
sum := x + y
end;
function product(protected x, y: complex): complex;
product := x * y
end;
negative := -x
inverse := x ** negativeOne
end;
{ only this function is not covered by Extended Pascal, ISO 10206 }
function conjugation(protected x: complex): complex;
begin
conjugation := cmplx(re(x), im(x) * negativeOne)
{ --- test suite ------------------------------------------------------------- }
function asString(protected x: complex): line;
const
totalWidth = 5;
fractionDigits = 2;
var
result: line;
begin
writeStr(result, '(', re(x):totalWidth:fractionDigits, ', ',
im(x):totalWidth:fractionDigits, ')');
asString := result
end;
{ === MAIN =================================================================== }
var
x: complex;
{ for demonstration purposes: how to initialize complex variables }
y: complex value cmplx(1.0, 4.0);
z: complex value polar(exp(1.0), 3.14159265358979);
begin
writeLn(asString(x), ' + ', asString(y), ' = ', asString(sum(x, y)));
writeLn(asString(x), ' * ', asString(z), ' = ', asString(product(x, z)));
writeLn(' −', asString(z), ' = ', asString(negative(z)));
writeLn(' inverse(', asString(z), ') = ', asString(inverse(z)));
writeLn(' conjugation(', asString(y), ') = ', asString(conjugation(y)));
end.</lang>
{{out}}
<pre>(-3.00, 2.00) + ( 1.00, 4.00) = (-2.00, 6.00)
(-3.00, 2.00) * (-2.72, 0.00) = ( 8.15, -5.44)
−(-2.72, 0.00) = ( 2.72, -0.00)
inverse((-2.72, 0.00)) = (-0.37, -0.00)
conjugation(( 1.00, 4.00)) = ( 1.00, -4.00)</pre>
The PXSC, Pascal eXtensions for scientific computing, define a standard data type similar to [[#Free Pascal|Free Pascal’s]] <tt>ucomplex</tt> data type.
=={{header|Perl}}==
|