summaryrefslogtreecommitdiff
path: root/tests/tsqrt.c
diff options
context:
space:
mode:
authorzimmerma <zimmerma@280ebfd0-de03-0410-8827-d642c229c3f4>2005-03-17 12:54:56 +0000
committerzimmerma <zimmerma@280ebfd0-de03-0410-8827-d642c229c3f4>2005-03-17 12:54:56 +0000
commite38d6f7f28c72e805b9ed8346c73770da7f1e1bf (patch)
tree53009b21c2d0813ec783ecb004b7a49ca3838d02 /tests/tsqrt.c
parent664a7ea825f48db68e7ee3e2cdff745a37a6aebb (diff)
downloadmpfr-e38d6f7f28c72e805b9ed8346c73770da7f1e1bf.tar.gz
added two property-tests
git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@3393 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'tests/tsqrt.c')
-rw-r--r--tests/tsqrt.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/tsqrt.c b/tests/tsqrt.c
index 3d6922015..d31330356 100644
--- a/tests/tsqrt.c
+++ b/tests/tsqrt.c
@@ -493,6 +493,65 @@ check_nan (void)
mpfr_clear (got);
}
+/* check that -1 <= x/sqrt(x^2+y^2) <= 1 for rounding to nearest or up */
+static void
+test_property1 (mp_prec_t p, mp_rnd_t r)
+{
+ mpfr_t x, y, z, t;
+
+ mpfr_init2 (x, p);
+ mpfr_init2 (y, p);
+ mpfr_init2 (z, p);
+ mpfr_init2 (t, p);
+
+ mpfr_random (x);
+ mpfr_random (y);
+ mpfr_mul (z, x, x, r);
+ mpfr_mul (t, x, x, r);
+ mpfr_add (z, z, t, r);
+ mpfr_sqrt (z, z, r);
+ mpfr_div (z, x, z, r);
+ if (mpfr_cmp_si (z, -1) < 0 || mpfr_cmp_ui (z, 1) > 0)
+ {
+ printf ("Error, -1 <= x/sqrt(x^2+y^2) <= 1 does not hold for r=%s\n",
+ mpfr_print_rnd_mode (r));
+ printf ("x="); mpfr_dump (x);
+ printf ("y="); mpfr_dump (y);
+ printf ("got "); mpfr_dump (z);
+ exit (1);
+ }
+
+ mpfr_clear (x);
+ mpfr_clear (y);
+ mpfr_clear (z);
+ mpfr_clear (t);
+}
+
+/* check sqrt(x^2) = x */
+static void
+test_property2 (mp_prec_t p, mp_rnd_t r)
+{
+ mpfr_t x, y;
+
+ mpfr_init2 (x, p);
+ mpfr_init2 (y, p);
+
+ mpfr_random (x);
+ mpfr_mul (y, x, x, r);
+ mpfr_sqrt (y, y, r);
+ if (mpfr_cmp (y, x))
+ {
+ printf ("Error, sqrt(x^2) = x does not hold for r=%s\n",
+ mpfr_print_rnd_mode (r));
+ printf ("x="); mpfr_dump (x);
+ printf ("got "); mpfr_dump (y);
+ exit (1);
+ }
+
+ mpfr_clear (x);
+ mpfr_clear (y);
+}
+
int
main (void)
{
@@ -501,6 +560,13 @@ main (void)
tests_start_mpfr ();
+ for (p = MPFR_PREC_MIN; p <= 128; p++)
+ {
+ test_property1 (p, GMP_RNDN);
+ test_property1 (p, GMP_RNDU);
+ test_property2 (p, GMP_RNDN);
+ }
+
check_diverse ("635030154261163106768013773815762607450069292760790610550915652722277604820131530404842415587328", 160, "796887792767063979679855997149887366668464780637");
special ();
check_nan ();