diff options
author | Karen Sargsyan <karsar@ibms.sinica.edu.tw> | 2016-05-21 18:00:59 +0800 |
---|---|---|
committer | Karen Sargsyan <karsar@ibms.sinica.edu.tw> | 2016-05-21 18:00:59 +0800 |
commit | 22527df1714c83e968913dabba4ba3ec81adc20c (patch) | |
tree | 9dafbbb36b0d25200b70a92e1aca8f6f0ae2f583 | |
parent | 02724f709fcafa9f4e854c271df6c2f79fd9cfa6 (diff) |
Some basic math functions: pow, sqrt, sin, cos, log, exp are added to work at client and server sides
-rw-r--r-- | include/urweb/urweb_cpp.h | 7 | ||||
-rw-r--r-- | lib/js/urweb.js | 18 | ||||
-rw-r--r-- | lib/ur/basis.urs | 7 | ||||
-rw-r--r-- | src/c/urweb.c | 24 | ||||
-rw-r--r-- | src/settings.sml | 8 | ||||
-rw-r--r-- | tests/math.ur | 14 |
6 files changed, 78 insertions, 0 deletions
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 <xml><body> + <button value="Power 2.0 of 2.0!" onclick={fn _ => alert (show (pow 2.0 2.0))}/> + {[(pow 2.0 2.0)]} + <button value="Square root of 25!" onclick={fn _ => alert (show (sqrt 25.0))}/> + {[(sqrt 25.0)]} + <button value="Sin of 0.1!" onclick={fn _ => alert (show (sin 0.1))}/> + {[(sin 0.1)]} + <button value="Cos of 0.1!" onclick={fn _ => alert (show (cos 0.1))}/> + {[(cos 0.1)]} + <button value="log of 0.1!" onclick={fn _ => alert (show (log 0.1))}/> + {[(log 0.1)]} + <button value="Exp of 0.1!" onclick={fn _ => alert (show (exp 0.1))}/> + {[(exp 0.1)]} + </body></xml> |