Skip to content

Question: negation for f64 #32

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

Closed
wbthomason opened this issue Jul 17, 2023 · 3 comments · Fixed by egraphs-good/egglog#168 or #34
Closed

Question: negation for f64 #32

wbthomason opened this issue Jul 17, 2023 · 3 comments · Fixed by egraphs-good/egglog#168 or #34

Comments

@wbthomason
Copy link
Contributor

Thanks for the library! I've been trying to use egglog for some numerical code, and have run into the missing unary negation operator for f64. I've tried patching this in myself analogously to its implementation for Rational, but I only end up with an error No matching primitive for: (neg f64).

Is there a more fundamental reason that negation is unsupported for f64? Could you help me figure out how to add this? Thanks!

@saulshanabrook
Copy link
Member

saulshanabrook commented Jul 17, 2023

Hey @wbthomason thanks for giving this a spin!

I believe the issue is that the upstream egglog library does not support negation yet for f64. I doubt this is due to an intentional choice, but just that they hadn't needed it yet.

Once it is added to egglog, we can add it here.

It can be added to the f64.rs file and a test can be added to f64.egg.

@wbthomason
Copy link
Contributor Author

Got it, thanks! I'll take a look at making a quick PR. If, in the meanwhile, I wanted to make a custom "float-like" type with negation support, would this be as easy as making a separate class_ call on a class with __neg__ defined, or do I still need the upstream support?

@saulshanabrook
Copy link
Member

Got it, thanks! I'll take a look at making a quick PR. If, in the meanwhile, I wanted to make a custom "float-like" type with negation support, would this be as easy as making a separate class_ call on a class with neg defined, or do I still need the upstream support?

Yeah I believe that should work! Something like this (untested):

@egraph.class_
class MyFloat(BaseExpr):
    def __init__(self, f: f64Like) -> None: ...
    def __add__(self, other: MyFloat) -> MyFloat: ...
    def __neg__(self) -> MyFloat: ...

x, y = vars_("x y", f64)
egraph.register(
    replace(MyFloat(x) + MyFloat(y)).with_(MyFloat(x - y)),
    replace(-MyFloat(x)).with_(MyFloat(f64(0) - x)),
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants