cython

Форк
0
/
builtin_float.py 
272 строки · 7.5 Кб
1
# mode: run
2
# tag: pure3.0
3

4
import cython
5

6

7
def empty_float():
8
    """
9
    >>> float()
10
    0.0
11
    >>> empty_float()
12
    0.0
13
    """
14
    x = float()
15
    return x
16

17

18
def float_conjugate():
19
    """
20
    >>> float_call_conjugate()
21
    1.5
22
    """
23
    x = 1.5 .conjugate()
24
    return x
25

26

27
def float_call_conjugate():
28
    """
29
    >>> float_call_conjugate()
30
    1.5
31
    """
32
    x = float(1.5).conjugate()
33
    return x
34

35

36
def from_int(i):
37
    """
38
    >>> from_int(0)
39
    0.0
40
    >>> from_int(1)
41
    1.0
42
    >>> from_int(-1)
43
    -1.0
44
    >>> from_int(99)
45
    99.0
46
    >>> from_int(-99)
47
    -99.0
48

49
    >>> for exp in (14, 15, 16, 30, 31, 32, 52, 53, 54, 60, 61, 62, 63, 64):
50
    ...     for sign in (1, 0, -1):
51
    ...         value = (sign or 1) * 2**exp + sign
52
    ...         float_value = from_int(value)
53
    ...         assert float_value == float(value), "expected %s2**%s+%s == %r, got %r, difference %r" % (
54
    ...             '-' if sign < 0 else '', exp, sign, float(value), float_value, float_value - float(value))
55
    """
56
    return float(i)
57

58

59
@cython.test_assert_path_exists(
60
    "//CoerceToPyTypeNode",
61
    "//CoerceToPyTypeNode//PythonCapiCallNode",
62
)
63
def from_bytes(s: bytes):
64
    """
65
    >>> from_bytes(b"123")
66
    123.0
67
    >>> from_bytes(b"123.25")
68
    123.25
69
    >>> from_bytes(b"98_5_6.2_1")
70
    9856.21
71
    >>> from_bytes(b"12_4_131_123123_1893798127398123_19238712_128937198237.8222113_519879812387")
72
    1.2413112312318938e+47
73
    >>> from_bytes(b"123E100")
74
    1.23e+102
75
    >>> from_bytes(b"12__._3")  # doctest: +ELLIPSIS
76
    Traceback (most recent call last):
77
    ValueError: ...12__._3...
78
    >>> from_bytes(b"_12.3")  # doctest: +ELLIPSIS
79
    Traceback (most recent call last):
80
    ValueError: ..._12.3...
81
    >>> from_bytes(b"12.3_")  # doctest: +ELLIPSIS
82
    Traceback (most recent call last):
83
    ValueError: ...12.3_...
84
    >>> from_bytes(b"na_n")  # doctest: +ELLIPSIS
85
    Traceback (most recent call last):
86
    ValueError: ...na_n...
87
    >>> from_bytes(b"_" * 10000)  # doctest: +ELLIPSIS
88
    Traceback (most recent call last):
89
    ValueError: ...____...
90
    >>> from_bytes(None)  # doctest: +ELLIPSIS
91
    Traceback (most recent call last):
92
    TypeError...
93
    """
94
    return float(s)
95

96

97
@cython.test_assert_path_exists(
98
    "//CoerceToPyTypeNode",
99
    "//CoerceToPyTypeNode//PythonCapiCallNode",
100
)
101
def from_bytes_literals():
102
    """
103
    >>> from_bytes_literals()
104
    (123.0, 123.23, 123.76, 1e+100)
105
    """
106
    return float(b"123"), float(b"123.23"), float(b"12_3.7_6"), float(b"1e100")
107

108

109
@cython.test_assert_path_exists(
110
    "//CoerceToPyTypeNode",
111
    "//CoerceToPyTypeNode//PythonCapiCallNode",
112
)
113
def from_bytearray(s: bytearray):
114
    """
115
    >>> from_bytearray(bytearray(b"123"))
116
    123.0
117
    >>> from_bytearray(bytearray(b"123.25"))
118
    123.25
119
    >>> from_bytearray(bytearray(b"98_5_6.2_1"))
120
    9856.21
121
    >>> from_bytearray(bytearray(b"12_4_131_123123_1893798127398123_19238712_128937198237.8222113_519879812387"))
122
    1.2413112312318938e+47
123
    >>> from_bytearray(bytearray(b"123E100"))
124
    1.23e+102
125
    >>> from_bytearray(bytearray(b"12__._3"))  # doctest: +ELLIPSIS
126
    Traceback (most recent call last):
127
    ValueError: ...12__._3...
128
    >>> from_bytearray(bytearray(b"_12.3"))  # doctest: +ELLIPSIS
129
    Traceback (most recent call last):
130
    ValueError: ..._12.3...
131
    >>> from_bytearray(bytearray(b"12.3_"))  # doctest: +ELLIPSIS
132
    Traceback (most recent call last):
133
    ValueError: ...12.3_...
134
    >>> from_bytearray(bytearray(b"in_f"))  # doctest: +ELLIPSIS
135
    Traceback (most recent call last):
136
    ValueError: ...in_f...
137
    >>> from_bytearray(None)  # doctest: +ELLIPSIS
138
    Traceback (most recent call last):
139
    TypeError...
140
    """
141
    return float(s)
142

143

