Skip to content

Commit

Permalink
Add Math constants to Python (#1677)
Browse files Browse the repository at this point in the history
* Add Math constants to Python

* Update math constant names

* Update source_python_misc.tex

* Remove is_undefined from specs

* Add NaN and Infinity, rename math_pi

* Replace ECMAScript with Python documentation

* Use latest Python documentation
  • Loading branch information
JJtan2002 authored Apr 16, 2024
1 parent bbfcf01 commit 294be20
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
8 changes: 5 additions & 3 deletions docs/specs/source_python_math.tex
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ \subsection*{MATH Library}
\begin{itemize}
\item \href{https://sourceacademy.org/sicpjs/1.1.4\#p8}{\lstinline{math_}$\textit{name}$},
where $\textit{name}$ is any name specified in the
JavaScript
Python
\texttt{Math} library, see\\
\href{https://www.ecma-international.org/ecma-262/9.0/index.html\#sec-math-object}{\color{DarkBlue}ECMAScript Specification, Section 20.2}. Examples:
\href{https://docs.python.org/3.11/library/math.html}{\color{DarkBlue}Python 3.11.8 Documentation}. Examples:
\begin{itemize}
\item \verb#math_PI#: \textit{primitive}, refers to the mathematical constant $\pi$,
\item \verb#math_nan#: \textit{primitive}, refers to the NaN (``Not a Number'') value,
\item \verb#math_inf#: \textit{primitive}, refers to the Infinity value,
\item \verb#math_pi#: \textit{primitive}, refers to the mathematical constant $\pi$,
\item \verb#math_sqrt#\texttt{(n)}: \textit{primitive}, returns the square root of the \emph{number} \texttt{n}.
\end{itemize}
\end{itemize}
Expand Down
10 changes: 3 additions & 7 deletions docs/specs/source_python_misc.tex
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ \subsection*{MISC Library}
\item \verb#parse_int#\texttt{(s, i)}: \textit{primitive},
interprets the \emph{string} \texttt{s} as an integer, using the positive integer \texttt{i} as radix, and returns the respective value,
see \href{https://www.ecma-international.org/ecma-262/9.0/index.html\#sec-parseint-string-radix}{\color{DarkBlue}ECMAScript Specification, Section 18.2.5}.
\item \href{https://sourceacademy.org/sicpjs/2.4.3\#p6}{\texttt{undefined}},
\texttt{\href{https://www.ecma-international.org/ecma-262/9.0/index.html\#sec-value-properties-of-the-global-object-nan}{\color{DarkBlue}NaN}}, \texttt{\href{https://www.ecma-international.org/ecma-262/9.0/index.html\#sec-value-properties-of-the-global-object-infinity}{\color{DarkBlue}Infinity}}: \textit{primitive}, refer to JavaScript's undefined,
NaN (``Not a Number'') and Infinity values, respectively.
\item \href{https://sourceacademy.org/sicpjs/4.1.2\#p2}{\lstinline{is_boolean(x)}}, \href{https://sourceacademy.org/sicpjs/2.3.2\#p5}{\lstinline{is_float(x), is_int(x)}}
\href{https://sourceacademy.org/sicpjs/2.3.2\#p7}{\lstinline{is_string(x)}}, \href{https://sourceacademy.org/sicpjs/4.1.2\#p2}{\lstinline{is_undefined(x)}}, \verb#is_function#\texttt{(x)}: \textit{primitive}, returns \texttt{true} if the type of \texttt{x} matches the function name and \texttt{false} if it does not. Following
JavaScript, we specify that \verb#is_number# returns \texttt{true} for \texttt{NaN} and \texttt{Infinity}.
\href{https://sourceacademy.org/sicpjs/2.3.2\#p7}{\lstinline{is_string(x)}}, \verb#is_function#\texttt{(x)}: \textit{primitive}, returns \texttt{true} if the type of \texttt{x} matches the function name and \texttt{false} if it does not. We specify that \verb#is_float# returns \texttt{true} for \texttt{NaN} and \texttt{Infinity}.
\item \texttt{prompt(s)}: \textit{primitive}, pops up a window that displays the \emph{string} \texttt{s}, provides
an input line for the user to enter a text, a ``Cancel'' button and an ``OK'' button. The call of \texttt{prompt}
suspends execution of the program until one of the two buttons is pressed. If
Expand All @@ -25,9 +21,9 @@ \subsection*{MISC Library}
of any call of \texttt{error} aborts the running program immediately.
\item \href{https://sourceacademy.org/sicpjs/2.1.3\#footnote-2}{\texttt{error(x, s)}}: \textit{primitive}, displays the string \texttt{s}, followed by a space character, followed by the value \texttt{x} in the console\footnotemark[\value{footnote}] with error flag. The evaluation
of any call of \texttt{error} aborts the running program immediately.
\item \href{https://sourceacademy.org/sicpjs/3.3.4\#p24}{\lstinline{String(x)}}: \textit{primitive}, returns a string that represents\footnotemark[\value{footnote}] the value \texttt{x}.
\item \href{https://sourceacademy.org/sicpjs/3.3.4\#p24}{\lstinline{str(x)}}: \textit{primitive}, returns a string that represents\footnotemark[\value{footnote}] the value \texttt{x}.
\end{itemize}
All library functions can be assumed to run
in $O(1)$ time, except \texttt{print}, \texttt{error} and \texttt{String},
in $O(1)$ time, except \texttt{print}, \texttt{error} and \texttt{str},
which run in $O(n)$ time, where $n$ is
the size (number of components such as pairs) of their first argument.
9 changes: 7 additions & 2 deletions src/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,8 +779,6 @@ export const importBuiltins = (context: Context, externalBuiltIns: CustomBuiltIn
defineBuiltin(context, 'char_at(str, index)', misc.char_at)
defineBuiltin(context, 'arity(f)', misc.arity)
defineBuiltin(context, 'None', null)
defineBuiltin(context, 'NaN', NaN)
defineBuiltin(context, 'Infinity', Infinity)

// Binary operators
defineBuiltin(context, '__py_adder(x, y)', pylib.__py_adder)
Expand Down Expand Up @@ -830,6 +828,13 @@ export const importBuiltins = (context: Context, externalBuiltIns: CustomBuiltIn
defineBuiltin(context, 'math_tan(x)', pylib.math_tan)
defineBuiltin(context, 'math_tanh(x)', pylib.math_tanh)
defineBuiltin(context, 'math_trunc(x)', pylib.math_trunc)

// Math constants
defineBuiltin(context, 'math_e', Math.E)
defineBuiltin(context, 'math_inf', Infinity)
defineBuiltin(context, 'math_nan', NaN)
defineBuiltin(context, 'math_pi', Math.PI)
defineBuiltin(context, 'math_tau', Math.PI * 2)
}
}
}
Expand Down

0 comments on commit 294be20

Please sign in to comment.