Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add primitives for math constants #985

Closed
JukkaL opened this issue Mar 26, 2023 · 0 comments · Fixed by python/mypy#15324
Closed

Add primitives for math constants #985

JukkaL opened this issue Mar 26, 2023 · 0 comments · Fixed by python/mypy#15324

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 26, 2023

Add primitives for at least commonly used math constants, such as math.pi and math.e. For example, math.pi should be compiled into the float literal 3.141592653589793.

See also #984.

Here's a list of constants we may want to cover:

  • pi
  • e
  • tau
  • inf
  • nan
dosisod added a commit to dosisod/mypy that referenced this issue May 30, 2023
This PR inlines math literals such as `math.pi` and `math.e`. Previously
Using `math.pi` would emit a getattr, unbox, error check, then box op code,
but now it just boxes the value directly.

This PR is an improvement, but there are a few things that could be improved
further:

* `math` is still being imported even if you only imported literal values such
  as `pi` (which is now inlined and doesn't need to be imported).

* We are essentially boxing a literal which should be a `CPyLit_Float` value
  instead. I looked into this, but AFAICT there is no "float object" primitve,
  meaning Mypyc will emit the same amount of code as before (minus the getattr)
  which is not ideal. Is there any way to indicate in the IR that a
  `CPyLit_Float` value is a boxed float as opposed to an unboxed float
  (`double`)?

Also, I added a test to ensure that the math literals are inlined properly and
have the same value, but the test is failing saying that `pi` does not exist
on module `math`. There are no errors when running the same file through mypy,
so I don't know why it is failing. I checked, and the issue still seems to be
present on `master`.

Closes mypyc/mypyc#985
JukkaL pushed a commit to python/mypy that referenced this issue Jun 2, 2023
This PR inlines math literals such as `math.pi` and `math.e`. Previously
Using `math.pi` would emit a getattr, unbox, error check, then box op
code (if we need a boxed value), but now it just generates a literal directly
(that is sometimes boxed).

Closes mypyc/mypyc#985
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant