irbuild-float.test 9.1 KB

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