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

f64 only mode #452

Closed
Kezii opened this issue Sep 23, 2021 · 5 comments
Closed

f64 only mode #452

Kezii opened this issue Sep 23, 2021 · 5 comments

Comments

@Kezii
Copy link

Kezii commented Sep 23, 2021

I'm using rhai as a glorified calculator inside a somewhat popular telegram bot (#69 ), the problem is that the hard distinction between integer and float types that rhai has, is making the language hard to use

It would be cool to have an option where you can make 10 / 9 or sqrt(2) and obtain the correct floating point result instead of an 1 or an error
I suppose this can be solved by having an option to treat all literals as f64 and parsing them as that
Using f64 exclusively does not give immediate problems (or at all) as noted in the lua documentation https://www.lua.org/pil/2.3.html

@schungx
Copy link
Collaborator

schungx commented Sep 23, 2021

It is not as simple as just parsing all integer literals as f64 because a lot of built-in functions return integer values, such as len.

INT is just about the most basic type in Rhai; I'm not sure whether it is possible to do away with it.

It is possible, however, to overload the division operator as well as certain math functions. For example, we can always overload / to do floating point instead when being passed two integers. We can define sqrt(INT) and pass it to sqrt(f64).

The user, however, may be surprised that a division turns the number into floating-point.

@schungx
Copy link
Collaborator

schungx commented Sep 23, 2021

Alternatively, we can allow you to set a mapper function into the Engine, such that you can convert any Token from the tokenizer into something else. For example, you can register a closure that turns any Token::IntegerConstant into Token::FloatConstant and that should provide you with exactly what you ask for.

Internall, Rhai already has facilities to set such a mapper function; it is just not exposed publicly.

@Kezii
Copy link
Author

Kezii commented Sep 23, 2021

Thanks for the answer
Yeah just changing the parsing of the literals is enough, that's the bulk of the usability problem, casting the result of len() is less disruptive than remembering to put a . after every constant
I guess that the mapper function should work

@schungx
Copy link
Collaborator

schungx commented Sep 24, 2021

You can pull from https://github.com/rhaiscript/rhai and get the version with Engine::on_parse_token which allows you to remap tokens.

Remember to pull in the internals feature.

@Kezii
Copy link
Author

Kezii commented Sep 24, 2021

that works perfectly, thanks

@Kezii Kezii closed this as completed Sep 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants