Skip to content

Commit

Permalink
Sample application to 'Numeric Functions and Conversion Functions'
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikt-ma committed Sep 10, 2020
1 parent 53b3ebe commit 2064f96
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 65 deletions.
7 changes: 3 additions & 4 deletions chapters/classes.tex
Expand Up @@ -1625,11 +1625,10 @@ \subsubsection{Type Conversion of Enumeration Values to String or Integer}\label

The type conversion function \lstinline!Integer(<expression of enumeration type>)! returns the ordinal number of the
enumeration value \lstinline!E.enumvalue!, to which the expression is evaluated,
where \lstinline!Integer(E.e1) =1, Integer(E.en) = n!, for an enumeration type
\lstinline!E=enumeration(e1, ..., en)!.
where \lstinline!Integer(E.e1) = 1!, \lstinline!Integer(E.en) = n!, for an enumeration type
\lstinline!E = enumeration(e1, ..., en)!.

\lstinline!String(E.enumvalue)! gives the string representation of the enumeration
value.
\lstinline!String(E.enumvalue)! gives the \lstinline!String! representation of the enumeration value.

\begin{example}
\lstinline!String(E.Small)! gives \lstinline!"Small"!.
Expand Down
141 changes: 80 additions & 61 deletions chapters/operatorsandexpressions.tex
Expand Up @@ -377,79 +377,98 @@ \subsection{Numeric Functions and Conversion Functions}\label{numeric-functions-
whereas the event-triggering mathematical functions are described in
\cref{event-triggering-mathematical-functions}.

\begin{longtable}{|p{4.5cm}|p{10cm}|} \hline
\endhead
\lstinline!abs(v)! & Is expanded into \lstinline!noEvent(if v >= 0 then v else -v)!. Argument v needs to be an Integer or Real expression.\\ \hline
\lstinline!sign(v)! & Is expanded into \lstinline!noEvent(if v>0 then 1 else if v<0 then -1 else 0)!. Argument v needs to be an Integer or Real
expression.\\ \hline
\lstinline!sqrt(v)! & Returns the square root of \lstinline!v if v>=0!, otherwise
an error occurs. Argument v needs to be an Integer or Real
expression.\\ \hline

\lstinline!Integer(e)! & Returns the ordinal number of the expression \lstinline!e! of
enumeration type that evaluates to the enumeration value \lstinline!E.enumvalue!,
where \lstinline!Integer(E.e1)=1!, \lstinline!Integer(E.en)=n!, for an enumeration type
\lstinline!E=enumeration(e1, ..., en)!. See also \cref{type-conversion-of-enumeration-values-to-string-or-integer}.\\ \hline
\lstinline!EnumTypeName(i)! &
For any enumeration type \lstinline!EnumTypeName!, returns the enumeration
value \lstinline!EnumTypeName!.e such that \lstinline!Integer(EnumTypeName.e) = i!. Refer to the definition of
\begin{operatordefinition}[abs]
\begin{synopsis}\begin{lstlisting}[language=synopsis]
abs(v)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Expands into \lstinline!noEvent(if v >= 0 then v else -v)!. Argument \lstinline!v! needs to be an \lstinline!Integer! or \lstinline!Real! expression.
\end{semantics}
\end{operatordefinition}

\begin{operatordefinition}[sign]
\begin{synopsis}\begin{lstlisting}[language=synopsis]
sign(v)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Expands into \lstinline!noEvent(if v>0 then 1 else if v<0 then -1 else 0)!. Argument \lstinline!v! needs to be an \lstinline!Integer! or \lstinline!Real! expression.
\end{semantics}
\end{operatordefinition}

\begin{operatordefinition}[sqrt]
\begin{synopsis}\begin{lstlisting}[language=synopsis]
sqrt(v)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Square root of \lstinline!v! if $\text{\lstinline!v!} \geq 0$, otherwise an error occurs. Argument \lstinline!v! needs to be an \lstinline!Integer! or \lstinline!Real! expression.
\end{semantics}
\end{operatordefinition}

\begin{operatordefinition}[Integer]
\begin{synopsis}\begin{lstlisting}[language=synopsis]
Integer(e)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Ordinal number of the expression \lstinline!e! of enumeration type that evaluates to the enumeration value \lstinline!E.enumvalue!, where \lstinline!Integer(E.e1) = 1!, \lstinline!Integer(E.en) = n!,
for an enumeration type \lstinline!E = enumeration(e1, ..., en)!. See also \cref{type-conversion-of-enumeration-values-to-string-or-integer}.
\end{semantics}
\end{operatordefinition}

\begin{operatordefinition}[<EnumTypeName>]
\begin{synopsis}\begin{lstlisting}[language=synopsis]
EnumTypeName(i)
\end{lstlisting}\end{synopsis}
\begin{semantics}
For any enumeration type \lstinline!EnumTypeName!, returns the enumeration value \lstinline!EnumTypeName.e! such that \lstinline!Integer(EnumTypeName.e) = i!. Refer to the definition of
\lstinline!Integer! above.

It is an error to attempt to convert values of i that do not correspond
to values of the enumeration type. See also \cref{type-conversion-of-integer-to-enumeration-values}.
\\ \hline

\begin{tabular}{@{}p{4.5cm}@{}}
\lstinline!String(b, <options>)!\\
\lstinline!String(i, <options>)!\\
\lstinline!String(r,!
\hspace*{1ex}\lstinline!significantDigits=d,!
\hspace*{1ex}\lstinline!<options>)!\\
\lstinline!String(r, format=s)!\\
\lstinline!String(e, <options>)!
\end{tabular}
&
Convert a scalar non-\lstinline!String! expression to a \lstinline!String! representation. The
first argument may be a \lstinline!Boolean b!, an \lstinline!Integer i!, a \lstinline!Real r! or an
\lstinline!Enumeration e! (\cref{type-conversion-of-enumeration-values-to-string-or-integer}). The other arguments must use named
arguments. The optional \textless{}options\textgreater{} are:

\lstinline!Integer minimumLength=0!: minimum length of the resulting string. If
necessary, the blank character is used to fill up unused space.

\lstinline!Boolean leftJustified = true!: if true, the converted result is left
justified in the string; if false it is right justified in the string.

For \lstinline!Real! expressions the output shall be according to the Modelica
grammar. \lstinline!Integer significantDigits!=6: defines the number of significant
digits in the result string.
It is an error to attempt to convert values of \lstinline!i! that do not correspond to values of the enumeration type. See also \cref{type-conversion-of-integer-to-enumeration-values}.
\end{semantics}
\end{operatordefinition}

\begin{operatordefinition}[String]
\begin{synopsis}\begin{lstlisting}[language=synopsis]
String(b, <options>)
String(i, <options>)
String(r, significantDigits=d, <options>)
String(r, format=s)
String(e, <options>)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Convert a scalar non-\lstinline!String! expression to a \lstinline!String! representation. The first argument may be a \lstinline!Boolean b!, an \lstinline!Integer i!, a \lstinline!Real r! or an
enumeration value \lstinline!e! (\cref{type-conversion-of-enumeration-values-to-string-or-integer}). The other arguments must use named arguments. For \lstinline!Real! expressions the output shall be according to the Modelica grammar.

The optional \lstinline!<options>! are:
\begin{itemize}
\item
\lstinline!Integer minimumLength = 0!: Minimum length of the resulting string. If necessary, the blank character is used to fill up unused space.
\item
\lstinline!Boolean leftJustified = true!: If true, the converted result is left justified in the string; if false it is right justified in the string.
\item
\lstinline!Integer significantDigits = 6!: Number of significant digits in the result string.
\end{itemize}

\begin{nonnormative}
Examples of \lstinline!Real! values formatted with 6 significant digits: \emph{12.3456}, \emph{0.0123456}, \emph{12345600}, \emph{1.23456E-10}.
\end{nonnormative}

The format string corresponding to options is:
The \lstinline!format! string corresponding to \lstinline!<options>! is:
\begin{itemize}
\item
for Reals: %\newline
\lstinline!(if leftJustified then "-" else "") + String(minimumLength)+"."+ String(signficantDigits)+"g"!,
For \lstinline!Real!: \lstinline!(if leftJustified then "-" else "") + String(minimumLength) + "." + String(signficantDigits) + "g"!
\item
for Integers: %\newline
\lstinline!(if leftJustified then "-" else "")+ String(minimumLength)+"d"!.
For \lstinline!Integer!: \lstinline!(if leftJustified then "-" else "") + String(minimumLength) + "d"!
\end{itemize}

Format string: According to ANSI-C the format string specifies one
conversion specifier (excluding the leading \%), may not contain length
modifiers, and may not use `\lstinline!*!' for width and/or precision. For all
numeric values the format specifiers `\lstinline!f!', `\lstinline!e!', `\lstinline!E!', `\lstinline!g!', `\lstinline!G!' are allowed. For
integral values it is also allowed to use the `\lstinline!d!', `\lstinline!i!', `\lstinline!o!', `\lstinline!x!', `\lstinline!X!', `\lstinline!u!', and
`\lstinline!c!' format specifiers (for non-integral values a tool may~round, truncate
or use a different format if the integer conversion characters are
used).

The x,X-formats (hexa-decimal)~and c (character) for Integers does not
lead to input~that agrees with the Modelica-grammar.\\ \hline
\end{longtable}
Form of the \lstinline!format! string: According to ANSI-C the format string specifies one conversion specifier (excluding the leading \%), may not contain length modifiers, and may not use
`\lstinline!*!' for width and/or precision. For all numeric values the format specifiers `\lstinline!f!', `\lstinline!e!', `\lstinline!E!', `\lstinline!g!', `\lstinline!G!' are allowed. For
integral values it is also allowed to use the `\lstinline!d!', `\lstinline!i!', `\lstinline!o!', `\lstinline!x!', `\lstinline!X!', `\lstinline!u!', and `\lstinline!c!' format specifiers (for
non-integral values a tool may round, truncate or use a different format if the integer conversion characters are used).

The `\lstinline!x!'/`\lstinline!X!' formats (hexa-decimal) and \lstinline!c! (character) for \lstinline!Integer! values give results that do not agree with the Modelica grammar.
\end{semantics}
\end{operatordefinition}


\subsubsection{Event Triggering Mathematical Functions}\label{event-triggering-mathematical-functions}

Expand Down

0 comments on commit 2064f96

Please sign in to comment.