[seed] [mpfr] Added mpfr.div



commit 5738d1175fc4838440bc994cb156a9f48035887c
Author: Matt ARSENAULT <arsenm2 rpi edu>
Date:   Wed Jul 8 18:24:23 2009 -0400

    [mpfr] Added mpfr.div

 modules/mpfr/seed-mpfr-arithmetic.c |   61 +++++++++++++++++++++++++++++++++++
 modules/mpfr/seed-mpfr.c            |    1 +
 modules/mpfr/seed-mpfr.h            |    1 +
 3 files changed, 63 insertions(+), 0 deletions(-)
---
diff --git a/modules/mpfr/seed-mpfr-arithmetic.c b/modules/mpfr/seed-mpfr-arithmetic.c
index 053a6b6..53d9653 100644
--- a/modules/mpfr/seed-mpfr-arithmetic.c
+++ b/modules/mpfr/seed-mpfr-arithmetic.c
@@ -186,6 +186,67 @@ SeedValue seed_mpfr_mul (SeedContext ctx,
     return seed_value_from_int(ctx, ret, exception);
 }
 
+SeedValue seed_mpfr_div (SeedContext ctx,
+                         SeedObject function,
+                         SeedObject this_object,
+                         gsize argument_count,
+                         const SeedValue args[],
+                         SeedException * exception)
+{
+    mpfr_rnd_t rnd;
+    mpfr_ptr rop, op1, op2;
+    gdouble dop1, dop2;
+    gint ret;
+    seed_mpfr_t argt1, argt2;
+    /* only want 1 double argument. alternatively, could accept 2,
+       add those, and set from the result*/
+
+    CHECK_ARG_COUNT("mpfr.div", 3);
+
+    rop = seed_object_get_private(this_object);
+    rnd = seed_value_to_mpfr_rnd_t(ctx, args[2], exception);
+
+    argt1 = seed_mpfr_arg_type(ctx, args[0], exception);
+    argt2 = seed_mpfr_arg_type(ctx, args[1], exception);
+
+    if ( (argt1 & argt2) == SEED_MPFR_MPFR )
+    {
+        /* both mpfr_t */
+        op1 = seed_object_get_private(args[0]);
+        op2 = seed_object_get_private(args[1]);
+        ret = mpfr_div(rop, op1, op2, rnd);
+    }
+    else if ( (argt1 | argt2) == (SEED_MPFR_MPFR | SEED_MPFR_DOUBLE) )
+    {
+        /* a double and an mpfr_t. Figure out the order */
+        if ( argt1 == SEED_MPFR_MPFR )
+        {
+            op1 = seed_object_get_private(args[0]);
+            dop2 = seed_value_to_double(ctx, args[1], exception);
+            mpfr_div_d(rop, op1, dop2, rnd);
+        }
+        else
+        {
+            dop1 = seed_value_to_double(ctx, args[0], exception);
+            op2 = seed_object_get_private(args[1]);
+            mpfr_d_div(rop, dop1, op2, rnd);
+        }
+    }
+    else if ( (argt1 & argt2) == SEED_MPFR_DOUBLE )
+    {
+        /* 2 doubles. hopefully doesn't happen */
+        dop1 = seed_value_to_double(ctx, args[0], exception);
+        dop2 = seed_value_to_double(ctx, args[1], exception);
+        ret = mpfr_set_d(rop, dop1 / dop2, rnd);
+    }
+    else
+    {
+        TYPE_EXCEPTION("mpfr.div", "double or mpfr_t");
+    }
+
+    return seed_value_from_int(ctx, ret, exception);
+}
+
 SeedValue seed_mpfr_sqrt (SeedContext ctx,
                           SeedObject function,
                           SeedObject this_object,
diff --git a/modules/mpfr/seed-mpfr.c b/modules/mpfr/seed-mpfr.c
index 6c3aa5a..0944fd8 100644
--- a/modules/mpfr/seed-mpfr.c
+++ b/modules/mpfr/seed-mpfr.c
@@ -989,6 +989,7 @@ seed_static_function mpfr_funcs[] =
     {"add", seed_mpfr_add, 0},
     {"sub", seed_mpfr_sub, 0},
     {"mul", seed_mpfr_mul, 0},
+    {"div", seed_mpfr_div, 0},
     {"sqrt", seed_mpfr_sqrt, 0},
     {"rec_sqrt", seed_mpfr_rec_sqrt, 0},
     {"cbrt", seed_mpfr_cbrt, 0},
diff --git a/modules/mpfr/seed-mpfr.h b/modules/mpfr/seed-mpfr.h
index edacff7..c0fe920 100644
--- a/modules/mpfr/seed-mpfr.h
+++ b/modules/mpfr/seed-mpfr.h
@@ -89,6 +89,7 @@ DEF_SEED_MPFR_FUNC(seed_mpfr_rint_trunc);
 DEF_SEED_MPFR_FUNC(seed_mpfr_add);
 DEF_SEED_MPFR_FUNC(seed_mpfr_sub);
 DEF_SEED_MPFR_FUNC(seed_mpfr_mul);
+DEF_SEED_MPFR_FUNC(seed_mpfr_div);
 
 DEF_SEED_MPFR_FUNC(seed_mpfr_sin);
 DEF_SEED_MPFR_FUNC(seed_mpfr_cos);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]