Arithmetic/Complex: Difference between revisions

no edit summary
(added ruby)
No edit summary
Line 6:
 
=={{header|Ada}}==
<lang ada>with Ada.Numerics.Generic_Complex_Types;
with Ada.Text_IO.Complex_IO;
 
Line 52:
C := Conjugate (C);
end Complex_Operations;
</adalang>
 
=={{header|ALGOL 68}}==
Line 92:
{{works with|C99}}
The more recent [[C99]] standard has built-in complex number primitive types, which can be declared with float, double, or long double precision. To use these types and their associated library functions, you must include the <complex.h> header. (Note: this is a ''different'' header than the <complex> templates that are defined by [[C++]].) [http://www.opengroup.org/onlinepubs/009695399/basedefs/complex.h.html] [http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.vacpp7a.doc/language/ref/clrc03complex_types.htm]
<lang c>
#include <complex.h>
 
Line 121:
printf("\n-a="); cprint(c); printf("\n");
}
</clang>
 
{{works with|C89}}
User-defined type:
<lang c>typedef struct{
double real;
double imag;
Line 176:
printf("\n-a="); put(neg(a)); printf("\n");
}
</clang>
 
=={{header|C++}}==
<lang cpp>
#include <iostream>
#include <complex>
Line 197:
std::cout << -a << std::endl;
}
</cpplang>
 
=={{header|Common Lisp}}==
 
Complex numbers are a built-in numeric type in Common Lisp. The literal syntax for a complex number is <codett>#C(<var>real</var> <var>imaginary</var>)</codett>. The components of a complex number may be integers, ratios, or floating-point. Arithmetic operations automatically return complex (or real) numbers when appropriate:
 
> (sqrt -1)
Line 226:
#C(0 -1/2)
 
Complex numbers can be constructed from real and imaginary parts using the <codett>complex</codett> function, and taken apart using the <codett>realpart</codett> and <codett>imagpart</codett> functions.
 
> (complex 64 (/ 3 4))
Line 239:
=={{header|D}}==
Complex number is a D built-in type.
<lang d>auto x = 1F+1i ; // auto type to cfloat
auto y = 3.14159+1.2i ; // cdouble
creal z ;
Line 250:
z = 1.0 / x ; writefln(z) ; // => 0.5+-0.5i
// negation
z = -x ; writefln(z) ; // => -1+-1i</dlang>
 
=={{header|Forth}}==
Line 370:
 
=={{header|Java}}==
<lang java>public class Complex{
public final double real;
public final double imag;
Line 408:
System.out.println(a.mult(b));
}
}</javalang>
 
=={{header|Maple}}==
Line 433:
=={{header|OCaml}}==
The "Complex" module provides the functionality of complex numbers.
<lang ocaml>open Complex
 
let print_complex z =
Line 444:
print_complex (mul a b);
print_complex (inv a);
print_complex (neg a)</ocamllang>
 
=={{header|Pascal}}==
<lang pascal>
program showcomplex(output);
 
Line 516:
writeln
end.
</pascallang>
 
=={{header|Perl}}==
The Math::Complex module provides the functionality of complex numbers.
<lang perl>use Math::Complex;
 
$a = 1 + 1*i;
Line 528:
$c = $a * $b;
$c = 1 / $a;
$c = -$a;</perllang>
 
=={{header|Pop11}}==
Line 565:
=={{header|Python}}==
 
<lang python>a = 1 + 1j
b = 3.14159 + 1.25j
 
Line 571:
c = a * b
c = 1 / a
c = -a</pythonlang>
 
=={{header|Ruby}}==
 
<lang ruby>require 'complex'
 
a = Complex(1, 1)
Line 584:
c = a * b
c = 1.0 / a
c = -a</rubylang>
 
=={{header|Scheme}}==
<lang scheme>(define a (make-rectangular 1 1))
(define b (make-rectangular 3.14159 1.25))
 
Line 593:
(define c (* a b))
(define c (/ 1 a))
(define c (- a))</schemelang>