September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,15 @@
# 0^0 => 1
# NOTE: jq converts very large integers to floats.
# This implementation uses reduce to avoid deep recursion
def power_int(n):
if n == 0 then 1
elif . == 0 then 0
elif n < 0 then 1/power_int(-n)
elif ((n | floor) == n) then
( (n % 2) | if . == 0 then 1 else -1 end ) as $sign
| if (. == -1) then $sign
elif . < 0 then (( -(.) | power_int(n) ) * $sign)
else . as $in | reduce range(1;n) as $i ($in; . * $in)
end
else error("This is a toy implementation that requires n be integral")
end ;

View file

@ -0,0 +1,13 @@
def demo(x;y):
x | [ power_int(y), (log*y|exp) ] ;
demo(2; 3),
demo(2; 64),
demo(1.1; 1024),
demo(1.1; -1024)
# Output:
[8, 7.999999999999998]
[18446744073709552000, 18446744073709525000]
[2.4328178969536854e+42, 2.4328178969536693e+42]
[4.1104597317052596e-43, 4.1104597317052874e-43]