[latex3-commits] [git/LaTeX3-latex3-latex3] master: Avoid rounding in \fp_format:nn without style nor precision (fixes #612) (2a8bb9119)

Bruno Le Floch bruno at le-floch.fr
Sun Aug 25 11:14:15 CEST 2019


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

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

commit 2a8bb91196f4bf9bc97c0f634389e3485a79b0e2
Author: Bruno Le Floch <bruno at le-floch.fr>
Date:   Sun Aug 25 11:14:15 2019 +0200

    Avoid rounding in \fp_format:nn without style nor precision (fixes #612)


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

2a8bb91196f4bf9bc97c0f634389e3485a79b0e2
 l3experimental/CHANGELOG.md                        |  1 +
 l3experimental/l3str/l3str-format.dtx              | 30 ++++++++++++++++------
 l3experimental/l3str/testfiles/m3str-format002.lvt |  4 ++-
 l3experimental/l3str/testfiles/m3str-format002.tlg | 20 +++++++++++++++
 4 files changed, 46 insertions(+), 9 deletions(-)

diff --git a/l3experimental/CHANGELOG.md b/l3experimental/CHANGELOG.md
index 7599b3755..3629539b8 100644
--- a/l3experimental/CHANGELOG.md
+++ b/l3experimental/CHANGELOG.md
@@ -12,6 +12,7 @@ this project uses date-based 'snapshot' version identifiers.
 - `\draw_unit_vector:n` returns a vertical vector when the length is
   zero (see #609)
 - Collect `pwd` data with no `\endlinechar` (see #613)
+- Default precision in `\fp_format:nn` when no style is specified
 
 ### Fixed
 
diff --git a/l3experimental/l3str/l3str-format.dtx b/l3experimental/l3str/l3str-format.dtx
index 1afe6ea2d..8105738a3 100644
--- a/l3experimental/l3str/l3str-format.dtx
+++ b/l3experimental/l3str/l3str-format.dtx
@@ -127,7 +127,7 @@
 %   \end{syntax}
 %   Evaluates the \meta{floating point expression} and converts the
 %   result to a string according to the \meta{format specification}.
-%   The \meta{precision} defaults to $6$.  The \meta{style} can be
+%   The \meta{style} can be
 %   \begin{itemize}
 %     \item |e| for scientific notation, with one digit before and
 %       \meta{precision} digits after the decimal separator, and an
@@ -138,6 +138,9 @@
 %       in the range $[10^{-4}, 10^{\meta{precision}})$ and style |e|
 %       otherwise.
 %   \end{itemize}
+%   When there is no \meta{style} specifier nor \meta{precision} the
+%   number is displayed without rounding.  Otherwise the
+%   \meta{precision} defaults to~$6$.
 % \end{function}
 %
 % \section{Possibilities, and things to do}
@@ -732,8 +735,9 @@
 %   (default), put nothing, represented as a brace group.  The width
 %   |#4| will be useful later, store it after \cs{s_stop}.  Afterwards,
 %   check the \meta{precision}: if it was not given, replace it by $6$
-%   (default precision).  Finally, dispatch depending on the
-%   \meta{style}.
+%   (default precision) unless no \meta{style} was given: in that case
+%   we want to use whatever precision is needed to fully describe the
+%   number.  Finally, dispatch depending on the \meta{style}.
 %    \begin{macrocode}
 \cs_new:Npn \@@_format_fp:NNNnnNw
     #1#2#3#4#5#6 \s__fp \__fp_chk:w #7 #8
@@ -753,7 +757,11 @@
       }
     \@@_format_put:nw { {#4} }
     \tl_if_empty:nTF {#5}
-      { \@@_format_put:nw { { 6} } }
+      {
+        \token_if_eq_meaning:NNTF #6 ?
+          { \@@_format_put:nw { {  } } }
+          { \@@_format_put:nw { { 6} } }
+      }
       { \@@_format_put:nw { {#5} } }
     \str_case:nnF {#6}
       {
@@ -879,7 +887,9 @@
 % \begin{macro}[EXP]{\@@_format_fp_g:wn}
 % \begin{macro}[EXP]{\@@_format_fp_g_aux:wn}
 % \begin{macro}[EXP]{\@@_format_fp_to_scientific:n, \@@_format_fp_trim:w}
-%   With the |g| type, first filter out special cases.  In the normal
+%   With the |g| type, first filter out special cases and the case where
+%   neither style nor precision were given (then one should not round).
+%   In the normal
 %   case, round to |#4| significant figures, then test the exponent: if
 %   $-4\leq \meta{exponent} < \meta{precision}$, use the presentation
 %   type |f|, otherwise use the presentation type |e|.  Also, a
@@ -898,12 +908,16 @@
         {3} { nan }
       }
       {
-        \exp_last_unbraced:Nf \@@_format_fp_g_aux:wn
+        \tl_if_empty:nTF {#3}
+          { \fp_to_tl:n { abs ( \s__fp \__fp_chk:w #1#2 ; ) } }
           {
-            \@@_format_fp_round:wn \s__fp \__fp_chk:w #1#2 ;
+            \exp_last_unbraced:Nf \@@_format_fp_g_aux:wn
+              {
+                \@@_format_fp_round:wn \s__fp \__fp_chk:w #1#2 ;
+                  { \int_max:nn {1} {#3} }
+              }
               { \int_max:nn {1} {#3} }
           }
-          { \int_max:nn {1} {#3} }
       }
   }
 \cs_new:Npn \@@_format_fp_g_aux:wn #1; #2
diff --git a/l3experimental/l3str/testfiles/m3str-format002.lvt b/l3experimental/l3str/testfiles/m3str-format002.lvt
index e44524407..6eff3e98c 100644
--- a/l3experimental/l3str/testfiles/m3str-format002.lvt
+++ b/l3experimental/l3str/testfiles/m3str-format002.lvt
@@ -1,5 +1,5 @@
 %
-% Copyright (C) 2012,2018 LaTeX3 Project
+% Copyright (C) 2012,2018,2019 LaTeX3 Project
 %
 
 \documentclass{minimal}
@@ -34,6 +34,7 @@
     \TESTEXP { Formatting~floating~point~#1 }
       {
         | \fp_format:nn {#1} {            } | \NEWLINE
+        | \fp_format:nn {#1} {          g } | \NEWLINE
         | \fp_format:nn {#1} {     18.5   } | \NEWLINE
         | \fp_format:nn {#1} {     18.10e } | \NEWLINE
         | \fp_format:nn {#1} {     18.1 f } | \NEWLINE
@@ -42,6 +43,7 @@
         | \fp_format:nn {#1} {  > +18.22f } | \NEWLINE
         | \fp_format:nn {#1} { .< +18.7 g } | \NEWLINE
         | \fp_format:nn {#1} { .^ -18     } | \NEWLINE
+        | \fp_format:nn {#1} { .^ -18   g } | \NEWLINE
         | \fp_format:nn {#1} { .> -18.7 f } | \NEWLINE
         | \fp_format:nn {#1} { >> ~18.6 g } | \NEWLINE
         | \fp_format:nn {#1} { ^< ~18.1 g } | \NEWLINE
diff --git a/l3experimental/l3str/testfiles/m3str-format002.tlg b/l3experimental/l3str/testfiles/m3str-format002.tlg
index 155a4ce29..096e72afe 100644
--- a/l3experimental/l3str/testfiles/m3str-format002.tlg
+++ b/l3experimental/l3str/testfiles/m3str-format002.tlg
@@ -4,6 +4,7 @@ Author: Bruno Le Floch
 ============================================================
 TEST 1: Formatting floating point 123456.7891234567
 ============================================================
+|123456.7891234567|
 |123457|
 |          1.2346e5|
 |    1.2345678912e5|
@@ -12,6 +13,7 @@ TEST 1: Formatting floating point 123456.7891234567
 |    1.234568e5    |
 |+123456.7891234567000000000000|
 |+123456.8.........|
+|123456.7891234567.|
 |......123457......|
 |....123456.7891235|
 |>>>>>>>>>>> 123457|
@@ -22,6 +24,7 @@ TEST 1: Formatting floating point 123456.7891234567
 ============================================================
 TEST 2: Formatting floating point -123456.7891234567e10
 ============================================================
+|-1234567891234567|
 |-1.23457e15|
 |        -1.2346e15|
 |  -1.2345678912e15|
@@ -30,6 +33,7 @@ TEST 2: Formatting floating point -123456.7891234567e10
 |   -1.234568e15   |
 |-1234567891234567.0000000000000000000000|
 |-1.234568e15......|
+|-1234567891234567.|
 |...-1.23457e15....|
 |-1234567891234567.0000000|
 |>>>>>>>-1.23457e15|
@@ -40,6 +44,7 @@ TEST 2: Formatting floating point -123456.7891234567e10
 ============================================================
 TEST 3: Formatting floating point 1.23456789124567e-10
 ============================================================
+|1.23456789124567e-10|
 |1.23457e-10|
 |        1.2346e-10|
 |  1.2345678912e-10|
@@ -48,6 +53,7 @@ TEST 3: Formatting floating point 1.23456789124567e-10
 |   1.234568e-10   |
 |+0.0000000001234567891246|
 |+1.234568e-10.....|
+|1.23456789124567e-10|
 |...1.23457e-10....|
 |.........0.0000000|
 |>>>>>> 1.23457e-10|
@@ -58,6 +64,7 @@ TEST 3: Formatting floating point 1.23456789124567e-10
 ============================================================
 TEST 4: Formatting floating point 1.23456789124567e-100
 ============================================================
+|1.23456789124567e-100|
 |1.23457e-100|
 |       1.2346e-100|
 | 1.2345678912e-100|
@@ -66,6 +73,7 @@ TEST 4: Formatting floating point 1.23456789124567e-100
 |  1.234568e-100   |
 |+0.0000000000000000000000|
 |+1.234568e-100....|
+|1.23456789124567e-100|
 |...1.23457e-100...|
 |.........0.0000000|
 |>>>>> 1.23457e-100|
@@ -76,6 +84,7 @@ TEST 4: Formatting floating point 1.23456789124567e-100
 ============================================================
 TEST 5: Formatting floating point -.999999999999999
 ============================================================
+|-0.999999999999999|
 |-1|
 |                -1|
 |   -1.0000000000e0|
@@ -84,6 +93,7 @@ TEST 5: Formatting floating point -.999999999999999
 |   -1.000000e0    |
 |-0.9999999999999990000000|
 |-1................|
+|-0.999999999999999|
 |........-1........|
 |........-1.0000000|
 |>>>>>>>>>>>>>>>>-1|
@@ -95,6 +105,7 @@ TEST 5: Formatting floating point -.999999999999999
 TEST 6: Formatting floating point 0
 ============================================================
 |0|
+|0|
 |                 0|
 |    0.0000000000e0|
 |               0.0|
@@ -103,6 +114,7 @@ TEST 6: Formatting floating point 0
 |+0.0000000000000000000000|
 |+0................|
 |........0.........|
+|........0.........|
 |.........0.0000000|
 |>>>>>>>>>>>>>>>> 0|
 | 0^^^^^^^^^^^^^^^^|
@@ -113,6 +125,7 @@ TEST 6: Formatting floating point 0
 TEST 7: Formatting floating point -0
 ============================================================
 |-0|
+|-0|
 |                -0|
 |   -0.0000000000e0|
 |              -0.0|
@@ -121,6 +134,7 @@ TEST 7: Formatting floating point -0
 |-0.0000000000000000000000|
 |-0................|
 |........-0........|
+|........-0........|
 |........-0.0000000|
 |>>>>>>>>>>>>>>>>-0|
 |-0^^^^^^^^^^^^^^^^|
@@ -131,6 +145,7 @@ TEST 7: Formatting floating point -0
 TEST 8: Formatting floating point inf
 ============================================================
 |inf|
+|inf|
 |               inf|
 |               inf|
 |               inf|
@@ -139,6 +154,7 @@ TEST 8: Formatting floating point inf
 |              +inf|
 |+inf..............|
 |.......inf........|
+|.......inf........|
 |...............inf|
 |>>>>>>>>>>>>>> inf|
 | inf^^^^^^^^^^^^^^|
@@ -149,6 +165,7 @@ TEST 8: Formatting floating point inf
 TEST 9: Formatting floating point -inf
 ============================================================
 |-inf|
+|-inf|
 |              -inf|
 |              -inf|
 |              -inf|
@@ -157,6 +174,7 @@ TEST 9: Formatting floating point -inf
 |              -inf|
 |-inf..............|
 |.......-inf.......|
+|.......-inf.......|
 |..............-inf|
 |>>>>>>>>>>>>>>-inf|
 |-inf^^^^^^^^^^^^^^|
@@ -167,6 +185,7 @@ TEST 9: Formatting floating point -inf
 TEST 10: Formatting floating point nan
 ============================================================
 |nan|
+|nan|
 |               nan|
 |               nan|
 |               nan|
@@ -175,6 +194,7 @@ TEST 10: Formatting floating point nan
 |              +nan|
 |+nan..............|
 |.......nan........|
+|.......nan........|
 |...............nan|
 |>>>>>>>>>>>>>> nan|
 | nan^^^^^^^^^^^^^^|





More information about the latex3-commits mailing list