Arithmetic/Complex: Difference between revisions

m (→‎{{header|Wren}}: Minor tidy)
(5 intermediate revisions by one other user not shown)
Line 2,495:
 
=={{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.
Note: see the [[Arithmetic/Complex#Unicon|Unicon]] section below for a Unicon-specific solution.
<syntaxhighlight lang="icon">procedure main()
 
Line 4,118:
Furthermore, the GPC defines a function <tt>conjugate</tt> so there is no need for writing such a custom function.
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|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
begin
var a := Cplx(1,2);
var b := Cplx(3,4);
Println(a + b);
Println(a - b);
Println(a * b);
Println(a / b);
Println(-a);
Println(1/a);
Println(a.Real,a.Imaginary);
Println(a.Conjugate);
Println(Abs(a));
Println(a ** b);
end.
</syntaxhighlight>
{{out}}
<pre>
(4 + i*6)
(-2 + i*-2)
(-5 + i*10)
(0.44 + i*0.08)
(-1 + i*-2)
(0.2 + i*-0.4)
1 2
(1 + i*-2)
2.23606797749979
(0.129009594074467 + i*0.0339240929051702)
</pre>
 
 
=={{header|Perl}}==
Line 5,196 ⟶ 5,228:
 
Note that the parentheses around ∠ notation are required. It has a related use in vectors: (1∠π/4) is a complex number, [1,∠π/4] is a vector in two dimensions in polar notation, and [(1∠π/4)] is a complex number in a vector.
 
=={{header|Unicon}}==
Takes advantage of Unicon's operator overloading extension and Unicon's Complex class. Negation is not supported by the Complex class.
 
<syntaxhighlight lang="unicon">import math
 
procedure main()
write("c1: ",(c1 := Complex(1.5,3)).toString())
write("c2: ",(c2 := Complex(1.5,1.5)).toString())
write("+: ",(c1+c2).toString())
write("-: ",(c1-c2).toString())
write("*: ",(c1*c2).toString())
write("/: ",(c1/c2).toString())
write("additive inverse: ",c1.addInverse().toString())
write("multiplicative inverse: ",c1.multInverse().toString())
write("conjugate of (4,-3i): ",Complex(4,-3).conjugate().toString())
end</syntaxhighlight>
 
{{out}}
<pre>c1: (1.5,3i)
c2: (1.5,1.5i)
+: (3.0,4.5i)
-: (0.0,1.5i)
*: (-2.25,6.75i)
/: (1.5,0.5i)
additive inverse: (-1.5,-3i)
multiplicative inverse: (0.1333333333333333,-0.2666666666666667i)
conjugate of (4,-3i): (4,3i)</pre>
 
=={{header|UNIX Shell}}==
205

edits