From 22527df1714c83e968913dabba4ba3ec81adc20c Mon Sep 17 00:00:00 2001 From: Karen Sargsyan Date: Sat, 21 May 2016 18:00:59 +0800 Subject: Some basic math functions: pow, sqrt, sin, cos, log, exp are added to work at client and server sides --- include/urweb/urweb_cpp.h | 7 +++++++ lib/js/urweb.js | 18 ++++++++++++++++++ lib/ur/basis.urs | 7 +++++++ src/c/urweb.c | 24 ++++++++++++++++++++++++ src/settings.sml | 8 ++++++++ tests/math.ur | 14 ++++++++++++++ 6 files changed, 78 insertions(+) create mode 100644 tests/math.ur diff --git a/include/urweb/urweb_cpp.h b/include/urweb/urweb_cpp.h index 5b6c6221..2d834568 100644 --- a/include/urweb/urweb_cpp.h +++ b/include/urweb/urweb_cpp.h @@ -388,6 +388,13 @@ uw_Basis_int uw_Basis_ceil(struct uw_context *, uw_Basis_float); uw_Basis_int uw_Basis_trunc(struct uw_context *, uw_Basis_float); uw_Basis_int uw_Basis_round(struct uw_context *, uw_Basis_float); +uw_Basis_float uw_Basis_pow(struct uw_context *, uw_Basis_float, uw_Basis_float); +uw_Basis_float uw_Basis_sqrt(struct uw_context *, uw_Basis_float); +uw_Basis_float uw_Basis_sin(struct uw_context *, uw_Basis_float); +uw_Basis_float uw_Basis_cos(struct uw_context *, uw_Basis_float); +uw_Basis_float uw_Basis_log(struct uw_context *, uw_Basis_float); +uw_Basis_float uw_Basis_exp(struct uw_context *, uw_Basis_float); + uw_Basis_string uw_Basis_atom(struct uw_context *, uw_Basis_string); uw_Basis_string uw_Basis_css_url(struct uw_context *, uw_Basis_string); uw_Basis_string uw_Basis_property(struct uw_context *, uw_Basis_string); diff --git a/lib/js/urweb.js b/lib/js/urweb.js index 410a0e23..7842775b 100644 --- a/lib/js/urweb.js +++ b/lib/js/urweb.js @@ -116,7 +116,25 @@ function pow(n, m) { return Math.pow(n, m); } +function sqrt(n){ + return Math.sqrt(n); +} + +function sin(n){ + return Math.sin(n); +} + +function cos(n){ + return Math.cos(n); +} + +function log(n){ + return Math.log(n); +} +function exp(n){ + return Math.exp(n); +} // Time, represented as counts of microseconds since the epoch var time_format = "%c"; diff --git a/lib/ur/basis.urs b/lib/ur/basis.urs index 883cc5b1..45a17eb1 100644 --- a/lib/ur/basis.urs +++ b/lib/ur/basis.urs @@ -153,6 +153,13 @@ val ceil : float -> int val trunc : float -> int val round : float -> int +(** * Basic Math *) + +val sqrt : float -> float +val sin : float -> float +val cos : float -> float +val log : float -> float +val exp : float -> float (** * Time *) diff --git a/src/c/urweb.c b/src/c/urweb.c index c23366fb..edf55f21 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -4517,6 +4517,30 @@ uw_Basis_int uw_Basis_round(uw_context ctx, uw_Basis_float n) { return round(n); } +uw_Basis_float uw_Basis_pow(uw_context ctx, uw_Basis_float n, uw_Basis_float m) { + return pow(n,m); +} + +uw_Basis_float uw_Basis_sqrt(uw_context ctx, uw_Basis_float n) { + return sqrt(n); +} + +uw_Basis_float uw_Basis_sin(uw_context ctx, uw_Basis_float n) { + return sin(n); +} + +uw_Basis_float uw_Basis_cos(uw_context ctx, uw_Basis_float n) { + return cos(n); +} + +uw_Basis_float uw_Basis_log(uw_context ctx, uw_Basis_float n) { + return log(n); +} + +uw_Basis_float uw_Basis_exp(uw_context ctx, uw_Basis_float n) { + return exp(n); +} + uw_Basis_string uw_Basis_atom(uw_context ctx, uw_Basis_string s) { char *p; diff --git a/src/settings.sml b/src/settings.sml index 85cab207..c0b5e0e6 100644 --- a/src/settings.sml +++ b/src/settings.sml @@ -336,6 +336,14 @@ val jsFuncsBase = basisM [("alert", "alert"), ("trunc", "trunc"), ("round", "round"), + ("pow", "pow"), + ("sqrt", "sqrt"), + ("sin", "sin"), + ("cos", "cos"), + ("log", "log"), + ("exp", "exp"), + + ("now", "now"), ("timeToString", "showTime"), ("htmlifyTime", "showTimeHtml"), diff --git a/tests/math.ur b/tests/math.ur new file mode 100644 index 00000000..8892fffe --- /dev/null +++ b/tests/math.ur @@ -0,0 +1,14 @@ +fun main () = return +