Integer overflow: Difference between revisions
Content added Content deleted
Line 1,369: | Line 1,369: | ||
-9223372036854775808 |
-9223372036854775808 |
||
</pre> |
</pre> |
||
=={{header|jq}}== |
|||
The C-based implementation of jq uses IEEE 754 64-bit numbers for numeric computations |
|||
without raising errors except for division by 0. |
|||
The Go-based implementation of jq, gojq, uses unbounded precision for integer computations |
|||
involving infix operators (+, -, %, /), but in the case of division, the result is only guaranteed to be |
|||
precise if the divisor is a factor of the dividend. Using gojq, the error raised by an attempt to divide |
|||
a number by 0 is catchable. |
|||
In the following section, a jq program that implements the task is presented. |
|||
The outputs produced by jq and by gojq |
|||
are then given. |
|||
''The task'' |
|||
<lang jq> |
|||
def compare: |
|||
if type == "string" then "\n\(.)\n" |
|||
else map(tostring) |
|||
| .[1] as $s |
|||
| .[0] |
|||
| if $s == . then . + ": agrees" |
|||
else $s + ": expression evaluates to " + . |
|||
end |
|||
end; |
|||
[ -(-2147483647-1),"2147483648"], |
|||
[2000000000 + 2000000000, "4000000000"], |
|||
[-2147483647 - 2147483647, "-4294967294"], |
|||
[46341 * 46341, "2147488281"], |
|||
[(-2147483647-1) / -1, "2147483648"], |
|||
"For 64-bit signed integers:", |
|||
[-(-9223372036854775807-1), "9223372036854775808"], |
|||
[5000000000000000000+5000000000000000000, "10000000000000000000"], |
|||
[-9223372036854775807 - 9223372036854775807, "-18446744073709551614"], |
|||
[3037000500 * 3037000500, "9223372037000250000"], |
|||
[(-9223372036854775807-1) / -1, "9223372036854775808"], |
|||
"For 32-bit unsigned integers:", |
|||
[-4294967295, "-4294967295"], |
|||
[3000000000 + 3000000000, "6000000000"], |
|||
[2147483647 - 4294967295, "-2147483648"], |
|||
[65537 * 65537, "4295098369"], |
|||
"For 64-bit unsigned integers:", |
|||
[-18446744073709551615, "-18446744073709551615"], |
|||
[10000000000000000000 + 10000000000000000000, "20000000000000000000"], |
|||
[9223372036854775807 - 18446744073709551615, "-9223372036854775808"], |
|||
[4294967296 * 4294967296, "18446744073709551616"] |
|||
| compare</lang> |
|||
===jq 1.6=== |
|||
<pre>2147483648: agrees |
|||
4000000000: agrees |
|||
-4294967294: agrees |
|||
2147488281: agrees |
|||
2147483648: agrees |
|||
For 64-bit signed integers: |
|||
9223372036854775808: expression evaluates to 9223372036854776000 |
|||
10000000000000000000: expression evaluates to 1e+19 |
|||
-18446744073709551614: expression evaluates to -18446744073709552000 |
|||
9223372037000250000: agrees |
|||
9223372036854775808: expression evaluates to 9223372036854776000 |
|||
For 32-bit unsigned integers: |
|||
-4294967295: agrees |
|||
6000000000: agrees |
|||
-2147483648: agrees |
|||
4295098369: agrees |
|||
For 64-bit unsigned integers: |
|||
-18446744073709551615: expression evaluates to -18446744073709552000 |
|||
20000000000000000000: expression evaluates to 2e+19 |
|||
-9223372036854775808: expression evaluates to -9223372036854776000 |
|||
18446744073709551616: expression evaluates to 18446744073709552000 |
|||
</pre> |
|||
===gojq=== |
|||
<pre> |
|||
gojq -nr -f rc-integer-overflow.jq |
|||
2147483648: agrees |
|||
4000000000: agrees |
|||
-4294967294: agrees |
|||
2147488281: agrees |
|||
2147483648: agrees |
|||
For 64-bit signed integers: |
|||
9223372036854775808: agrees |
|||
10000000000000000000: agrees |
|||
-18446744073709551614: agrees |
|||
9223372037000250000: agrees |
|||
9223372036854775808: agrees |
|||
For 32-bit unsigned integers: |
|||
-4294967295: agrees |
|||
6000000000: agrees |
|||
-2147483648: agrees |
|||
4295098369: agrees |
|||
For 64-bit unsigned integers: |
|||
-18446744073709551615: agrees |
|||
20000000000000000000: agrees |
|||
-9223372036854775808: agrees |
|||
18446744073709551616: agrees |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |