[latex3-commits] [git/LaTeX3-latex3-latex3] l3bitset: extend bitset_to_arabic to larger values with fp_eval (359d12f0b)
Ulrike Fischer
fischer at troubleshooting-tex.de
Sun Dec 20 20:50:51 CET 2020
Repository : https://github.com/latex3/latex3
On branch : l3bitset
Link : https://github.com/latex3/latex3/commit/359d12f0b3dad7d7aaf44aaac7d1741677ec44b6
>---------------------------------------------------------------
commit 359d12f0b3dad7d7aaf44aaac7d1741677ec44b6
Author: Ulrike Fischer <fischer at troubleshooting-tex.de>
Date: Sun Dec 20 20:50:51 2020 +0100
extend bitset_to_arabic to larger values with fp_eval
>---------------------------------------------------------------
359d12f0b3dad7d7aaf44aaac7d1741677ec44b6
l3kernel/l3bitset.dtx | 30 +++--
l3kernel/testfiles/m3bitset001.lvt | 15 ++-
l3kernel/testfiles/m3bitset001.tlg | 232 +++++++++++++++++++++++++++++++++----
3 files changed, 242 insertions(+), 35 deletions(-)
diff --git a/l3kernel/l3bitset.dtx b/l3kernel/l3bitset.dtx
index 46f43f0d4..6617c6b3c 100644
--- a/l3kernel/l3bitset.dtx
+++ b/l3kernel/l3bitset.dtx
@@ -145,8 +145,9 @@
% \end{syntax}
% This leaves the current value of the bitset expressed as
% a decimal number in the input stream. If no bit has been set yet,
-% the output is zero. The function uses \cs{int_from_bin:n}, so it
-% can only be used with a bitset whose value is smaller than $2^{31}-1$.
+% the output is zero. The function uses \cs{int_from_bin:n} if largest
+% index that have been set or unset is smaller then 32, and a slower implementation
+% based on \cs{fp_eval} otherwise.
% \end{function}
%
%
@@ -392,13 +393,24 @@
% \bitset_to_bin:N, \bitset_to_bin:c,
% }
% The naming of the commands follow the names in the \texttt{int} module.
-% \cs{bitset_to_arabic:N} uses \cs{int_from_bin:n} and so can only handle small
-% numbers. There is no test for the string length, so that long bitset with
-% many leading zeros work nevertheless.
+% \cs{bitset_to_arabic:N} uses \cs{int_from_bin:n} if the string is shorter
+% then 32 and the slower \cs{fp_eval} for larger bitsets.
+%
% \begin{macrocode}
\cs_new:Npn \bitset_to_arabic:N #1
{
- \exp_args:Ne \int_from_bin:n { #1 }
+ \int_compare:nNnTF { \str_count:N #1 } < {32}
+ { \exp_args:No \int_from_bin:n {#1} }
+ {
+ \exp_after:wN \@@_to_int:nN \exp_after:wN 0
+ #1 \q_recursion_tail \q_recursion_stop
+ }
+ }
+
+\cs_new:Npn \@@_to_int:nN #1#2
+ {
+ \quark_if_recursion_tail_stop_do:Nn #2 {#1}
+ \exp_args:Nf \@@_to_int:nN { \fp_eval:n { #1 * 2 + #2 } }
}
\cs_new:Npn \bitset_to_bin:N #1
@@ -446,10 +458,8 @@
#1 { LaTeX/kernel } { show-bitset }
{ \token_to_str:N #2 }
{ \bitset_to_bin:N #2 }
- { \int_compare:nNnTF { \str_count:N #2 } < {32}
- { \bitset_to_arabic:N #2 }
- {---bitset~is~to~long~to~show~it--- }
- } { }
+ { \bitset_to_arabic:N #2 }
+ { }
}
}
diff --git a/l3kernel/testfiles/m3bitset001.lvt b/l3kernel/testfiles/m3bitset001.lvt
index 2d1401cec..e2a195f58 100644
--- a/l3kernel/testfiles/m3bitset001.lvt
+++ b/l3kernel/testfiles/m3bitset001.lvt
@@ -234,5 +234,18 @@
\bitset_index:Nn \g_my_test_bitset {15}
}
-\END
+\OMIT
+\bitset_new:N \l_my_testb_bitset
+\TIMO
+\TEST{to~arabic}
+ {
+ \int_step_inline:nn {40}
+ {
+ \bitset_show:N \l_my_testb_bitset
+ \bitset_set_false:Nn \l_my_testb_bitset {#1-1}
+ \bitset_set_true:Nn \l_my_testb_bitset {#1}
+ }
+ }
+
+\END
diff --git a/l3kernel/testfiles/m3bitset001.tlg b/l3kernel/testfiles/m3bitset001.tlg
index b488433ce..9afb391c5 100644
--- a/l3kernel/testfiles/m3bitset001.tlg
+++ b/l3kernel/testfiles/m3bitset001.tlg
@@ -119,49 +119,29 @@ TEST 15: show index 8 global
============================================================
TEST 16: add index 8 with sum
============================================================
-! Arithmetic overflow.
-<recently read> \__int_eval_end:
-l. ... }
-I can't evaluate this expression,
-since the result is out of range.
10000000000000000000000000000000111010101
-469
+1099511628245
1
============================================================
============================================================
TEST 17: add index 8 with sum global
============================================================
-! Arithmetic overflow.
-<recently read> \__int_eval_end:
-l. ... }
-I can't evaluate this expression,
-since the result is out of range.
10000000000000000000000000000000111010101
-469
+1099511628245
1
============================================================
============================================================
TEST 18: add index 15 with max-function
============================================================
-! Arithmetic overflow.
-<recently read> \__int_eval_end:
-l. ... }
-I can't evaluate this expression,
-since the result is out of range.
10000000000000000000000000100000111010101
-469
+1099511644629
1
============================================================
============================================================
TEST 19: add index 15 with max-function
============================================================
-! Arithmetic overflow.
-<recently read> \__int_eval_end:
-l. ... }
-I can't evaluate this expression,
-since the result is out of range.
10000000000000000000000000100000111010101
-469
+1099511644629
1
============================================================
============================================================
@@ -192,3 +172,207 @@ TEST 23: remove index 15 with sum global
469
0
============================================================
+============================================================
+TEST 24: to arabic
+============================================================
+The bitset \l_my_testb_bitset has the representation:
+> binary: 0
+> arabic: 0.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 1
+> arabic: 1.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 10
+> arabic: 2.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 100
+> arabic: 4.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 1000
+> arabic: 8.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 10000
+> arabic: 16.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 100000
+> arabic: 32.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 1000000
+> arabic: 64.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 10000000
+> arabic: 128.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 100000000
+> arabic: 256.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 1000000000
+> arabic: 512.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 10000000000
+> arabic: 1024.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 100000000000
+> arabic: 2048.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 1000000000000
+> arabic: 4096.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 10000000000000
+> arabic: 8192.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 100000000000000
+> arabic: 16384.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 1000000000000000
+> arabic: 32768.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 10000000000000000
+> arabic: 65536.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 100000000000000000
+> arabic: 131072.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 1000000000000000000
+> arabic: 262144.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 10000000000000000000
+> arabic: 524288.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 100000000000000000000
+> arabic: 1048576.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 1000000000000000000000
+> arabic: 2097152.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 10000000000000000000000
+> arabic: 4194304.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 100000000000000000000000
+> arabic: 8388608.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 1000000000000000000000000
+> arabic: 16777216.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 10000000000000000000000000
+> arabic: 33554432.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 100000000000000000000000000
+> arabic: 67108864.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 1000000000000000000000000000
+> arabic: 134217728.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 10000000000000000000000000000
+> arabic: 268435456.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 100000000000000000000000000000
+> arabic: 536870912.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 1000000000000000000000000000000
+> arabic: 1073741824.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 10000000000000000000000000000000
+> arabic: 2147483648.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 100000000000000000000000000000000
+> arabic: 4294967296.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 1000000000000000000000000000000000
+> arabic: 8589934592.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 10000000000000000000000000000000000
+> arabic: 17179869184.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 100000000000000000000000000000000000
+> arabic: 34359738368.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 1000000000000000000000000000000000000
+> arabic: 68719476736.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 10000000000000000000000000000000000000
+> arabic: 137438953472.
+<recently read> }
+l. ... }
+The bitset \l_my_testb_bitset has the representation:
+> binary: 100000000000000000000000000000000000000
+> arabic: 274877906944.
+<recently read> }
+l. ... }
+============================================================
More information about the latex3-commits
mailing list.