int64, uint64 : it's impossible to get reliable big integers from literal inputs
@sgougeon)
Reported by Samuel GOUGEON (%c_uint64.sci (1.72 KB, text/x-csrc)
%c_uint64.sci
BUG DESCRIPTION:
----------------
There is presently no reliable and easy way to get a big integer from a literal input like with
--> uint64(9000000000000001000)
ans =
9000000000000001024
--> uint64(9000000000000001000) - uint64(9000000000000000000) - 1000
ans =
24
I know that this comes from the fact that the literal 9000000000000001000 is by default parsed as the
decimal number 9000000000000001000., and since 1/9000000000000001000 < %eps, there is a round-off error,
after what the "truncated" decimal number is converted into an uint64.
This limitation presently prevents working with big integers built by hand. Only big integers built with
an algorithm can be considered. This is rather frustrating.
A solution would be to make int64() and uint64() able to process input numbers as strings,
as for instance hex2dec() and hex2bin() do it. Then we could use for instance
--> uint64("9000000000000001000") + [ 1 -1001 ; 4 7]
ans =
9000000000000001001 8999999999999999999
9000000000000001004 9000000000000001007
This would be easily done by some overloads, provided that int64() and uint64() are opened to overloading:
--> uint64("9000000000000001000")
uint64: Wrong type for input argument #1: integer, boolean or double expected.
--> int64("9000000000000001000")
int64: Wrong type for input argument #1: integer, boolean or double expected.
A %c_uint64() overload is proposed in attachment. We it we get the expected result as in the example above.
Another example:
--> %c_uint64(cat(3,"7000000000000001000", "8000000000000001000", "9000000000000001000"))
ans =
(:,:,1)
7000000000000001000
(:,:,2)
8000000000000001000
(:,:,3)
9000000000000001000
%c_int64() could be implemented as well, to take care of signed inputs.
ERROR LOG:
----------
See above: wrong results. No possible overloading.
HOW TO REPRODUCE THE BUG:
-------------------------
uint64(9000000000000001000) - uint64(9000000000000000000) - 1000
OTHER INFORMATION:
------------------
Discussion started on https://www.mail-archive.com/dev@lists.scilab.org/msg01093.html
Edited by Samuel GOUGEON