| 1 |
[case testFloatAdd] |
| 2 |
def f(x: float, y: float) -> float: |
| 3 |
return x + y |
| 4 |
def g(x: float) -> float: |
| 5 |
z = x - 1.5 |
| 6 |
return 2.5 * z |
| 7 |
[out] |
| 8 |
def f(x, y): |
| 9 |
x, y, r0 :: float |
| 10 |
L0: |
| 11 |
r0 = x + y |
| 12 |
return r0 |
| 13 |
def g(x): |
| 14 |
x, r0, z, r1 :: float |
| 15 |
L0: |
| 16 |
r0 = x - 1.5 |
| 17 |
z = r0 |
| 18 |
r1 = 2.5 * z |
| 19 |
return r1 |
| 20 |
|
| 21 |
[case testFloatBoxAndUnbox] |
| 22 |
from typing import Any |
| 23 |
def f(x: float) -> object: |
| 24 |
return x |
| 25 |
def g(x: Any) -> float: |
| 26 |
return x |
| 27 |
[out] |
| 28 |
def f(x): |
| 29 |
x :: float |
| 30 |
r0 :: object |
| 31 |
L0: |
| 32 |
r0 = box(float, x) |
| 33 |
return r0 |
| 34 |
def g(x): |
| 35 |
x :: object |
| 36 |
r0 :: float |
| 37 |
L0: |
| 38 |
r0 = unbox(float, x) |
| 39 |
return r0 |
| 40 |
|
| 41 |
[case testFloatNegAndPos] |
| 42 |
def f(x: float) -> float: |
| 43 |
y = +x * -0.5 |
| 44 |
return -y |
| 45 |
[out] |
| 46 |
def f(x): |
| 47 |
x, r0, y, r1 :: float |
| 48 |
L0: |
| 49 |
r0 = x * -0.5 |
| 50 |
y = r0 |
| 51 |
r1 = -y |
| 52 |
return r1 |
| 53 |
|
| 54 |
[case testFloatCoerceFromInt] |
| 55 |
def from_int(x: int) -> float: |
| 56 |
return x |
| 57 |
|
| 58 |
def from_literal() -> float: |
| 59 |
return 5 |
| 60 |
|
| 61 |
def from_literal_neg() -> float: |
| 62 |
return -2 |
| 63 |
[out] |
| 64 |
def from_int(x): |
| 65 |
x :: int |
| 66 |
r0 :: float |
| 67 |
L0: |
| 68 |
r0 = CPyFloat_FromTagged(x) |
| 69 |
return r0 |
| 70 |
def from_literal(): |
| 71 |
L0: |
| 72 |
return 5.0 |
| 73 |
def from_literal_neg(): |
| 74 |
L0: |
| 75 |
return -2.0 |
| 76 |
|
| 77 |
[case testConvertBetweenFloatAndInt] |
| 78 |
def to_int(x: float) -> int: |
| 79 |
return int(x) |
| 80 |
def from_int(x: int) -> float: |
| 81 |
return float(x) |
| 82 |
[out] |
| 83 |
def to_int(x): |
| 84 |
x :: float |
| 85 |
r0 :: int |
| 86 |
L0: |
| 87 |
r0 = CPyTagged_FromFloat(x) |
| 88 |
return r0 |
| 89 |
def from_int(x): |
| 90 |
x :: int |
| 91 |
r0 :: float |
| 92 |
L0: |
| 93 |
r0 = CPyFloat_FromTagged(x) |
| 94 |
return r0 |
| 95 |
|
| 96 |
[case testFloatOperatorAssignment] |
| 97 |
def f(x: float, y: float) -> float: |
| 98 |
x += y |
| 99 |
x -= 5.0 |
| 100 |
return x |
| 101 |
[out] |
| 102 |
def f(x, y): |
| 103 |
x, y, r0, r1 :: float |
| 104 |
L0: |
| 105 |
r0 = x + y |
| 106 |
x = r0 |
| 107 |
r1 = x - 5.0 |
| 108 |
x = r1 |
| 109 |
return x |
| 110 |
|
| 111 |
[case testFloatOperatorAssignmentWithInt] |
| 112 |
def f(x: float, y: int) -> None: |
| 113 |
x += y |
| 114 |
x -= 5 |
| 115 |
[out] |
| 116 |
def f(x, y): |
| 117 |
x :: float |
| 118 |
y :: int |
| 119 |
r0, r1, r2 :: float |
| 120 |
L0: |
| 121 |
r0 = CPyFloat_FromTagged(y) |
| 122 |
r1 = x + r0 |
| 123 |
x = r1 |
| 124 |
r2 = x - 5.0 |
| 125 |
x = r2 |
| 126 |
return 1 |
| 127 |
|
| 128 |
[case testFloatComparison] |
| 129 |
def lt(x: float, y: float) -> bool: |
| 130 |
return x < y |
| 131 |
def eq(x: float, y: float) -> bool: |
| 132 |
return x == y |
| 133 |
[out] |
| 134 |
def lt(x, y): |
| 135 |
x, y :: float |
| 136 |
r0 :: bit |
| 137 |
L0: |
| 138 |
r0 = x < y |
| 139 |
return r0 |
| 140 |
def eq(x, y): |
| 141 |
x, y :: float |
| 142 |
r0 :: bit |
| 143 |
L0: |
| 144 |
r0 = x == y |
| 145 |
return r0 |
| 146 |
|
| 147 |
[case testFloatOpWithLiteralInt] |
| 148 |
def f(x: float) -> None: |
| 149 |
y = x * 2 |
| 150 |
z = 1 - y |
| 151 |
b = z < 3 |
| 152 |
c = 0 == z |
| 153 |
[out] |
| 154 |
def f(x): |
| 155 |
x, r0, y, r1, z :: float |
| 156 |
r2 :: bit |
| 157 |
b :: bool |
| 158 |
r3 :: bit |
| 159 |
c :: bool |
| 160 |
L0: |
| 161 |
r0 = x * 2.0 |
| 162 |
y = r0 |
| 163 |
r1 = 1.0 - y |
| 164 |
z = r1 |
| 165 |
r2 = z < 3.0 |
| 166 |
b = r2 |
| 167 |
r3 = 0.0 == z |
| 168 |
c = r3 |
| 169 |
return 1 |
| 170 |
|
| 171 |
[case testFloatCallFunctionWithLiteralInt] |
| 172 |
def f(x: float) -> None: pass |
| 173 |
|
| 174 |
def g() -> None: |
| 175 |
f(3) |
| 176 |
f(-2) |
| 177 |
[out] |
| 178 |
def f(x): |
| 179 |
x :: float |
| 180 |
L0: |
| 181 |
return 1 |
| 182 |
def g(): |
| 183 |
r0, r1 :: None |
| 184 |
L0: |
| 185 |
r0 = f(3.0) |
| 186 |
r1 = f(-2.0) |
| 187 |
return 1 |
| 188 |
|
| 189 |
[case testFloatAsBool] |
| 190 |
def f(x: float) -> int: |
| 191 |
if x: |
| 192 |
return 2 |
| 193 |
else: |
| 194 |
return 5 |
| 195 |
[out] |
| 196 |
def f(x): |
| 197 |
x :: float |
| 198 |
r0 :: bit |
| 199 |
L0: |
| 200 |
r0 = x != 0.0 |
| 201 |
if r0 goto L1 else goto L2 :: bool |
| 202 |
L1: |
| 203 |
return 4 |
| 204 |
L2: |
| 205 |
return 10 |
| 206 |
L3: |
| 207 |
unreachable |
| 208 |
|
| 209 |
[case testCallSqrtViaMathModule] |
| 210 |
import math |
| 211 |
|
| 212 |
def f(x: float) -> float: |
| 213 |
return math.sqrt(x) |
| 214 |
[out] |
| 215 |
def f(x): |
| 216 |
x, r0 :: float |
| 217 |
L0: |
| 218 |
r0 = CPyFloat_Sqrt(x) |
| 219 |
return r0 |
| 220 |
|
| 221 |
[case testFloatFinalConstant] |
| 222 |
from typing import Final |
| 223 |
|
| 224 |
X: Final = 123.0 |
| 225 |
Y: Final = -1.0 |
| 226 |
|
| 227 |
def f() -> float: |
| 228 |
a = X |
| 229 |
return a + Y |
| 230 |
[out] |
| 231 |
def f(): |
| 232 |
a, r0 :: float |
| 233 |
L0: |
| 234 |
a = 123.0 |
| 235 |
r0 = a + -1.0 |
| 236 |
return r0 |
| 237 |
|
| 238 |
[case testFloatDefaultArg] |
| 239 |
def f(x: float = 1.5) -> float: |
| 240 |
return x |
| 241 |
[out] |
| 242 |
def f(x, __bitmap): |
| 243 |
x :: float |
| 244 |
__bitmap, r0 :: u32 |
| 245 |
r1 :: bit |
| 246 |
L0: |
| 247 |
r0 = __bitmap & 1 |
| 248 |
r1 = r0 == 0 |
| 249 |
if r1 goto L1 else goto L2 :: bool |
| 250 |
L1: |
| 251 |
x = 1.5 |
| 252 |
L2: |
| 253 |
return x |
| 254 |
|
| 255 |
[case testFloatMixedOperations] |
| 256 |
def f(x: float, y: int) -> None: |
| 257 |
if x < y: |
| 258 |
z = x + y |
| 259 |
x -= y |
| 260 |
z = y + z |
| 261 |
if y == x: |
| 262 |
x -= 1 |
| 263 |
[out] |
| 264 |
def f(x, y): |
| 265 |
x :: float |
| 266 |
y :: int |
| 267 |
r0 :: float |
| 268 |
r1 :: bit |
| 269 |
r2, r3, z, r4, r5, r6, r7, r8 :: float |
| 270 |
r9 :: bit |
| 271 |
r10 :: float |
| 272 |
L0: |
| 273 |
r0 = CPyFloat_FromTagged(y) |
| 274 |
r1 = x < r0 |
| 275 |
if r1 goto L1 else goto L2 :: bool |
| 276 |
L1: |
| 277 |
r2 = CPyFloat_FromTagged(y) |
| 278 |
r3 = x + r2 |
| 279 |
z = r3 |
| 280 |
r4 = CPyFloat_FromTagged(y) |
| 281 |
r5 = x - r4 |
| 282 |
x = r5 |
| 283 |
r6 = CPyFloat_FromTagged(y) |
| 284 |
r7 = r6 + z |
| 285 |
z = r7 |
| 286 |
L2: |
| 287 |
r8 = CPyFloat_FromTagged(y) |
| 288 |
r9 = r8 == x |
| 289 |
if r9 goto L3 else goto L4 :: bool |
| 290 |
L3: |
| 291 |
r10 = x - 1.0 |
| 292 |
x = r10 |
| 293 |
L4: |
| 294 |
return 1 |
| 295 |
|
| 296 |
[case testFloatDivideSimple] |
| 297 |
def f(x: float, y: float) -> float: |
| 298 |
z = x / y |
| 299 |
z = z / 2.0 |
| 300 |
return z / 3 |
| 301 |
[out] |
| 302 |
def f(x, y): |
| 303 |
x, y :: float |
| 304 |
r0 :: bit |
| 305 |
r1 :: bool |
| 306 |
r2, z, r3, r4 :: float |
| 307 |
L0: |
| 308 |
r0 = y == 0.0 |
| 309 |
if r0 goto L1 else goto L2 :: bool |
| 310 |
L1: |
| 311 |
r1 = raise ZeroDivisionError('float division by zero') |
| 312 |
unreachable |
| 313 |
L2: |
| 314 |
r2 = x / y |
| 315 |
z = r2 |
| 316 |
r3 = z / 2.0 |
| 317 |
z = r3 |
| 318 |
r4 = z / 3.0 |
| 319 |
return r4 |
| 320 |
|
| 321 |
[case testFloatDivideIntOperand] |
| 322 |
def f(n: int, m: int) -> float: |
| 323 |
return n / m |
| 324 |
[out] |
| 325 |
def f(n, m): |
| 326 |
n, m :: int |
| 327 |
r0 :: float |
| 328 |
L0: |
| 329 |
r0 = CPyTagged_TrueDivide(n, m) |
| 330 |
return r0 |
| 331 |
|
| 332 |
[case testFloatResultOfIntDivide] |
| 333 |
def f(f: float, n: int) -> float: |
| 334 |
x = f / n |
| 335 |
return n / x |
| 336 |
[out] |
| 337 |
def f(f, n): |
| 338 |
f :: float |
| 339 |
n :: int |
| 340 |
r0 :: float |
| 341 |
r1 :: bit |
| 342 |
r2 :: bool |
| 343 |
r3, x, r4 :: float |
| 344 |
r5 :: bit |
| 345 |
r6 :: bool |
| 346 |
r7 :: float |
| 347 |
L0: |
| 348 |
r0 = CPyFloat_FromTagged(n) |
| 349 |
r1 = r0 == 0.0 |
| 350 |
if r1 goto L1 else goto L2 :: bool |
| 351 |
L1: |
| 352 |
r2 = raise ZeroDivisionError('float division by zero') |
| 353 |
unreachable |
| 354 |
L2: |
| 355 |
r3 = f / r0 |
| 356 |
x = r3 |
| 357 |
r4 = CPyFloat_FromTagged(n) |
| 358 |
r5 = x == 0.0 |
| 359 |
if r5 goto L3 else goto L4 :: bool |
| 360 |
L3: |
| 361 |
r6 = raise ZeroDivisionError('float division by zero') |
| 362 |
unreachable |
| 363 |
L4: |
| 364 |
r7 = r4 / x |
| 365 |
return r7 |
| 366 |
|
| 367 |
[case testFloatExplicitConversions] |
| 368 |
def f(f: float, n: int) -> int: |
| 369 |
x = float(n) |
| 370 |
y = float(x) # no-op |
| 371 |
return int(y) |
| 372 |
[out] |
| 373 |
def f(f, n): |
| 374 |
f :: float |
| 375 |
n :: int |
| 376 |
r0, x, y :: float |
| 377 |
r1 :: int |
| 378 |
L0: |
| 379 |
r0 = CPyFloat_FromTagged(n) |
| 380 |
x = r0 |
| 381 |
y = x |
| 382 |
r1 = CPyTagged_FromFloat(y) |
| 383 |
return r1 |
| 384 |
|
| 385 |
[case testFloatModulo] |
| 386 |
def f(x: float, y: float) -> float: |
| 387 |
return x % y |
| 388 |
[out] |
| 389 |
def f(x, y): |
| 390 |
x, y :: float |
| 391 |
r0 :: bit |
| 392 |
r1 :: bool |
| 393 |
r2, r3 :: float |
| 394 |
r4, r5, r6, r7 :: bit |
| 395 |
r8, r9 :: float |
| 396 |
L0: |
| 397 |
r0 = y == 0.0 |
| 398 |
if r0 goto L1 else goto L2 :: bool |
| 399 |
L1: |
| 400 |
r1 = raise ZeroDivisionError('float modulo') |
| 401 |
unreachable |
| 402 |
L2: |
| 403 |
r2 = x % y |
| 404 |
r3 = r2 |
| 405 |
r4 = r3 == 0.0 |
| 406 |
if r4 goto L5 else goto L3 :: bool |
| 407 |
L3: |
| 408 |
r5 = x < 0.0 |
| 409 |
r6 = y < 0.0 |
| 410 |
r7 = r5 == r6 |
| 411 |
if r7 goto L6 else goto L4 :: bool |
| 412 |
L4: |
| 413 |
r8 = r3 + y |
| 414 |
r3 = r8 |
| 415 |
goto L6 |
| 416 |
L5: |
| 417 |
r9 = copysign(0.0, y) |
| 418 |
r3 = r9 |
| 419 |
L6: |
| 420 |
return r3 |
| 421 |
|
| 422 |
[case testFloatFloorDivide] |
| 423 |
def f(x: float, y: float) -> float: |
| 424 |
return x // y |
| 425 |
def g(x: float, y: int) -> float: |
| 426 |
return x // y |
| 427 |
[out] |
| 428 |
def f(x, y): |
| 429 |
x, y, r0 :: float |
| 430 |
L0: |
| 431 |
r0 = CPyFloat_FloorDivide(x, y) |
| 432 |
return r0 |
| 433 |
def g(x, y): |
| 434 |
x :: float |
| 435 |
y :: int |
| 436 |
r0, r1 :: float |
| 437 |
L0: |
| 438 |
r0 = CPyFloat_FromTagged(y) |
| 439 |
r1 = CPyFloat_FloorDivide(x, r0) |
| 440 |
return r1 |
| 441 |
|
| 442 |
[case testFloatNarrowToIntDisallowed] |
| 443 |
class C: |
| 444 |
x: float |
| 445 |
|
| 446 |
def narrow_local(x: float, n: int) -> int: |
| 447 |
x = n # E: Incompatible value representations in assignment (expression has type "int", variable has type "float") |
| 448 |
return x |
| 449 |
|
| 450 |
def narrow_tuple_lvalue(x: float, y: float, n: int) -> int: |
| 451 |
x, y = 1.0, n # E: Incompatible value representations in assignment (expression has type "int", variable has type "float") |
| 452 |
return y |
| 453 |
|
| 454 |
def narrow_multiple_lvalues(x: float, y: float, n: int) -> int: |
| 455 |
x = a = n # E: Incompatible value representations in assignment (expression has type "int", variable has type "float") |
| 456 |
a = y = n # E: Incompatible value representations in assignment (expression has type "int", variable has type "float") |
| 457 |
return x + y |
| 458 |
|
| 459 |
def narrow_attribute(c: C, n: int) -> int: |
| 460 |
c.x = n # E: Incompatible value representations in assignment (expression has type "int", variable has type "float") |
| 461 |
return c.x |
| 462 |
|
| 463 |
def narrow_using_int_literal(x: float) -> int: |
| 464 |
x = 1 # E: Incompatible value representations in assignment (expression has type "int", variable has type "float") |
| 465 |
return x |
| 466 |
|
| 467 |
def narrow_using_declaration(n: int) -> int: |
| 468 |
x: float |
| 469 |
x = n # E: Incompatible value representations in assignment (expression has type "int", variable has type "float") |
| 470 |
return x |
| 471 |
|
| 472 |
[case testFloatInitializeFromInt] |
| 473 |
def init(n: int) -> None: |
| 474 |
# These are strictly speaking safe, since these don't narrow, but for consistency with |
| 475 |
# narrowing assignments, generate errors here |
| 476 |
x: float = n # E: Incompatible value representations in assignment (expression has type "int", variable has type "float") |
| 477 |
y: float = 5 # E: Incompatible value representations in assignment (expression has type "int", variable has type "float") |
| 478 |
|
| 479 |
[case testFloatCoerceTupleFromIntValues] |
| 480 |
from __future__ import annotations |
| 481 |
|
| 482 |
def f(x: int) -> None: |
| 483 |
t: tuple[float, float, float] = (x, 2.5, -7) |
| 484 |
[out] |
| 485 |
def f(x): |
| 486 |
x, r0 :: int |
| 487 |
r1 :: tuple[int, float, int] |
| 488 |
r2 :: int |
| 489 |
r3 :: float |
| 490 |
r4, t :: tuple[float, float, float] |
| 491 |
L0: |
| 492 |
r0 = x |
| 493 |
r1 = (r0, 2.5, -14) |
| 494 |
r2 = r1[0] |
| 495 |
r3 = CPyFloat_FromTagged(r2) |
| 496 |
r4 = (r3, 2.5, -7.0) |
| 497 |
t = r4 |
| 498 |
return 1 |