summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorPaolo Bosetti <[email protected]>2012-06-04 15:56:51 -0700
committerPaolo Bosetti <[email protected]>2012-06-04 15:57:05 -0700
commit2c7cee8a1a7886691f5103a4319fca411408b500 (patch)
tree6b619ff387733a2b258abec3d37192886a631c43
parentab3c178a5f9742ff1fd6522c87b5cfd56e685e89 (diff)
downloadmruby-2c7cee8a1a7886691f5103a4319fca411408b500.tar.gz
mruby-2c7cee8a1a7886691f5103a4319fca411408b500.zip
Added Math.sqrt() that was missing in math.c, and added relevant test case
-rw-r--r--src/math.c20
-rw-r--r--test/t/math.rb10
2 files changed, 30 insertions, 0 deletions
diff --git a/src/math.c b/src/math.c
index b0d911573..eff2edcad 100644
--- a/src/math.c
+++ b/src/math.c
@@ -465,6 +465,25 @@ math_log10(mrb_state *mrb, mrb_value obj)
/*
* call-seq:
+ * Math.sqrt(numeric) -> float
+ *
+ * Returns the square root of <i>numeric</i>.
+ *
+ */
+static mrb_value
+math_sqrt(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = sqrt(x);
+
+ return mrb_float_value(x);
+}
+
+
+/*
+ * call-seq:
* Math.cbrt(numeric) -> float
*
* Returns the cube root of <i>numeric</i>.
@@ -646,6 +665,7 @@ mrb_init_math(mrb_state *mrb)
mrb_define_module_function(mrb, mrb_math, "log", math_log, -1);
mrb_define_module_function(mrb, mrb_math, "log2", math_log2, 1);
mrb_define_module_function(mrb, mrb_math, "log10", math_log10, 1);
+ mrb_define_module_function(mrb, mrb_math, "sqrt", math_sqrt, 1);
mrb_define_module_function(mrb, mrb_math, "cbrt", math_cbrt, 1);
mrb_define_module_function(mrb, mrb_math, "frexp", math_frexp, 1);
diff --git a/test/t/math.rb b/test/t/math.rb
index 5b9da4cb3..47a3bf527 100644
--- a/test/t/math.rb
+++ b/test/t/math.rb
@@ -76,6 +76,16 @@ assert('Math.log10 10**100') do
check_float(Math.log10(10**100), 100.0)
end
+assert('Math.sqrt') do
+ num = [0.0, 1.0, 2.0, 3.0, 4.0]
+ sqr = [0, 1, 4, 9, 16]
+ result = true
+ sqr.each_with_index do |v,i|
+ result &= check_float(Math.sqrt(v), num[i])
+ end
+ result
+end
+
assert('Math.cbrt') do
num = [-2.0, -1.0, 0.0, 1.0, 2.0]
cub = [-8, -1, 0, 1, 8]