144
@cython.test_assert_path_exists(
145
    "//CoerceToPyTypeNode",
146
    "//CoerceToPyTypeNode//PythonCapiCallNode",
147
)
148
def from_str(s: 'str'):
149
    """
150
    >>> from_str("123")
151
    123.0
152
    >>> from_str("123.25")
153
    123.25
154
    >>> from_str("3_21.2_5")
155
    321.25
156
    >>> from_str("12_4_131_123123_1893798127398123_19238712_128937198237.8222113_519879812387")
157
    1.2413112312318938e+47
158
    >>> from_str("123E100")
159
    1.23e+102
160
    >>> from_str("12__._3")  # doctest: +ELLIPSIS
161
    Traceback (most recent call last):
162
    ValueError: ...12__._3...
163
    >>> from_str("_12.3")  # doctest: +ELLIPSIS
164
    Traceback (most recent call last):
165
    ValueError: ..._12.3...
166
    >>> from_str("12.3_")  # doctest: +ELLIPSIS
167
    Traceback (most recent call last):
168
    ValueError: ...12.3_...
169
    >>> from_str("n_an")  # doctest: +ELLIPSIS
170
    Traceback (most recent call last):
171
    ValueError: ...n_an...
172
    >>> from_str(None)  # doctest: +ELLIPSIS
173
    Traceback (most recent call last):
174
    TypeError...
175
    """
176
    return float(s)
177

178

179
@cython.test_assert_path_exists(
180
    "//CoerceToPyTypeNode",
181
    "//CoerceToPyTypeNode//PythonCapiCallNode",
182
)
183
def from_str_literals():
184
    """
185
    >>> from_str_literals()
186
    (123.0, 123.23, 124.23, 1e+100)
187
    """
188
    return float("123"), float("123.23"), float("1_2_4.2_3"), float("1e100")
189

190

191
@cython.test_assert_path_exists(
192
    "//CoerceToPyTypeNode",
193
    "//CoerceToPyTypeNode//PythonCapiCallNode",
194
)
195
def from_unicode(s: 'unicode'):
196
    """
197
    >>> from_unicode(u"123")
198
    123.0
199
    >>> from_unicode(u"123.25")
200
    123.25
201
    >>> from_unicode(u"12_4.8_5")
202
    124.85
203
    >>> from_unicode(u"12_4_131_123123_1893798127398123_19238712_128937198237.8222113_519879812387")
204
    1.2413112312318938e+47
205
    >>> from_unicode(u"123E100")
206
    1.23e+102
207
    >>> from_unicode("೬")
208
    6.0
209
    >>> from_unicode(u"123.23\\N{PUNCTUATION SPACE}")
210
    123.23
211
    >>> from_unicode(u"\\N{PUNCTUATION SPACE} 123.23 \\N{PUNCTUATION SPACE}")
212
    123.23
213
    >>> from_unicode(u"\\N{PUNCTUATION SPACE} 12_3.2_3 \\N{PUNCTUATION SPACE}")
214
    123.23
215
    >>> from_unicode(u"\\N{PUNCTUATION SPACE} " * 25 + u"123.54 " + u"\\N{PUNCTUATION SPACE} " * 22)  # >= 40 chars
216
    123.54
217
    >>> from_unicode(u"\\N{PUNCTUATION SPACE} " * 25 + u"1_23.5_4 " + u"\\N{PUNCTUATION SPACE} " * 22)
218
    123.54
219
    >>> from_unicode(u"\\N{PUNCTUATION SPACE} " + u"123.54 " * 2 + u"\\N{PUNCTUATION SPACE}")  # doctest: +ELLIPSIS
220
    Traceback (most recent call last):
221
    ValueError: ...123.54 123.54...
222
    >>> from_unicode(u"\\N{PUNCTUATION SPACE} " * 25 + u"123.54 " * 2 + u"\\N{PUNCTUATION SPACE} " * 22)  # doctest: +ELLIPSIS
223
    Traceback (most recent call last):
224
    ValueError: ...123.54 123.54...
225
    >>> from_unicode(u"_12__._3")  # doctest: +ELLIPSIS
226
    Traceback (most recent call last):
227
    ValueError: ..._12__._3...
228
    >>> from_unicode(u"_12.3")  # doctest: +ELLIPSIS
229
    Traceback (most recent call last):
230
    ValueError: ..._12.3...
231
    >>> from_unicode(u"12.3_")  # doctest: +ELLIPSIS
232
    Traceback (most recent call last):
233
    ValueError: ...12.3_...
234
    >>> from_unicode(u"i_nf")  # doctest: +ELLIPSIS
235
    Traceback (most recent call last):
236
    ValueError: ...i_nf...
237
    >>> from_unicode(None)  # doctest: +ELLIPSIS
238
    Traceback (most recent call last):
239
    TypeError...
240
    """
241
    return float(s)
242

243

244
@cython.test_assert_path_exists(
245
    "//CoerceToPyTypeNode",
246
    "//CoerceToPyTypeNode//PythonCapiCallNode",
247
)
248
def from_unicode_literals():
249
    """
250
    >>> from_unicode_literals()
251
    (123.0, 123.23, 123.45, 1e+100, 123.23)
252
    """
253
    return float(u"123"), float(u"123.23"), float(u"12_3.4_5"), float(u"1e100"), float(u"123.23\N{PUNCTUATION SPACE}")
254

255

256
def catch_valueerror(val):
257
    """
258
    >>> catch_valueerror("foo")
259
    False
260
    >>> catch_valueerror(u"foo")
261
    False
262
    >>> catch_valueerror(b"foo")
263
    False
264
    >>> catch_valueerror(bytearray(b"foo"))
265
    False
266
    >>> catch_valueerror("-1")
267
    -1.0
268
    """
269
    try:
270
        return float(val)
271
    except ValueError:
272
        return False
273

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.