-
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
Added base/1 and unbase/1 #2241
base: master
Are you sure you want to change the base?
Conversation
In my experience it is more convenient to work with this representation of numbers – array of base digits – when order of digits is reversed. So the lowest order digit is the first and highest is the last in the array:
That way when implementing operations with multiple numbers same order digits match naturally, for example:
For the names what do you think about Lastly, we need to document these in the manual. |
And tests, too. |
@odnoletkov Sorry for the inactivity. These are all good suggestions, I'll be trying to fullfill them. |
@itchyny Not sure where the tests are though - I'm relatively new to jq's source code. |
I think there is a bug in frombase. -def frombase($a): reduce (.[] | reverse) as $i (0; . * $a + $i);
+def frombase($a): reduce (reverse | .[]) as $i (0; . * $a + $i); |
in -def tobase($a): [while(. > 0; (. / $a) | floor) % $a];
+def tobase($a): [while(. > 0; (. / $a) | floor) % $a]|if .==[] then [0] else . end; |
Base conversion functions get re-implemented quite frequently, thus they deserve their own built-ins.
base/1
takes an operandx
and converts the input to basex
. Example usage:unbase/1
is the opposite ofbase/1
: it takes an operandx
and converts the input from basex
. Example usage: