diff options
author | Adam Chlipala <adamc@csail.mit.edu> | 2016-05-24 11:41:11 -0400 |
---|---|---|
committer | Adam Chlipala <adamc@csail.mit.edu> | 2016-05-24 11:41:11 -0400 |
commit | f4c8df8e16524cf9ab7a9ab6ff6824ef5055b21c (patch) | |
tree | 7c7b838d73689bd6180ba532accde984c7e2d420 | |
parent | 02724f709fcafa9f4e854c271df6c2f79fd9cfa6 (diff) | |
parent | ae4778d7a3f4dacde39232b503bd134ce71444ee (diff) |
Merge pull request #26 from karsar/math_update
Some basic math functions: pow, sqrt, sin, cos, log, exp
-rw-r--r-- | include/urweb/urweb_cpp.h | 13 | ||||
-rw-r--r-- | lib/js/urweb.js | 42 | ||||
-rw-r--r-- | lib/ur/basis.urs | 15 | ||||
-rw-r--r-- | src/c/urweb.c | 48 | ||||
-rw-r--r-- | src/settings.sml | 13 | ||||
-rw-r--r-- | tests/math.ur | 26 |
6 files changed, 156 insertions, 1 deletions
diff --git a/include/urweb/urweb_cpp.h b/include/urweb/urweb_cpp.h index 5b6c6221..48cfa784 100644 --- a/include/urweb/urweb_cpp.h +++ b/include/urweb/urweb_cpp.h @@ -387,6 +387,19 @@ uw_Basis_float uw_Basis_floatFromInt(struct uw_context *, uw_Basis_int); 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_int uw_Basis_floor(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_float uw_Basis_asin(struct uw_context *, uw_Basis_float); +uw_Basis_float uw_Basis_acos(struct uw_context *, uw_Basis_float); +uw_Basis_float uw_Basis_atan(struct uw_context *, uw_Basis_float); +uw_Basis_float uw_Basis_atan2(struct uw_context *, uw_Basis_float, uw_Basis_float); +uw_Basis_float uw_Basis_abs(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); diff --git a/lib/js/urweb.js b/lib/js/urweb.js index 410a0e23..96a12637 100644 --- a/lib/js/urweb.js +++ b/lib/js/urweb.js @@ -116,6 +116,48 @@ 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); +} + +function asin(n){ + return Math.asin(n); +} +function acos(n){ + return Math.acos(n); +} + +function atan(n){ + return Math.atan(n); +} + +function atan2(n, m){ + return Math.atan2(n, m); +} + +function floor(n){ + return Math.floor(n); +} + +function abs(n){ + return Math.abs(n); +} // Time, represented as counts of microseconds since the epoch diff --git a/lib/ur/basis.urs b/lib/ur/basis.urs index 883cc5b1..1163daed 100644 --- a/lib/ur/basis.urs +++ b/lib/ur/basis.urs @@ -152,7 +152,20 @@ val float : int -> float val ceil : float -> int val trunc : float -> int val round : float -> int - +val floor : float -> int + +(** * Basic Math *) + +val sqrt : float -> float +val sin : float -> float +val cos : float -> float +val log : float -> float +val exp : float -> float +val asin : float -> float +val acos : float -> float +val atan : float -> float +val atan2 : float -> float -> float +val abs: float -> float (** * Time *) diff --git a/src/c/urweb.c b/src/c/urweb.c index c23366fb..6eb3c21c 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -4517,6 +4517,54 @@ uw_Basis_int uw_Basis_round(uw_context ctx, uw_Basis_float n) { return round(n); } +uw_Basis_int uw_Basis_floor(uw_context ctx, uw_Basis_float n) { + return floor(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_float uw_Basis_asin(uw_context ctx, uw_Basis_float n) { + return asin(n); +} + +uw_Basis_float uw_Basis_acos(uw_context ctx, uw_Basis_float n) { + return acos(n); +} + +uw_Basis_float uw_Basis_atan(uw_context ctx, uw_Basis_float n) { + return atan(n); +} + +uw_Basis_float uw_Basis_atan2(uw_context ctx, uw_Basis_float n, uw_Basis_float m) { + return atan2(n, m); +} + +uw_Basis_float uw_Basis_abs(uw_context ctx, uw_Basis_float n) { + return fabs(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..b72789df 100644 --- a/src/settings.sml +++ b/src/settings.sml @@ -335,6 +335,19 @@ val jsFuncsBase = basisM [("alert", "alert"), ("ceil", "ceil"), ("trunc", "trunc"), ("round", "round"), + ("floor", "floor"), + + ("pow", "pow"), + ("sqrt", "sqrt"), + ("sin", "sin"), + ("cos", "cos"), + ("log", "log"), + ("exp", "exp"), + ("asin", "asin"), + ("acos", "acos"), + ("atan", "atan"), + ("atan2", "atan2"), + ("abs", "abs"), ("now", "now"), ("timeToString", "showTime"), diff --git a/tests/math.ur b/tests/math.ur new file mode 100644 index 00000000..964b73e6 --- /dev/null +++ b/tests/math.ur @@ -0,0 +1,26 @@ +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)]} + <button value="asin of 0.1!" onclick={fn _ => alert (show (asin 0.1))}/> + {[(asin 0.1)]} + <button value="acos of 0.1!" onclick={fn _ => alert (show (acos 0.1))}/> + {[(acos 0.1)]} + <button value="atan of 0.1!" onclick={fn _ => alert (show (atan 0.1))}/> + {[(atan 0.1)]} + <button value="atan2 of 0.1 and -0.2!" onclick={fn _ => alert (show (atan2 0.1 (-0.2)))}/> + {[(atan2 0.1 (-0.2))]} + <button value="floor of 34.5!" onclick={fn _ => alert (show (floor 34.5))}/> + {[(floor 34.5)]} + <button value="abs of -10.0!" onclick={fn _ => alert (show (abs (-10.0)))}/> + {[(abs (-10.0))]} + </body></xml> |