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

RFC: bit-float literal #9371

Open
simonbyrne opened this issue Dec 15, 2014 · 8 comments
Open

RFC: bit-float literal #9371

simonbyrne opened this issue Dec 15, 2014 · 8 comments
Assignees
Labels
design Design of APIs or of the language itself maths Mathematical functions parser Language parsing and surface syntax

Comments

@simonbyrne
Copy link
Contributor

I would like to propose a bit-float literal:

julia> 0b1.101p0
1.625

As the name suggests, it's similar to a hex float literal, except just binary digits are used (conveniently, we can keep p as the exponent symbol, as it is base 2 in both cases).

The purpose is somewhat distinct: whereas hex floats are mainly used as an interchange format to ensure no rounding error when converting to/from strings, bit float literals are more for human consumption, basically as a more understandable format than bits, to understand and explain what floats look like underneath (e.g. to answer questions like this).

The two main changes required:

  1. Modifying the parser: although I have a rough understanding of lisp, I have no experience with how the parser works, so any pointers on where to look/what to modify would be appreciated.
  2. @printf format: I was thinking %b (as it comes after %a), but other suggestions are welcome.

Thoughts?

@tkelman
Copy link
Contributor

tkelman commented Dec 15, 2014

From previous interaction with hex floats and deficient compilers, I think the parser is using C strtod for some? most? all? numeric literals. So this might be a little messy to patch in - I could easily be wrong though.

@simonbyrne
Copy link
Contributor Author

That was what I feared: I guess the two alternatives would be:

a. Use the "implicit macro" approach, similar to Int128 and BigInt
b. Try to interpret it in the parser itself. It is just binary (so should be significantly easier than a decimal conversion), but I guess could still be a lot of work.

@jakebolewski
Copy link
Member

You could experiment with adding support for this in JuliaParser. This would go in the lexing code and should be pretty straightforward. Just eval the resulting AST to check for correctness. My feeling is that it would not be terribly hard to support this.

@StefanKarpinski
Copy link
Member

I'm ok with adding this feature – largely because it doesn't erode the numeric literal juxtaposition syntax any further but also because it would probably be handy for seeing what's going on with floats, as simon said. So +1

@simonbyrne
Copy link
Contributor Author

Actually, using JuliaParser.jl doesn't actually require any changes at all, as it already gets passed to straight to Base.float64(::String).

@RauliRuohonen
Copy link

Awesome idea. I've actually wished this existed a few times in rather odd circumstances: doing manual binary search of all things. Imagine evaluating f(0b1.1p0) and getting a bad result, then f(0b1.11p0) and getting a good result, and going on to check f(0b1.101p0) which is at the midpoint of the interval and so on. Not all that common scenario, but happens when you don't have code for recognizing the two results and decide to judge them by sight.

BTW, would something break if the p0 could be omitted from 0b1.0p0 and 0x1.0p0? You can omit e0 from 1.0e0, and the added convenience is significant despite the difference being just two characters.

@ihnorton ihnorton added the needs decision A decision on this change is needed label May 8, 2015
@StefanKarpinski StefanKarpinski added parser Language parsing and surface syntax maths Mathematical functions design Design of APIs or of the language itself and removed needs decision A decision on this change is needed labels Sep 13, 2016
@StefanKarpinski StefanKarpinski added this to the 0.6.0 milestone Sep 13, 2016
@JeffBezanson
Copy link
Member

Since this is a new feature that doesn't seem to be under active development, moving to 1.0.

@JeffBezanson JeffBezanson modified the milestones: 1.0, 0.6.0 Jan 4, 2017
@StefanKarpinski
Copy link
Member

These should be implemented in string macros IMO.

@Keno Keno modified the milestones: 1.x, 1.0 Jul 20, 2017
@DilumAluthge DilumAluthge removed this from the 1.x milestone Mar 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Design of APIs or of the language itself maths Mathematical functions parser Language parsing and surface syntax
Projects
None yet
Development

No branches or pull requests

9 participants