\newpage \section{Module utils} The \tkzname{utils} module provides a collection of general-purpose utility functions used throughout the \tkzNamePack{tkz-elements} library. These functions are designed to support common tasks such as numerical rounding, type checking, floating-point comparisons, and table operations. \vspace{1em} Although these functions are not directly related to geometric constructions, they play a vital role in ensuring the consistency, robustness, and readability of the core algorithms. Most of them are small, efficient, and reusable in other contexts. \vspace{1em} This module is loaded automatically by \tkzNamePack{tkz-elements}, but its functions can also be used independently if needed. \subsection{Table of module functions \tkzname{utils}} \begin{table}[htbp] \centering \caption{Functions of the module \tkzMod{utils}.} \begin{tabular}{@{}ll@{}} \toprule \textbf{Function} & \textbf{Reference} \\ \midrule \tkzFct{utils}{utils.parse\_point(str)} & [\ref{sub:function_utils_parse_point}] \\ \tkzFct{utils}{utils.format\_number(r, n)} & [\ref{sub:function_utils_format_number}]\\ \tkzFct{utils}{utils.format\_coord(x, decimals)} & [\ref{sub:function_utils_format_coord}]\\ \tkzFct{utils}{utils.format\_point(z, decimals)} & [\ref{sub:function_utils_format_point}]\\ \tkzFct{utils}{utils.checknumber(x, decimals)} & [\ref{sub:function_utils_checknumber}]\\ \tkzFct{utils}{utils.almost\_equal(a, b, eps)} & [\ref{sub:function_utils_almost_equal}]\\ \tkzFct{utils}{utils.wlog(...)} & [\ref{sub:function_utils_wlog}]\\ \bottomrule \end{tabular} \end{table} \subsection{Function \tkzFct{utils}{parse\_point(str)}} \label{sub:function_utils_parse_point} Parses a string of the form \code{"(x,y)"} and returns the corresponding numeric coordinates. This function supports optional spaces and scientific notation. \texttt{Syntax: } \begin{center} \code{local x, y = utils.parse\_point("(1.5, -2.3)")} \end{center} \texttt{Purpose: } The function takes a string argument and parses it to extract the \code{x} and \code{y} components as numbers. The input string must follow the format \code{"(x, y)"} where \code{x} and \code{y} can be floating-point values written in decimal or scientific notation. \texttt{Arguments: } \code{str} – A string representing a point, e.g., \code{"(3.5, -2.0)"}. \texttt{Returns: } \code{x}, \code{y} – numeric coordinates as Lua numbers. Two numerical values: the real and imaginary parts of the point. \texttt{Features: } \begin{itemize} \item Accepts optional spaces around numbers and commas. \item Accepts scientific notation (\code{1e-2}, \code{3.4E+1}). \item Raises an error for invalid formats. \end{itemize} \texttt{Example usage: } \begin{verbatim} local x, y = utils.parse_point("(3.5, -2)") -- x = 3.5, y = -2.0 \end{verbatim} \texttt{Related functions: } \begin{itemize} \item \tkzFct{utils}{format\_point(z, decimals)} \item \tkzFct{utils}{format\_number(x, decimals)} \end{itemize} \subsection{Function \tkzFct{utils}{format\_number(x, decimals)}} \label{sub:function_utils_format_number} This function formats a numeric value (or a numeric string) into a string representation with a fixed number of decimal places. \texttt{Syntax :} \begin{center} \code{local str = utils.format\_number(math.pi, 3)} \end{center} \texttt{Purpose: } The function converts a number (or a string that can be converted to a number) into a string with the specified number of decimal digits. It is especially useful when generating clean numerical output for display or export to \TIKZ{} coordinates. \texttt{Arguments: } \begin{itemize} \item \code{x} – A number or a string convertible to a number. \item \code{decimals} – Optional. The number of decimal places (default is 5). \end{itemize} \texttt{Returns: } A string representing the value of \code{x} with the specified number of decimals. \texttt{Features: } \begin{itemize} \item Automatically converts strings to numbers if possible. \item Ensures consistent formatting for \TIKZ{} coordinates or LaTeX output. \item Raises an error if the input is not valid. \end{itemize} \texttt{Example usage: } \begin{verbatim} local a = utils.format_number(math.pi, 3) % a = "3.142" local b = utils.format_number("2.718281828", 2) % b = "2.72" \end{verbatim} \texttt{Error handling.} An error is raised if \code{x} is not a valid number or numeric string. \texttt{Related functions: } \begin{itemize} \item \tkzFct{utils}{to\_decimal\_string(x, decimals)} \item \tkzFct{utils}{format\_point(z, decimals)} \end{itemize} \subsection{Function \tkzFct{utils}{format\_coord(x, decimals)}} \label{sub:function_utils_format_coord} This function formats a numerical value into a string with a fixed number of decimal places. It is a lighter version of \tkzFct{utils}{format\_number}, intended for internal use when inputs are guaranteed to be numeric. \texttt{Syntax: } \begin{center} \code{local s = utils.format\_coord(3.14159, 2)} \hfill $\rightarrow$ \code{"3.14"} \end{center} \texttt{Arguments: } \begin{itemize} \item \code{x} – A number (not validated). \item \code{decimals} – Optional number of decimal places (default: 5). \end{itemize} \medskip \texttt{Returns: } A string with fixed decimal formatting. \medskip \texttt{Notes: } This function is used internally by \tkzFct{path}{add\_pair\_to\_path} and other path-building methods. Unlike \tkzFct{utils}{format\_number}, it does not perform input validation and should only be used with known numeric inputs. \medskip \texttt{Related functions: } \begin{itemize} \item \tkzFct{utils}{format\_number(x, decimals)} – safer alternative with validation \end{itemize} \subsection{Function \tkzFct{utils}{checknumber(x, decimals)}} \label{sub:function_utils_checknumber} Validates and converts a number or numeric string into a fixed-format decimal string. \medskip \texttt{Syntax: } \begin{center} \code{local s = utils.checknumber("2.71828", 4)} \hfill $\rightarrow$ \code{"2.7183"} \end{center} \texttt{Arguments: } \begin{itemize} \item \code{x} – A number or numeric string. \item \code{decimals} – Optional number of decimal digits (default: 5). \end{itemize} \medskip \texttt{Returns: } A formatted string representing the value rounded to the specified number of decimal places. \medskip \texttt{Remarks: } Used internally to validate input before formatting. Returns an error if the input is not convertible. \medskip \texttt{Related functions: } \begin{itemize} \item \tkzFct{utils}{format\_number} \end{itemize} \subsection{Function \tkzFct{utils}{format\_point(z, decimals)}} \label{sub:function_utils_format_point} Converts a complex point into a string representation suitable for coordinate output. \texttt{Syntax: } \begin{center} \code{local s = utils.format\_point(z, 4)} \hfill $\rightarrow$ \code{"(1.0000,2.0000)"} \end{center} \medskip \texttt{Arguments: } \begin{itemize} \item \code{z} – A table with fields \code{re} and \code{im}. \item \code{decimals} – Optional precision (default: 5). \end{itemize} \medskip \texttt{Returns: } A string representing the point as \code{"(x,y)"}. \medskip \texttt{Error handling.} Raises an error if \code{z} does not have numeric \code{re} and \code{im} components. \medskip \texttt{Related functions: } \begin{itemize} \item \tkzFct{utils}{format\_coord} \end{itemize} \subsection{Function \tkzFct{utils}{almost\_equal(a, b, epsilon)}} \label{sub:function_utils_almost_equal} Returns \code{true} if two numbers are approximately equal within a given tolerance. \medskip \texttt{Syntax: } \begin{center} \code{if utils.almost\_equal(x, y) then ... end} \end{center} \medskip \texttt{Arguments: } \begin{itemize} \item \code{a}, \code{b} – Two numbers to compare. \item \code{epsilon} – Optional tolerance (default: \code{tkz\_epsilon}). \end{itemize} \medskip \texttt{Returns: } A boolean: \code{true} if the values differ by less than the tolerance. \subsection{Function \tkzFct{utils}{wlog(...)}} \label{sub:function_utils_wlog} Logs a formatted message to the .log file only, with a \code{[tkz-elements]} prefix. \medskip \texttt{Syntax: } \begin{center} \code{utils.wlog("Internal value: \%s", tostring(value))} \end{center} \medskip \texttt{Returns: } No return value. Logging only. \endinput