-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
bignum/bigreal number representation needed #218
Comments
I've started on implementing delayed parsing of numbers so as to preserve their original form wherever possible (i.e., whenever the actual number isn't needed as a double in the jq program). It turns out that this will require a lot of work :( Even then we'll need a bignum library to support bignum math in jq. |
I'm not entirely sure it's wrong to do so. What format would you prefer? |
In term of math it is not wrong to write 1e+07 instead of 10000000. But in terms of software it makes a big difference: Consider a Unix pipe (my usecase) like % some program | jq 'a cool filter' | some other program B Now jq's output is fed this to program B, which can deal with int64 or even int128, but not with floats, because in the original data there are no floats. When jq makes a conversion like above, program B bails out. See: #143 (comment) To answer your question: I would prefer if jq would not change the representation of a number, if this number is just moved from jq input to jq output. (like "sort -g", it does interpret the input numbers but still outputs the original lines.) |
@tischwa +1 |
It'd be a lot easier to provide options for how numbers are formatted on output than to preserve input form (when not touched by arithmetic). |
@nicowilliams I already did this, in my fork: https://github.com/airfrog/jq |
Wouldn't it be possible to keep for each parsed number not only the numeric value, but also the input string? I remember this #143 (comment) where airfrog seemed to have something similar working. (Ahh, I just saw he sent a pull request.) I think in terms of universal usability jq would gain a lot, if it would follow the typical philosophy of the classical Unix filter-like programs, which only modify the input, if they have to. Examles: % cat num.txt % jq '.' num.txt % awk '{print $1, 1*$1}' num.txt % sort -n num.txt % sort -g num.txt So usually the input is fed through, only if awk has to do the computation 1*$1 it switches internally to a numeric representation, the plain $1 is printed exactly as given in the input. Also sort -n/-g has to interpret the lines numerically but still gives the original input as output. |
+1 |
jq does have David M. Gay's bigint code in OTOH, jq maybe doesn't need bignum operations, just a bignum representation falling back to doubles for arithmetic (and comparison?). But I'd prefer to only fallback to doubles for libm functions for which we find no better alternative. |
@nicowilliams wrote:
bignum operations for the medium or long term; bignum representation for the short term (or tomorrow :-) |
Well, it's early days and there's still research to be done. http://www.eskimo.com/~eresrch/float/ looks promising, though I've no idea what the license on it would be (I sent the author email about this). It's very complete, but a) it's fixed-precision (probably easy to change to be dynamic) and b) it doesn't handle normal string representation of numbers (probably also easy to fix). I haven't looked but I suspect it also doesn't do double2big and big2double conversion. |
The author of Big Float (http://www.eskimo.com/~eresrch/float/) has agreed to let us use it under friendly terms. I'll take a look at it and see how suitable it is. |
Would be really really nice to have some progress here. I've just been bitten by this bug and it's really hard to catch as the numbers jq spits out looks totally legid - i.e. within the same magnitude |
@kutzi (and anyone else interested in bignum support in jq) We have a PR (that I need to find time to finish) for 64-bit integer support (in addition to IEEE754 doubles). We're not likely to add any kind of bignums unless someone submits a PR. If you or anyone else wants to work on bignum support for jq, you'll need to be aware of a couple of things:
I'd start by adding a compile-time option to use allocated numbers so that numeric Next I'd look for a suitable bignum library. There are quite a number of them, but they'd have to be a) C-coded or otherwise have a C API, b) licensed in a way that's friendly to jq's license and jq's users. Lastly, I'd integrate such a bignum library much like Oniguruma: as a [git] submodule that is used if |
Any updates? |
Would this be the right issue to ask about having jq preserve zeros after the decimal point as well, e.g. not converting As a concrete example Java's System.out.println(new BigDecimal("500"));
System.out.println(new BigDecimal("500.0"));
System.out.println(new BigDecimal("5e2"));
System.out.println();
System.out.println(new BigDecimal(BigInteger.valueOf(500), 0));
System.out.println(new BigDecimal(BigInteger.valueOf(5000), 1));
System.out.println(new BigDecimal(BigInteger.valueOf(5), -2)); =>
|
jq 1.7 released with support for literal large numbers. closing |
Awesome, thank you! |
There are cases that jq converts extra large numbers to ones with scientific (exponent) notation.
The text was updated successfully, but these errors were encountered: