Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Math constants to Python #1677

Merged
merged 7 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.7/library/math.html}{\color{DarkBlue}Python 3.7.17 Documentation}. Examples:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you want to go for the latest stable release? https://docs.python.org/3/library/math.html

Are you implementing all functions in the Python math library? How about math.isnan, or math.factorial. If any functions are missing, please create an issue where you list what needs to be done to make the implementation consistent with the specs.

\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 @@ -422,7 +422,7 @@
'call_cc(f)',
context.variant === Variant.EXPLICIT_CONTROL
? call_with_current_continuation
: (f: any) => {

Check warning on line 425 in src/createContext.ts

View workflow job for this annotation

GitHub Actions / build

'f' is defined but never used. Allowed unused args must match /^_/u
throw new Error('call_cc is only available in Explicit-Control variant')
}
)
Expand Down Expand Up @@ -779,8 +779,6 @@
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 @@
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
Loading