about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2017-03-04 23:12:05 -0800
committerKartik K. Agaram <vc@akkartik.com>2017-03-04 23:12:05 -0800
commit7183dbc7f26bd51c169d3b346ce42b751f3b8fb0 (patch)
tree83ac3d9d8cf8a96bb0666e0eab91d8dc586c9231
parentb5f2a629c2617fc08a3b6d3fcc7d40f3974c8217 (diff)
downloadmu-7183dbc7f26bd51c169d3b346ce42b751f3b8fb0.tar.gz
3753 - new instruction: 'square-root'
Thanks Lakshman Swaminathan for the request.
-rw-r--r--022arithmetic.cc23
1 files changed, 23 insertions, 0 deletions
diff --git a/022arithmetic.cc b/022arithmetic.cc
index 9ae5693b..7b74264f 100644
--- a/022arithmetic.cc
+++ b/022arithmetic.cc
@@ -762,6 +762,29 @@ def main [
 +mem: storing -12 in location 1
 
 :(before "End Primitive Recipe Declarations")
+SQUARE_ROOT,
+:(before "End Primitive Recipe Numbers")
+put(Recipe_ordinal, "square-root", SQUARE_ROOT);
+:(before "End Primitive Recipe Checks")
+case SQUARE_ROOT: {
+  if (SIZE(inst.ingredients) != 1) {
+    raise << maybe(get(Recipe, r).name) << "'square-root' requires exactly one ingredient, but got '" << inst.original_string << "'\n" << end();
+    break;
+  }
+  if (!is_mu_number(inst.ingredients.at(0))) {
+    raise << maybe(get(Recipe, r).name) << "first ingredient of 'square-root' should be a number, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
+    break;
+  }
+  break;
+}
+:(before "End Primitive Recipe Implementations")
+case SQUARE_ROOT: {
+  products.resize(1);
+  products.at(0).push_back(sqrt(ingredients.at(0).at(0)));
+  break;
+}
+
+:(before "End Primitive Recipe Declarations")
 CHARACTER_TO_CODE,
 :(before "End Primitive Recipe Numbers")
 put(Recipe_ordinal, "character-to-code", CHARACTER_TO_CODE);