[latex3-commits] [git/LaTeX3-latex3-latex3] master: Implement logb to get exponent of floating point number (see #352) (c4c2921)

Bruno Le Floch bruno at le-floch.fr
Sat Nov 3 17:57:37 CET 2018


Repository : https://github.com/latex3/latex3
On branch  : master
Link       : https://github.com/latex3/latex3/commit/c4c2921e17614741f9e93953a7be5c08c8ba3747

>---------------------------------------------------------------

commit c4c2921e17614741f9e93953a7be5c08c8ba3747
Author: Bruno Le Floch <bruno at le-floch.fr>
Date:   Sat Nov 3 17:56:52 2018 +0100

    Implement logb to get exponent of floating point number (see #352)


>---------------------------------------------------------------

c4c2921e17614741f9e93953a7be5c08c8ba3747
 l3kernel/l3candidates.dtx               |   15 ++++++++++++++-
 l3kernel/l3fp-basics.dtx                |   27 ++++++++++++++++++++++++++-
 l3kernel/testfiles/m3expl001.luatex.tlg |    3 +++
 l3kernel/testfiles/m3expl001.ptex.tlg   |    3 +++
 l3kernel/testfiles/m3expl001.tlg        |    3 +++
 l3kernel/testfiles/m3expl001.uptex.tlg  |    3 +++
 l3kernel/testfiles/m3expl001.xetex.tlg  |    3 +++
 l3kernel/testfiles/m3expl003.luatex.tlg |    3 +++
 l3kernel/testfiles/m3expl003.ptex.tlg   |    3 +++
 l3kernel/testfiles/m3expl003.tlg        |    3 +++
 l3kernel/testfiles/m3expl003.uptex.tlg  |    3 +++
 l3kernel/testfiles/m3expl003.xetex.tlg  |    3 +++
 l3kernel/testfiles/m3fp-basics005.lvt   |   19 +++++++++++++++++++
 l3kernel/testfiles/m3fp-basics005.tlg   |   18 ++++++++++++++++++
 14 files changed, 107 insertions(+), 2 deletions(-)

diff --git a/l3kernel/l3candidates.dtx b/l3kernel/l3candidates.dtx
index f8e1c40..cd5751b 100644
--- a/l3kernel/l3candidates.dtx
+++ b/l3kernel/l3candidates.dtx
@@ -269,7 +269,20 @@
 %   applicable.
 % \end{function}
 %
-% \section{Additions to \pkg{l3fp-convert}}
+% \section{Additions to \pkg{l3fp}}
+%
+% \begin{function}[EXP, added = 2018-11-03]{logb}
+%   \begin{syntax}
+%     \cs{fp_eval:n} \{ |logb(| \meta{fpexpr} |)| \}
+%   \end{syntax}
+%   Determines the exponent of the \meta{fpexpr}, namely the floor of
+%   the base-$10$ logarithm of its absolute value.  \enquote{Division by
+%   zero} occurs when evaluating $\operatorname{logb}(\pm 0) = -\infty$.
+%   Other special values are $\operatorname{logb}(\pm\infty)=+\infty$
+%   and $\operatorname{logb}(\nan{})=\nan{}$.  If the operand is a tuple
+%   or is \nan{}, then \enquote{invalid operation} occurs and the result
+%   is \nan{}.
+% \end{function}
 %
 % \begin{function}[EXP, added = 2018-11-03]{\fp_sign:n}
 %   \begin{syntax}
diff --git a/l3kernel/l3fp-basics.dtx b/l3kernel/l3fp-basics.dtx
index 4481093..34b20ee 100644
--- a/l3kernel/l3fp-basics.dtx
+++ b/l3kernel/l3fp-basics.dtx
@@ -73,6 +73,7 @@
 % \begin{macro}[EXP]
 %   {
 %     \@@_parse_word_abs:N   ,
+%     \@@_parse_word_logb:N  ,
 %     \@@_parse_word_sign:N  ,
 %     \@@_parse_word_sqrt:N  ,
 %   }
@@ -80,6 +81,8 @@
 %    \begin{macrocode}
 \cs_new:Npn \@@_parse_word_abs:N
   { \@@_parse_unary_function:NNN \@@_set_sign_o:w 0 }
+\cs_new:Npn \@@_parse_word_logb:N
+  { \@@_parse_unary_function:NNN \@@_logb_o:w ? }
 \cs_new:Npn \@@_parse_word_sign:N
   { \@@_parse_unary_function:NNN \@@_sign_o:w ? }
 \cs_new:Npn \@@_parse_word_sqrt:N
@@ -2029,7 +2032,29 @@
 %    \end{macrocode}
 % \end{macro}
 %
-% \subsection{About the sign}
+% \subsection{About the sign and exponent}
+%
+% \begin{macro}[EXP]{\@@_logb_o:w, \@@_logb_aux_o:w}
+%   The exponent of a normal number is its \meta{exponent} minus one.
+%    \begin{macrocode}
+\cs_new:Npn \@@_logb_o:w ? \s_@@ \@@_chk:w #1#2; @
+  {
+    \if_case:w #1 \exp_stop_f:
+           \@@_case_use:nw
+             { \@@_division_by_zero_o:Nnw \c_minus_inf_fp { logb } }
+    \or:   \exp_after:wN \@@_logb_aux_o:w
+    \or:   \@@_case_return_o:Nw \c_inf_fp
+    \else: \@@_case_return_same_o:w
+    \fi:
+    \s_@@ \@@_chk:w #1 #2;
+  }
+\cs_new:Npn \@@_logb_aux_o:w \s_@@ \@@_chk:w #1 #2 #3 #4 ;
+  {
+    \exp_after:wN \@@_parse:n \exp_after:wN
+      { \int_value:w \int_eval:w #3 - 1 \exp_after:wN }
+  }
+%    \end{macrocode}
+% \end{macro}
 %
 % \begin{macro}[EXP]{\@@_sign_o:w}
 % \begin{macro}[EXP]{\@@_sign_aux_o:w}
diff --git a/l3kernel/testfiles/m3expl001.luatex.tlg b/l3kernel/testfiles/m3expl001.luatex.tlg
index 264c575..9b750a1 100644
--- a/l3kernel/testfiles/m3expl001.luatex.tlg
+++ b/l3kernel/testfiles/m3expl001.luatex.tlg
@@ -3944,6 +3944,7 @@ Defining \__fp_ternary:NwwN on line ...
 Defining \__fp_ternary_auxi:NwwN on line ...
 Defining \__fp_ternary_auxii:NwwN on line ...
 Defining \__fp_parse_word_abs:N on line ...
+Defining \__fp_parse_word_logb:N on line ...
 Defining \__fp_parse_word_sign:N on line ...
 Defining \__fp_parse_word_sqrt:N on line ...
 Defining \__fp_-_o:ww on line ...
@@ -4020,6 +4021,8 @@ Defining \__fp_sqrt_auxxi_o:wwnnN on line ...
 Defining \__fp_sqrt_auxxii_o:nnnnnnnnw on line ...
 Defining \__fp_sqrt_auxxiii_o:w on line ...
 Defining \__fp_sqrt_auxxiv_o:wnnnnnnnN on line ...
+Defining \__fp_logb_o:w on line ...
+Defining \__fp_logb_aux_o:w on line ...
 Defining \__fp_sign_o:w on line ...
 Defining \__fp_sign_aux_o:w on line ...
 Defining \__fp_set_sign_o:w on line ...
diff --git a/l3kernel/testfiles/m3expl001.ptex.tlg b/l3kernel/testfiles/m3expl001.ptex.tlg
index 342a731..4cb9333 100644
--- a/l3kernel/testfiles/m3expl001.ptex.tlg
+++ b/l3kernel/testfiles/m3expl001.ptex.tlg
@@ -4237,6 +4237,7 @@ Defining \__fp_ternary:NwwN on line ...
 Defining \__fp_ternary_auxi:NwwN on line ...
 Defining \__fp_ternary_auxii:NwwN on line ...
 Defining \__fp_parse_word_abs:N on line ...
+Defining \__fp_parse_word_logb:N on line ...
 Defining \__fp_parse_word_sign:N on line ...
 Defining \__fp_parse_word_sqrt:N on line ...
 Defining \__fp_-_o:ww on line ...
@@ -4313,6 +4314,8 @@ Defining \__fp_sqrt_auxxi_o:wwnnN on line ...
 Defining \__fp_sqrt_auxxii_o:nnnnnnnnw on line ...
 Defining \__fp_sqrt_auxxiii_o:w on line ...
 Defining \__fp_sqrt_auxxiv_o:wnnnnnnnN on line ...
+Defining \__fp_logb_o:w on line ...
+Defining \__fp_logb_aux_o:w on line ...
 Defining \__fp_sign_o:w on line ...
 Defining \__fp_sign_aux_o:w on line ...
 Defining \__fp_set_sign_o:w on line ...
diff --git a/l3kernel/testfiles/m3expl001.tlg b/l3kernel/testfiles/m3expl001.tlg
index bd40260..8a0aaac 100644
--- a/l3kernel/testfiles/m3expl001.tlg
+++ b/l3kernel/testfiles/m3expl001.tlg
@@ -4237,6 +4237,7 @@ Defining \__fp_ternary:NwwN on line ...
 Defining \__fp_ternary_auxi:NwwN on line ...
 Defining \__fp_ternary_auxii:NwwN on line ...
 Defining \__fp_parse_word_abs:N on line ...
+Defining \__fp_parse_word_logb:N on line ...
 Defining \__fp_parse_word_sign:N on line ...
 Defining \__fp_parse_word_sqrt:N on line ...
 Defining \__fp_-_o:ww on line ...
@@ -4313,6 +4314,8 @@ Defining \__fp_sqrt_auxxi_o:wwnnN on line ...
 Defining \__fp_sqrt_auxxii_o:nnnnnnnnw on line ...
 Defining \__fp_sqrt_auxxiii_o:w on line ...
 Defining \__fp_sqrt_auxxiv_o:wnnnnnnnN on line ...
+Defining \__fp_logb_o:w on line ...
+Defining \__fp_logb_aux_o:w on line ...
 Defining \__fp_sign_o:w on line ...
 Defining \__fp_sign_aux_o:w on line ...
 Defining \__fp_set_sign_o:w on line ...
diff --git a/l3kernel/testfiles/m3expl001.uptex.tlg b/l3kernel/testfiles/m3expl001.uptex.tlg
index 81056cb..3058484 100644
--- a/l3kernel/testfiles/m3expl001.uptex.tlg
+++ b/l3kernel/testfiles/m3expl001.uptex.tlg
@@ -4237,6 +4237,7 @@ Defining \__fp_ternary:NwwN on line ...
 Defining \__fp_ternary_auxi:NwwN on line ...
 Defining \__fp_ternary_auxii:NwwN on line ...
 Defining \__fp_parse_word_abs:N on line ...
+Defining \__fp_parse_word_logb:N on line ...
 Defining \__fp_parse_word_sign:N on line ...
 Defining \__fp_parse_word_sqrt:N on line ...
 Defining \__fp_-_o:ww on line ...
@@ -4313,6 +4314,8 @@ Defining \__fp_sqrt_auxxi_o:wwnnN on line ...
 Defining \__fp_sqrt_auxxii_o:nnnnnnnnw on line ...
 Defining \__fp_sqrt_auxxiii_o:w on line ...
 Defining \__fp_sqrt_auxxiv_o:wnnnnnnnN on line ...
+Defining \__fp_logb_o:w on line ...
+Defining \__fp_logb_aux_o:w on line ...
 Defining \__fp_sign_o:w on line ...
 Defining \__fp_sign_aux_o:w on line ...
 Defining \__fp_set_sign_o:w on line ...
diff --git a/l3kernel/testfiles/m3expl001.xetex.tlg b/l3kernel/testfiles/m3expl001.xetex.tlg
index 8ef2c99..6862742 100644
--- a/l3kernel/testfiles/m3expl001.xetex.tlg
+++ b/l3kernel/testfiles/m3expl001.xetex.tlg
@@ -3975,6 +3975,7 @@ Defining \__fp_ternary:NwwN on line ...
 Defining \__fp_ternary_auxi:NwwN on line ...
 Defining \__fp_ternary_auxii:NwwN on line ...
 Defining \__fp_parse_word_abs:N on line ...
+Defining \__fp_parse_word_logb:N on line ...
 Defining \__fp_parse_word_sign:N on line ...
 Defining \__fp_parse_word_sqrt:N on line ...
 Defining \__fp_-_o:ww on line ...
@@ -4051,6 +4052,8 @@ Defining \__fp_sqrt_auxxi_o:wwnnN on line ...
 Defining \__fp_sqrt_auxxii_o:nnnnnnnnw on line ...
 Defining \__fp_sqrt_auxxiii_o:w on line ...
 Defining \__fp_sqrt_auxxiv_o:wnnnnnnnN on line ...
+Defining \__fp_logb_o:w on line ...
+Defining \__fp_logb_aux_o:w on line ...
 Defining \__fp_sign_o:w on line ...
 Defining \__fp_sign_aux_o:w on line ...
 Defining \__fp_set_sign_o:w on line ...
diff --git a/l3kernel/testfiles/m3expl003.luatex.tlg b/l3kernel/testfiles/m3expl003.luatex.tlg
index 264c575..9b750a1 100644
--- a/l3kernel/testfiles/m3expl003.luatex.tlg
+++ b/l3kernel/testfiles/m3expl003.luatex.tlg
@@ -3944,6 +3944,7 @@ Defining \__fp_ternary:NwwN on line ...
 Defining \__fp_ternary_auxi:NwwN on line ...
 Defining \__fp_ternary_auxii:NwwN on line ...
 Defining \__fp_parse_word_abs:N on line ...
+Defining \__fp_parse_word_logb:N on line ...
 Defining \__fp_parse_word_sign:N on line ...
 Defining \__fp_parse_word_sqrt:N on line ...
 Defining \__fp_-_o:ww on line ...
@@ -4020,6 +4021,8 @@ Defining \__fp_sqrt_auxxi_o:wwnnN on line ...
 Defining \__fp_sqrt_auxxii_o:nnnnnnnnw on line ...
 Defining \__fp_sqrt_auxxiii_o:w on line ...
 Defining \__fp_sqrt_auxxiv_o:wnnnnnnnN on line ...
+Defining \__fp_logb_o:w on line ...
+Defining \__fp_logb_aux_o:w on line ...
 Defining \__fp_sign_o:w on line ...
 Defining \__fp_sign_aux_o:w on line ...
 Defining \__fp_set_sign_o:w on line ...
diff --git a/l3kernel/testfiles/m3expl003.ptex.tlg b/l3kernel/testfiles/m3expl003.ptex.tlg
index 342a731..4cb9333 100644
--- a/l3kernel/testfiles/m3expl003.ptex.tlg
+++ b/l3kernel/testfiles/m3expl003.ptex.tlg
@@ -4237,6 +4237,7 @@ Defining \__fp_ternary:NwwN on line ...
 Defining \__fp_ternary_auxi:NwwN on line ...
 Defining \__fp_ternary_auxii:NwwN on line ...
 Defining \__fp_parse_word_abs:N on line ...
+Defining \__fp_parse_word_logb:N on line ...
 Defining \__fp_parse_word_sign:N on line ...
 Defining \__fp_parse_word_sqrt:N on line ...
 Defining \__fp_-_o:ww on line ...
@@ -4313,6 +4314,8 @@ Defining \__fp_sqrt_auxxi_o:wwnnN on line ...
 Defining \__fp_sqrt_auxxii_o:nnnnnnnnw on line ...
 Defining \__fp_sqrt_auxxiii_o:w on line ...
 Defining \__fp_sqrt_auxxiv_o:wnnnnnnnN on line ...
+Defining \__fp_logb_o:w on line ...
+Defining \__fp_logb_aux_o:w on line ...
 Defining \__fp_sign_o:w on line ...
 Defining \__fp_sign_aux_o:w on line ...
 Defining \__fp_set_sign_o:w on line ...
diff --git a/l3kernel/testfiles/m3expl003.tlg b/l3kernel/testfiles/m3expl003.tlg
index bd40260..8a0aaac 100644
--- a/l3kernel/testfiles/m3expl003.tlg
+++ b/l3kernel/testfiles/m3expl003.tlg
@@ -4237,6 +4237,7 @@ Defining \__fp_ternary:NwwN on line ...
 Defining \__fp_ternary_auxi:NwwN on line ...
 Defining \__fp_ternary_auxii:NwwN on line ...
 Defining \__fp_parse_word_abs:N on line ...
+Defining \__fp_parse_word_logb:N on line ...
 Defining \__fp_parse_word_sign:N on line ...
 Defining \__fp_parse_word_sqrt:N on line ...
 Defining \__fp_-_o:ww on line ...
@@ -4313,6 +4314,8 @@ Defining \__fp_sqrt_auxxi_o:wwnnN on line ...
 Defining \__fp_sqrt_auxxii_o:nnnnnnnnw on line ...
 Defining \__fp_sqrt_auxxiii_o:w on line ...
 Defining \__fp_sqrt_auxxiv_o:wnnnnnnnN on line ...
+Defining \__fp_logb_o:w on line ...
+Defining \__fp_logb_aux_o:w on line ...
 Defining \__fp_sign_o:w on line ...
 Defining \__fp_sign_aux_o:w on line ...
 Defining \__fp_set_sign_o:w on line ...
diff --git a/l3kernel/testfiles/m3expl003.uptex.tlg b/l3kernel/testfiles/m3expl003.uptex.tlg
index 81056cb..3058484 100644
--- a/l3kernel/testfiles/m3expl003.uptex.tlg
+++ b/l3kernel/testfiles/m3expl003.uptex.tlg
@@ -4237,6 +4237,7 @@ Defining \__fp_ternary:NwwN on line ...
 Defining \__fp_ternary_auxi:NwwN on line ...
 Defining \__fp_ternary_auxii:NwwN on line ...
 Defining \__fp_parse_word_abs:N on line ...
+Defining \__fp_parse_word_logb:N on line ...
 Defining \__fp_parse_word_sign:N on line ...
 Defining \__fp_parse_word_sqrt:N on line ...
 Defining \__fp_-_o:ww on line ...
@@ -4313,6 +4314,8 @@ Defining \__fp_sqrt_auxxi_o:wwnnN on line ...
 Defining \__fp_sqrt_auxxii_o:nnnnnnnnw on line ...
 Defining \__fp_sqrt_auxxiii_o:w on line ...
 Defining \__fp_sqrt_auxxiv_o:wnnnnnnnN on line ...
+Defining \__fp_logb_o:w on line ...
+Defining \__fp_logb_aux_o:w on line ...
 Defining \__fp_sign_o:w on line ...
 Defining \__fp_sign_aux_o:w on line ...
 Defining \__fp_set_sign_o:w on line ...
diff --git a/l3kernel/testfiles/m3expl003.xetex.tlg b/l3kernel/testfiles/m3expl003.xetex.tlg
index 8ef2c99..6862742 100644
--- a/l3kernel/testfiles/m3expl003.xetex.tlg
+++ b/l3kernel/testfiles/m3expl003.xetex.tlg
@@ -3975,6 +3975,7 @@ Defining \__fp_ternary:NwwN on line ...
 Defining \__fp_ternary_auxi:NwwN on line ...
 Defining \__fp_ternary_auxii:NwwN on line ...
 Defining \__fp_parse_word_abs:N on line ...
+Defining \__fp_parse_word_logb:N on line ...
 Defining \__fp_parse_word_sign:N on line ...
 Defining \__fp_parse_word_sqrt:N on line ...
 Defining \__fp_-_o:ww on line ...
@@ -4051,6 +4052,8 @@ Defining \__fp_sqrt_auxxi_o:wwnnN on line ...
 Defining \__fp_sqrt_auxxii_o:nnnnnnnnw on line ...
 Defining \__fp_sqrt_auxxiii_o:w on line ...
 Defining \__fp_sqrt_auxxiv_o:wnnnnnnnN on line ...
+Defining \__fp_logb_o:w on line ...
+Defining \__fp_logb_aux_o:w on line ...
 Defining \__fp_sign_o:w on line ...
 Defining \__fp_sign_aux_o:w on line ...
 Defining \__fp_set_sign_o:w on line ...
diff --git a/l3kernel/testfiles/m3fp-basics005.lvt b/l3kernel/testfiles/m3fp-basics005.lvt
index ad07326..f57a8c5 100644
--- a/l3kernel/testfiles/m3fp-basics005.lvt
+++ b/l3kernel/testfiles/m3fp-basics005.lvt
@@ -38,4 +38,23 @@
   }
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\TEST { logb() }
+  {
+    \TYPE { \fp_to_tl:n { logb(-0) } }
+    \TYPE { \fp_to_tl:n { logb(+0) } }
+    \TYPE { \fp_to_tl:n { logb(-1) } }
+    \TYPE { \fp_to_tl:n { logb(+1) } }
+    \TYPE { \fp_to_tl:n { logb(-.9999999999999999e10000) } }
+    \TYPE { \fp_to_tl:n { logb(+.9999999999999999e10000) } }
+    \TYPE { \fp_to_tl:n { logb(-.1e-10000) } }
+    \TYPE { \fp_to_tl:n { logb(+.1e-10000) } }
+    \TYPE { \fp_to_tl:n { logb(inf) } }
+    \TYPE { \fp_to_tl:n { logb(-inf) } }
+    \TYPE { \fp_to_tl:n { logb(nan) } }
+    \TYPE { \fp_to_tl:n { logb(8.3746251^2) } }
+    \TYPE { \fp_to_tl:n { logb( 1.765432987654 ) } }
+    \TYPE { \fp_to_tl:n { round(1234.567, 2 - logb(1234.567)) } }
+  }
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \END
diff --git a/l3kernel/testfiles/m3fp-basics005.tlg b/l3kernel/testfiles/m3fp-basics005.tlg
index 710399b..6b4c71e 100644
--- a/l3kernel/testfiles/m3fp-basics005.tlg
+++ b/l3kernel/testfiles/m3fp-basics005.tlg
@@ -146,3 +146,21 @@ nan
 31.42696819415675
 31.74357147323754
 ============================================================
+============================================================
+TEST 2: logb()
+============================================================
+-inf
+-inf
+0
+0
+9999
+9999
+-10001
+-10001
+inf
+inf
+nan
+1
+0
+1230
+============================================================





More information about the latex3-commits mailing list