-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathcomplex_numbers.imag.m
97 lines (78 loc) · 2.81 KB
/
complex_numbers.imag.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 1997-1998,2001, 2004-2006 The University of Melbourne.
% Copyright (C) 2015, 2018, 2022 The Mercury team.
% This file is distributed under the terms specified in COPYING.LIB.
%-----------------------------------------------------------------------------%
%
% File: imag.m.
% Main author: fjh.
% Stability: medium.
%
% Imaginary numbers.
%
% There are several reasons for supporting a separate type for imaginary
% numbers rather than just treating them as a special case of complex numbers.
% It is sometimes more convenient, and can be slightly more efficient. But
% perhaps the most important reason is to get correct handling of infinity and
% not-a-number on platforms that support IEEE floating point.
%
% Note that the overloaded versions of the binary operators that provide
% mixed type arithmetic are defined in different modules.
%
% See also:
% float.m, imag_float.m, float_imag.m,
% complex.m, imag_complex.m, complex_imag.m.
%
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- module complex_numbers.imag.
:- interface.
:- import_module float.
%-----------------------------------------------------------------------------%
:- type imag
---> im(float).
:- func i = imag. % i = sqrt(-1)
:- func j = imag. % another name for `i'
%-----------------------------------------------------------------------------%
% Addition.
%
:- func imag + imag = imag.
:- mode in + in = uo is det.
% Subtraction.
%
:- func imag - imag = imag.
:- mode in - in = uo is det.
% Multiplication.
%
:- func imag * imag = float.
:- mode in * in = uo is det.
% Division.
%
:- func imag / imag = float.
:- mode in / in = uo is det.
% Unary plus.
%
:- func + imag = imag.
:- mode + in = uo is det.
% Unary minus.
%
:- func - imag = imag.
:- mode - in = uo is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
%-----------------------------------------------------------------------------%
i = im(1.0).
j = i.
%-----------------------------------------------------------------------------%
+im(X) = im(X + 0.0).
-im(X) = im(-X).
im(X) + im(Y) = im(X + Y).
im(X) - im(Y) = im(X - Y).
im(X) * im(Y) = 0.0 - X * Y.
im(X) / im(Y) = X / Y.
%-----------------------------------------------------------------------------%
:- end_module complex_numbers.imag.
%-----------------------------------------------------------------------------%