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

Hexadecimal Floating Point #1550

Closed
i-am-linja opened this issue Jan 27, 2025 · 5 comments
Closed

Hexadecimal Floating Point #1550

i-am-linja opened this issue Jan 27, 2025 · 5 comments

Comments

@i-am-linja
Copy link

There is a standard hexadecimal syntax for floating point numbers: 0x*.*p*, in which the hex numeral after the p represents a power of two by which to multiply the pointed hex numeral before it. I think it would be good if Janet had this syntax as well.

@sogaiu
Copy link
Contributor

sogaiu commented Jan 27, 2025

I think there is also an explanation here along with a specific example.

Increasing the exponent by 1 multiplies by 2, not 16: 20p0 = 10p1 = 8p2 = 4p3 = 2p4 = 1p5. Usually, the number is normalized so that the hexadecimal digits start with 1. (zero is usually 0 with no P).

Example: 1.3DEp42 represents 1.3DE(base 16) × 2^42(base 10).

@bakpakin
Copy link
Member

bakpakin commented Jan 30, 2025

We do already support something similar with the & character. For example, 0xAB.CD&5 is equivalent to 0xAB.CD * 16^5, or equivalently 0xABCD * 16^3.

Would be fairly easy to special case the 0x prefix to handle a different exponentiation, but it's not strictly needed. Also where is this standard syntax coming from, is it just C++ or something similar? IEEE?

@sogaiu
Copy link
Contributor

sogaiu commented Jan 30, 2025

The following is from Wikipedia:

Just as decimal numbers can be represented in exponential notation, so too can hexadecimal numbers. P notation uses the letter P (or p, for "power"), whereas E (or e) serves a similar purpose in decimal E notation.

...

P notation is required by the IEEE 754-2008 binary floating-point standard and can be used for floating-point literals in the C99 edition of the C programming language.

via the first and last paragraphs of the aforementioned section.

There are some footnotes at the original article that I did not reproduce here, I thought it might be better to see them in context.

bakpakin added a commit that referenced this issue Jan 30, 2025
@bakpakin
Copy link
Member

Thanks for the reference, I was able to get a quick implementation working. I'm pretty confident it's fine, but I will add some more testing at some point before merging.

@sogaiu
Copy link
Contributor

sogaiu commented Feb 3, 2025

On a related note, I verified a variety of values at janet's repl and recorded them as tests for a tree-sitter grammar here.

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

No branches or pull requests

3 participants