summaryrefslogtreecommitdiffstats
path: root/arith.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2012-03-22 10:48:00 -0700
committerKaz Kylheku <kaz@kylheku.com>2012-03-22 10:48:00 -0700
commit1ccc6d458fbda380233019a1d80d5aff576d9d03 (patch)
tree97e9057e446ac2e19f265ebe829634b46e81f79c /arith.c
parent946c88ae095260a816aae8e1d5eacb32e4424718 (diff)
downloadtxr-1ccc6d458fbda380233019a1d80d5aff576d9d03.tar.gz
txr-1ccc6d458fbda380233019a1d80d5aff576d9d03.tar.bz2
txr-1ccc6d458fbda380233019a1d80d5aff576d9d03.zip
Fix sqrt confusion. There must be a separate isqrt
for the integer square root. * arith.c (sqroot_fixnum): Renamed back to isqrt_fixnum. (sqroot): Rewritten to handle only floating-point square root. (isqrt): New function, based on previous sqroot, handles only integers. * eval.c (eval_init): New intrinsic, isqrt. * lib.h (isqrt): New declaration. * txr.1: Doc stubs. * txr.vim: Highlighting for isqrt.
Diffstat (limited to 'arith.c')
-rw-r--r--arith.c22
1 files changed, 10 insertions, 12 deletions
diff --git a/arith.c b/arith.c
index a820dc8e..27ac3faf 100644
--- a/arith.c
+++ b/arith.c
@@ -1225,7 +1225,7 @@ inval:
base, exp, mod, nao);
}
-static int_ptr_t sqroot_fixnum(int_ptr_t a)
+static int_ptr_t isqrt_fixnum(int_ptr_t a)
{
int_ptr_t mask = (int_ptr_t) 1 << (highest_bit(a) / 2);
int_ptr_t root = 0;
@@ -1239,7 +1239,7 @@ static int_ptr_t sqroot_fixnum(int_ptr_t a)
return root;
}
-val sqroot(val anum)
+val isqrt(val anum)
{
switch (type(anum)) {
case NUM:
@@ -1247,7 +1247,7 @@ val sqroot(val anum)
cnum a = c_num(anum);
if (a < 0)
goto negop;
- return num_fast(sqroot_fixnum(c_num(anum)));
+ return num_fast(isqrt_fixnum(c_num(anum)));
}
case BGNUM:
{
@@ -1256,20 +1256,13 @@ val sqroot(val anum)
goto negop;
return normalize(n);
}
- case FLNUM:
- {
- double a = c_flo(anum);
- if (a < 0)
- goto negop;
- return flo(sqrt(a));
- }
default:
break;
}
- uw_throwf(error_s, lit("sqrt: invalid operand ~s"), anum, nao);
+ uw_throwf(error_s, lit("isqrt: non-integer operand ~s"), anum, nao);
negop:
- uw_throw(error_s, lit("sqrt: negative operand"));
+ uw_throw(error_s, lit("isqrt: negative operand"));
}
val gcd(val anum, val bnum)
@@ -1326,6 +1319,11 @@ val loga(val num)
return flo(log(c_flo(to_float(lit("log"), num))));
}
+val sqroot(val num)
+{
+ return flo(sqrt(c_flo(to_float(lit("sqrt"), num))));
+}
+
/*
* TODO: replace this text-based hack!
*/