1# mode: run
2# tag: division
3
4
5def int_by_float():
6    """
7    >>> int_by_float()
8    0.5
9    """
10    return 1 / 2.0
11
12
13def float_by_int():
14    """
15    >>> float_by_int()
16    2.0
17    """
18    return 2.0 / 1
19
20
21def float_by_float():
22    """
23    >>> float_by_float()
24    1.5
25    """
26    return 3.0 / 2.0
27
28
29def div_by_0(x):
30    """
31    >>> div_by_0(0)  # doctest: +ELLIPSIS
32    Traceback (most recent call last):
33    ZeroDivisionError: float division...
34    >>> div_by_0(0.0)  # doctest: +ELLIPSIS
35    Traceback (most recent call last):
36    ZeroDivisionError: float division...
37    >>> div_by_0(1)  # doctest: +ELLIPSIS
38    Traceback (most recent call last):
39    ZeroDivisionError: float division...
40    >>> div_by_0(1.0)  # doctest: +ELLIPSIS
41    Traceback (most recent call last):
42    ZeroDivisionError: float division...
43    >>> float('inf') / 0.0  # doctest: +ELLIPSIS
44    Traceback (most recent call last):
45    ZeroDivisionError: float division...
46    >>> div_by_0(float('inf'))  # doctest: +ELLIPSIS
47    Traceback (most recent call last):
48    ZeroDivisionError: float division...
49    >>> div_by_0(float('-inf'))  # doctest: +ELLIPSIS
50    Traceback (most recent call last):
51    ZeroDivisionError: float division...
52    >>> float('nan') / 0.0  # doctest: +ELLIPSIS
53    Traceback (most recent call last):
54    ZeroDivisionError: float division...
55    >>> div_by_0(float('nan'))  # doctest: +ELLIPSIS
56    Traceback (most recent call last):
57    ZeroDivisionError: float division...
58    """
59    return x / 0.0
60
61
62def div_1_by(x):
63    """
64    >>> div_1_by(1.0)
65    1.0
66    >>> div_1_by(2.0)
67    0.5
68    >>> div_1_by(0.5)
69    2.0
70    >>> 1.0 / float('inf')
71    0.0
72    >>> div_1_by(float('inf'))
73    0.0
74    >>> div_1_by(float('-inf'))
75    -0.0
76    >>> div_1_by(float('nan'))
77    nan
78    >>> div_1_by(0)  # doctest: +ELLIPSIS
79    Traceback (most recent call last):
80    ZeroDivisionError: float division...
81    >>> div_1_by(0.0)  # doctest: +ELLIPSIS
82    Traceback (most recent call last):
83    ZeroDivisionError: float division...
84    """
85    return 1.0 / x
86
87
88def div_by_2(x):
89    """
90    >>> div_by_2(1.0)
91    0.5
92    >>> float('inf') / 2.0
93    inf
94    >>> div_by_2(float('inf'))
95    inf
96    >>> div_by_2(float('-inf'))
97    -inf
98    >>> float('nan') / 2.0
99    nan
100    >>> div_by_2(float('nan'))
101    nan
102    """
103    return x / 2.0
104
105
106def div_by_neg_2(x):
107    """
108    >>> div_by_neg_2(1.0)
109    -0.5
110    >>> div_by_neg_2(-1.0)
111    0.5
112    >>> (-2**14) / (-2.0)
113    8192.0
114    >>> div_by_neg_2(-2**14)
115    8192.0
116    >>> (-2**52) / (-2.0)
117    2251799813685248.0
118    >>> div_by_neg_2(-2**52)
119    2251799813685248.0
120    >>> (-2**53-1) / (-2.0)
121    4503599627370496.0
122    >>> div_by_neg_2(-2**53-1)
123    4503599627370496.0
124    >>> float('inf') / -2.0
125    -inf
126    >>> div_by_neg_2(float('inf'))
127    -inf
128    >>> div_by_neg_2(float('-inf'))
129    inf
130    >>> float('nan') / -2.0
131    nan
132    >>> div_by_neg_2(float('nan'))
133    nan
134    """
135    return x / -2.0
136
137
138def div_neg_2_by(x):
139    """
140    >>> div_neg_2_by(1.0)
141    -2.0
142    >>> div_neg_2_by(-1)
143    2.0
144    >>> div_neg_2_by(-2.0)
145    1.0
146    >>> div_neg_2_by(-2)
147    1.0
148    >>> -2.0 / float('inf')
149    -0.0
150    >>> div_neg_2_by(float('inf'))
151    -0.0
152    >>> div_neg_2_by(float('-inf'))
153    0.0
154    >>> float('nan') / -2.0
155    nan
156    >>> div_neg_2_by(float('nan'))
157    nan
158    >>> div_neg_2_by(0)  # doctest: +ELLIPSIS
159    Traceback (most recent call last):
160    ZeroDivisionError: float division...
161    >>> div_neg_2_by(0.0)  # doctest: +ELLIPSIS
162    Traceback (most recent call last):
163    ZeroDivisionError: float division...
164    """
165    return (-2.0) / x
166
167
168def div_by_nan(x):
169    """
170    >>> 1.0 / float('nan')
171    nan
172    >>> div_by_nan(1.0)
173    nan
174    >>> float('nan') / float('nan')
175    nan
176    >>> div_by_nan(float('nan'))
177    nan
178    >>> float('inf') / float('nan')
179    nan
180    >>> div_by_nan(float('inf'))
181    nan
182    """
183    return x / float("nan")
184
185
186def div_nan_by(x):
187    """
188    >>> float('nan') / 1.0
189    nan
190    >>> div_nan_by(1.0)
191    nan
192    >>> float('nan') / float('nan')
193    nan
194    >>> div_nan_by(float('nan'))
195    nan
196    >>> div_nan_by(0)  # doctest: +ELLIPSIS
197    Traceback (most recent call last):
198    ZeroDivisionError: float division...
199    >>> div_nan_by(0.0)  # doctest: +ELLIPSIS
200    Traceback (most recent call last):
201    ZeroDivisionError: float division...
202    """
203    return float("nan") / x
204
205
206def div_by_inf(x):
207    """
208    >>> 1 / float('inf')
209    0.0
210    >>> div_by_inf(1)
211    0.0
212    >>> 1.0 / float('inf')
213    0.0
214    >>> div_by_inf(1.0)
215    0.0
216    >>> div_by_inf(float('inf'))
217    nan
218    """
219    return x / float("inf")
220
221
222def div_inf_by(x):
223    """
224    >>> float('inf') / 1.0
225    inf
226    >>> div_inf_by(1.0)
227    inf
228    >>> float('inf') / float('nan')
229    nan
230    >>> div_inf_by(float('nan'))
231    nan
232    >>> float('inf') / float('-inf')
233    nan
234    >>> div_inf_by(float('-inf'))
235    nan
236    >>> float("inf") / 0.0  # doctest: +ELLIPSIS
237    Traceback (most recent call last):
238    ZeroDivisionError: float division...
239    >>> div_inf_by(0)  # doctest: +ELLIPSIS
240    Traceback (most recent call last):
241    ZeroDivisionError: float division...
242    >>> div_inf_by(0.0)  # doctest: +ELLIPSIS
243    Traceback (most recent call last):
244    ZeroDivisionError: float division...
245    """
246    return float("inf") / x
247
248
249def div_neg_inf_by(x):
250    """
251    >>> float('-inf') / 1.0
252    -inf
253    >>> div_neg_inf_by(1.0)
254    -inf
255    >>> float('-inf') / -1.0
256    inf
257    >>> div_neg_inf_by(-1.0)
258    inf
259    >>> float("-inf") / 0.0  # doctest: +ELLIPSIS
260    Traceback (most recent call last):
261    ZeroDivisionError: float division...
262    >>> div_neg_inf_by(0)  # doctest: +ELLIPSIS
263    Traceback (most recent call last):
264    ZeroDivisionError: float division...
265    >>> div_neg_inf_by(0.0)  # doctest: +ELLIPSIS
266    Traceback (most recent call last):
267    ZeroDivisionError: float division...
268    """
269    return float("-inf") / x
270