1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
% \iffalse meta-comment
%%
%% File: l3fp-assign.dtx Copyright (C) 2011-2012 The LaTeX3 project
%%
%% It may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License (LPPL), either version 1.3c of this
%% license or (at your option) any later version. The latest version
%% of this license is in the file
%%
%% http://www.latex-project.org/lppl.txt
%%
%% This file is part of the "l3kernel bundle" (The Work in LPPL)
%% and all files in that bundle must be distributed together.
%%
%% The released version of this bundle is available from CTAN.
%%
%% -----------------------------------------------------------------------
%%
%% The development version of the bundle can be found at
%%
%% http://www.latex-project.org/svnroot/experimental/trunk/
%%
%% for those people who are interested.
%%
%%%%%%%%%%%
%% NOTE: %%
%%%%%%%%%%%
%%
%% Snapshots taken from the repository represent work in progress and may
%% not work or may contain conflicting material! We therefore ask
%% people _not_ to put them into distributions, archives, etc. without
%% prior consultation with the LaTeX Project Team.
%%
%% -----------------------------------------------------------------------
%%
%
%<*driver>
\RequirePackage{l3bootstrap}
\GetIdInfo$Id: l3fp-assign.dtx 4082 2012-08-12 12:14:17Z bruno $
{L3 Floating-point assignments}
\documentclass[full]{l3doc}
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{The \textsf{l3fp-assign} package\thanks{This file
% has version number \ExplFileVersion, last
% revised \ExplFileDate.}\\
% Floating point expressions}
% \author{^^A
% The \LaTeX3 Project\thanks
% {^^A
% E-mail:
% \href{mailto:latex-team@latex-project.org}
% {latex-team@latex-project.org}^^A
% }^^A
% }
% \date{Released \ExplFileDate}
% \maketitle
%
% \begin{documentation}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{l3fp-assign} implementation}
%
% \begin{macrocode}
%<*initex|package>
% \end{macrocode}
%
% \begin{macrocode}
%<@@=fp>
% \end{macrocode}
%
% \subsection{Assigning values}
%
% \begin{macro}{\fp_new:N}
% Floating point variables are initialized to be $+0$.
% \begin{macrocode}
\cs_new_protected:Npn \fp_new:N #1
{ \cs_new_eq:NN #1 \c_zero_fp }
\cs_generate_variant:Nn \fp_new:N {c}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}
% {
% \fp_set:Nn, \fp_set:cn,
% \fp_gset:Nn, \fp_gset:cn,
% \fp_const:Nn, \fp_const:cn
% }
% Simply use \cs{@@_parse:n} within various \texttt{x}-expanding
% assignments.
% \begin{macrocode}
\cs_new_protected:Npn \fp_set:Nn #1#2
{ \tl_set:Nx #1 { \@@_parse:n {#2} } }
\cs_new_protected:Npn \fp_gset:Nn #1#2
{ \tl_gset:Nx #1 { \@@_parse:n {#2} } }
\cs_new_protected:Npn \fp_const:Nn #1#2
{ \tl_const:Nx #1 { \@@_parse:n {#2} } }
\cs_generate_variant:Nn \fp_set:Nn {c}
\cs_generate_variant:Nn \fp_gset:Nn {c}
\cs_generate_variant:Nn \fp_const:Nn {c}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}
% {
% \fp_set_eq:NN , \fp_set_eq:cN , \fp_set_eq:Nc , \fp_set_eq:cc,
% \fp_gset_eq:NN, \fp_gset_eq:cN, \fp_gset_eq:Nc, \fp_gset_eq:cc
% }
% Copying a floating point is the same as copying the underlying token
% list.
% \begin{macrocode}
\cs_new_eq:NN \fp_set_eq:NN \tl_set_eq:NN
\cs_new_eq:NN \fp_gset_eq:NN \tl_gset_eq:NN
\cs_generate_variant:Nn \fp_set_eq:NN { c , Nc , cc }
\cs_generate_variant:Nn \fp_gset_eq:NN { c , Nc , cc }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fp_zero:N, \fp_zero:c, \fp_gzero:N, \fp_gzero:c}
% Setting a floating point to zero: copy \cs{c_zero_fp}.
% \begin{macrocode}
\cs_new_protected:Npn \fp_zero:N #1 { \fp_set_eq:NN #1 \c_zero_fp }
\cs_new_protected:Npn \fp_gzero:N #1 { \fp_gset_eq:NN #1 \c_zero_fp }
\cs_generate_variant:Nn \fp_zero:N { c }
\cs_generate_variant:Nn \fp_gzero:N { c }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}
% {\fp_zero_new:N, \fp_zero_new:c, \fp_gzero_new:N, \fp_gzero_new:c}
% Set the floating point to zero, or define it if needed.
% \begin{macrocode}
\cs_new_protected:Npn \fp_zero_new:N #1
{ \fp_if_exist:NTF #1 { \fp_zero:N #1 } { \fp_new:N #1 } }
\cs_new_protected:Npn \fp_gzero_new:N #1
{ \fp_if_exist:NTF #1 { \fp_gzero:N #1 } { \fp_new:N #1 } }
\cs_generate_variant:Nn \fp_zero_new:N { c }
\cs_generate_variant:Nn \fp_gzero_new:N { c }
% \end{macrocode}
% \end{macro}
%
% \subsection{Updating values}
%
% These match the equivalent functions in \pkg{l3int} and \pkg{l3skip}.
%
% \begin{macro}
% {
% \fp_add:Nn, \fp_add:cn, \fp_gadd:Nn, \fp_gadd:cn,
% \fp_sub:Nn, \fp_sub:cn, \fp_gsub:Nn, \fp_gsub:cn,
% }
% \begin{macro}[aux]{\@@_add:NNNn}
% For the sake of error recovery we should not simply set |#1| to
% $|#1| \pm (|#2|)$: for instance, if |#2| is % ^^A(
% |0)+2|, the parsing error would be raised at the last closing
% parenthesis rather than at the closing parenthesis in the user
% argument. Thus we evaluate |#2| instead of just putting
% parentheses. As an optimization we use \cs{@@_parse:n} rather than
% \cs{fp_eval:n}, which would convert the result away from the
% internal representation and back.
% \begin{macrocode}
\cs_new_protected_nopar:Npn \fp_add:Nn { \@@_add:NNNn \fp_set:Nn + }
\cs_new_protected_nopar:Npn \fp_gadd:Nn { \@@_add:NNNn \fp_gset:Nn + }
\cs_new_protected_nopar:Npn \fp_sub:Nn { \@@_add:NNNn \fp_set:Nn - }
\cs_new_protected_nopar:Npn \fp_gsub:Nn { \@@_add:NNNn \fp_gset:Nn - }
\cs_new_protected:Npn \@@_add:NNNn #1#2#3#4
{ #1 #3 { #3 #2 \@@_parse:n {#4} } }
\cs_generate_variant:Nn \fp_add:Nn { c }
\cs_generate_variant:Nn \fp_gadd:Nn { c }
\cs_generate_variant:Nn \fp_sub:Nn { c }
\cs_generate_variant:Nn \fp_gsub:Nn { c }
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Showing values}
%
% \begin{macro}{\fp_show:N, \fp_show:c, \fp_show:n}
% This shows the result of computing its argument. The
% \cs{__msg_show_variable:x} auxiliary expects its input in a slightly odd
% form, starting with |>|, and displays the rest.
% \begin{macrocode}
\cs_new_protected:Npn \fp_show:N #1
{ \__msg_show_variable:x { > \fp_to_tl:N #1 } }
\cs_new_protected:Npn \fp_show:n #1
{ \__msg_show_variable:x { > \fp_to_tl:n {#1} } }
\cs_generate_variant:Nn \fp_show:N { c }
% \end{macrocode}
% \end{macro}
%
% \subsection{Some useful constants and scratch variables}
%
% \begin{variable}{\c_one_fp, \c_e_fp}
% Some constants.
% \begin{macrocode}
\fp_const:Nn \c_e_fp { 2.718 2818 2845 9045 }
\fp_const:Nn \c_one_fp { 1 }
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\c_pi_fp, \c_one_degree_fp}
% We do not round $\pi$ to the closest multiple of $10^{-15}$, which
% would underestimate it by roughly $2.4\cdot 10^{-16}$, but instead
% round it up to the next nearest multiple, which is an overestimate
% by roughly $7.7\cdot 10^{-16}$. This particular choice of rounding
% has very nice properties: it is exactly divisible by $4$ and by
% $180$ as a $16$-digit precision floating point number, hence
% ensuring that $\sin(180\mathrm{deg}) = \sin(\pi) = 0$ exactly, with
% no rounding artifact.
% \begin{macrocode}
\fp_const:Nn \c_pi_fp { 3.141 5926 5358 9794 }
\fp_const:Nn \c_one_degree_fp { 0.0 1745 3292 5199 4330 }
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_tmpa_fp, \l_tmpb_fp, \g_tmpa_fp, \g_tmpb_fp}
% Scratch variables are simply initialized there.
% \begin{macrocode}
\fp_new:N \l_tmpa_fp
\fp_new:N \l_tmpb_fp
\fp_new:N \g_tmpa_fp
\fp_new:N \g_tmpb_fp
% \end{macrocode}
% \end{variable}
%
% \begin{macrocode}
%</initex|package>
% \end{macrocode}
%
% \end{implementation}
%
% \PrintChanges
%
% \PrintIndex
|