-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Binary, octal, and hexadecimal literals and formatting #1968
Binary, octal, and hexadecimal literals and formatting #1968
Conversation
Looks really good and straightforward at first sight Colin! Thanks a lot 👍 I'll do some more indepth review soon. Maybe a first feedback: I think we should write a section about these new features in the docs, on the expression parser syntax page and/or maybe also on the numbers page, not sure. |
Thanks! I am working on some more improvements to this that I will push soon. |
In the last commit I modified the way the literals are parsed and formatted. The literals are parsed as 32 bit 2s complement signed integers. This change causes the following comparisons to be true:
This is contrary to the way these literals are treated in plain JS where they would be treated as unsigned integers, i.e.:
I believe it makes more sense to treat binary/octal/hex literals as signed integers because that's what the bitwise operators in mathjs and plain JS operate on. Similarly the format functions This commit also adds checking that a literal doesn't exceed the size of a 32 bit 2s complement signed integer in the parser and checks that the value to be formatted is an integer and is within the size of such integers. For example, I am not sure if it makes sense for this addition to behave this way or not. Please let me know what you think @josdejong. |
I did some more thorough testing, and this really works like a charm. Nice job Christopher! Some feedbacks:
|
It's Colin, but thanks!
Me too.
Thanks!
I will get on this as soon as I have a chance.
I do think the number of bits should be configurable. I like the idea of using BigNumbers behind the scenes, but I understand that would be quite a larger undertaking (maybe?). I had an idea that if you use a literal, like I think ideally (and I'm pretty sure you mentioned this elsewhere) there would be separate types ( |
Sorry for mixing up your name Colin :( Ok let's think through making number of bits configurable. It may be quite easy to implement it as BigNumbers, we'll have to see. I was also thinking: JavaScript does not have types like |
Maybe we should support both |
I was thinking the types |
Ah, I get what you mean. So we could make new custom data types for it like |
I added a small section in the syntax documentation. |
Thanks for adding docs, looks good. I think this PR is ready to go, unless we want to directly implement support for configurable number of bits and integration with an other type like BigNumber or BigInt or anything. I think this first step will not conflict with those extensions so I expect we can do that safely as a separate, second iteration. What do you think? |
I think this is a good first step too. I agree the other improvements can come later as a second step. |
👍 merging now. Thanks again! |
Thanks! |
Published now in |
Adds support for binary, octal, and hexadecimal literals.