-
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
jv: Add some support for 64 bit ints in a very conservative way #1246
base: master
Are you sure you want to change the base?
Conversation
This adds an extra int64_t field to jv, which is only written when parsing number literals (and only if they are integers within range), and only read when printing. Any operations done over those numbers will downgrade them to a double with 53 bit precision. This is intentional for the scope of this patch: $ echo 111111111111111111 | jq -c '[., 1*.]' [111111111111111111,111111111111111100] For ints between 53 and 64 bits, this matches the behavior of awk, as suggested in the bug tracker by tischwa: $ echo 111111111111111111 | awk '{print $1, 1*$1}' 111111111111111111 111111111111111104
double number; | ||
struct { | ||
double dbl; | ||
int64_t int64; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not desirable, unfortunately, as it increases the sizeof(jv)
.
What I would recommend instead is a new jv
kind flag that represents a signed 64-bit integer. Then add jv_int64()
(creates a jv
from an int64_t
), jv_int64_value()
(returns an int64_t
given a numeric jv
). For backwards compatibility I'd have jv_get_kind()
return JV_KIND_NUMBER
, and then add a jv_get_number_kind()
that reports the numeric kind. jv_number_value()
and jv_int64_value()
should both accept numbers of any kind, and do the appropriate conversion.
This would allow us to also add uint64_t
as well.
All arithmetic and math functions would continue to operate on double
s, though perhaps the arithmetic operations could eventually be made to operate on integers (falling back on double
s in the event of overflow/underflow).
Hi. Thanks for this contribution. I left a comment on |
omg you're alive |
heheh, yeah, I'm alive. Sorry for the absence :( I've been heads down in other things. |
I'm working on an alternative version of this where a numeric My main fear is that this will slow things down. I may add macros for disabling this feature. |
Why is it not acceptable to increase sizeof(jv) anyway? Is there some sort of API/ABI guarantee for other projects? |
There's no ABI constraint on the size of a |
Another thing is that having multiple number representations at once in any one |
I like a lot bitwise operations on integers, but I liked also XSLT in the past, and XSLT has only one kind of "numbers": let jq do the same? Speaking of XSLT, how about implementing translate? JJOR |
BTW, your submission is awesome. I'm running with it and modifying it to make the int value a part of the |
@fadado jq will only have one kind of number. The idea here is that when the input was an integer that fits in an |
This adds an extra int64_t field to jv, which is only written when
parsing number literals (and only if they are integers within range),
and only read when printing.
Any operations done over those numbers will downgrade them to a double
with 53 bit precision. This is intentional for the scope of this patch:
For ints between 53 and 64 bits, this matches the behavior of awk,
as suggested in the bug tracker by tischwa:
Fixes issue #369 (at least partially)
I tried to add tests but the stuff in jq.test seems to be parsed by jq itself, so they end up being comparisons between the double values, not the int64 ones.
This is an itch i've really needed to scratch for a long time. Using jq as a pretty printer for json, I almost started getting used to big numbers getting mangled. Not anymore!