Arithmetic/Complex: Difference between revisions
Content added Content deleted
(adding lambdalk) |
(→{{header|Smalltalk}}: more examples involving fractions) |
||
Line 4,490: | Line 4,490: | ||
a imaginary displayNl. |
a imaginary displayNl. |
||
a negated displayNl.</lang> |
a negated displayNl.</lang> |
||
{{works with|Smalltalk/X}} |
|||
Complex is already in the basic class library. Multiples of imaginary are created by sending an "i" message to a number, which can be added to another number. Thus 5i => (0+5i), 1+(1/3)I => (1+1/3i) and (1.0+2i) => (1.0+2i). Notice that the read and image components can be arbitrary integers, fractions or floating point numbers. And the results will be exact (i.e. fractions) if possible. |
|||
<lang smalltalk> |
|||
|a b| |
|||
a := 1 + 1i. |
|||
b := 3.14159 + 1.2i. |
|||
Transcript show:'a => '; showCR:a. |
|||
Transcript show:'b => '; showCR:b. |
|||
Transcript show:'a+b => '; showCR:(a + b). |
|||
Transcript show:'a-b => '; showCR:(a - b). |
|||
Transcript show:'a*b => '; showCR:(a * b). |
|||
Transcript show:'a/b => '; showCR:(a / b). |
|||
Transcript show:'a reciprocal => '; showCR:a reciprocal. |
|||
Transcript show:'a conjugated => '; showCR:a conjugated. |
|||
Transcript show:'a abs => '; showCR:a abs. |
|||
Transcript show:'a real => '; showCR:a real. |
|||
Transcript show:'a imaginary => '; showCR:a imaginary. |
|||
Transcript show:'a negated => '; showCR:a negated. |
|||
Transcript show:'a sqrt => '; showCR:a sqrt.</lang> |
|||
a2 := (1/2) + 1i. |
|||
b2 := (2/3) + 2i. |
|||
Transcript show:'a2+b2 => '; showCR:(a2 + b2). |
|||
Transcript show:'a2-b2 => '; showCR:(a2 - b2). |
|||
Transcript show:'a2*b2 => '; showCR:(a2 * b2). |
|||
Transcript show:'a2/b2 => '; showCR:(a2 / b2). |
|||
Transcript show:'a2 reciprocal => '; showCR:a2 reciprocal. |
|||
{{out}} |
|||
<pre>a => (1+1i) |
|||
b => (3.14159+1.2i) |
|||
a+b => (4.14159+2.2i) |
|||
a-b => (-2.14159-0.2i) |
|||
a*b => (1.94159+4.34159i) |
|||
a/b => (0.383885788269082+0.171676461306887i) |
|||
a reciprocal => ((1/2)-(1/2)i) |
|||
a conjugated => (1-1i) |
|||
a abs => 1.4142135623731 |
|||
a real => 1 |
|||
a imaginary => 1 |
|||
a negated => (-1-1i) |
|||
a sqrt => (1.09868411346781+0.455089860562227i)</pre> |
|||
a2+b2 => ((7/6)+3i) |
|||
a2-b2 => ((-1/6)-1i) |
|||
a2*b2 => ((-5/3)+(5/3)i) |
|||
a2/b2 => ((21/40)-(3/40)i) |
|||
a2 reciprocal => ((2/5)-(4/5)i) |
|||
=={{header|smart BASIC}}== |
=={{header|smart BASIC}}== |