From 7183dbc7f26bd51c169d3b346ce42b751f3b8fb0 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 4 Mar 2017 23:12:05 -0800 Subject: 3753 - new instruction: 'square-root' Thanks Lakshman Swaminathan for the request. --- 022arithmetic.cc | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/022arithmetic.cc b/022arithmetic.cc index 9ae5693b..7b74264f 100644 --- a/022arithmetic.cc +++ b/022arithmetic.cc @@ -761,6 +761,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") -- cgit 1.4.1-2-gfad0