Ejemplo n.º 1
0
void
mpc_and (mpc_t *rop, mpc_t op1, mpc_t op2)
{
  /* Little did I realize that I would eventually make it
   * an error to apply bitwise operators to floating point
   * variables.
   */
  mpc_t temp;
  
  temp.precision = (op1.precision < op2.precision) ? op2.precision : op1.precision;
  mpz_init (temp.object);

  if (op1.precision == op2.precision)
    mpz_and (temp.object, op1.object, op2.object);
  else if (op1.precision > op2.precision)
    {
      mpz_set (temp.object, op2.object);
      power_of_ten (temp.object, op1.precision - op2.precision);
      mpz_and (temp.object, op1.object, temp.object);
    }
  else
    {
      mpz_set (temp.object, op1.object);
      power_of_ten (temp.object, op2.precision - op1.precision);
      mpz_and (temp.object, temp.object, op2.object);
    }

  rop->precision = temp.precision;
  mpz_set (rop->object, temp.object);
}
Ejemplo n.º 2
0
void
mpc_add (mpc_t *rop, mpc_t op1, mpc_t op2)
{
  mpc_t temp;
  
  temp.precision = (op1.precision < op2.precision) ? op2.precision : op1.precision;
  mpz_init (temp.object);

  if (op1.precision == op2.precision)
    mpz_add (temp.object, op1.object, op2.object);
  else if (op1.precision > op2.precision)
    {
      mpz_set (temp.object, op2.object);
      power_of_ten (temp.object, op1.precision - op2.precision);
      mpz_add (temp.object, temp.object, op1.object);
    }
  else
    {
      mpz_set (temp.object, op1.object);
      power_of_ten (temp.object, op2.precision - op1.precision);
      mpz_add (temp.object, temp.object, op2.object);
    }

  rop->precision = temp.precision;
  mpz_set (rop->object, temp.object);
}
Ejemplo n.º 3
0
signed long int
mpc_get_si (mpc_t rop)
{
  mpz_t temp1;
  mpz_t temp2;

  mpz_init_set (temp1, rop.object);
  mpz_init_set_ui (temp2, 1);
  power_of_ten (temp2, rop.precision);
  mpz_tdiv_q (temp1, temp1, temp2);

  return mpz_get_si (temp1);
}
Ejemplo n.º 4
0
int
mpc_cmp (mpc_t op1, mpc_t op2)
{
  mpz_t temp;

  if (op1.precision == op2.precision)
    return mpz_cmp (op1.object, op2.object);
  else if (op1.precision > op2.precision)
    {
      mpz_init_set (temp, op2.object);
      power_of_ten (temp, op1.precision - op2.precision);

      return mpz_cmp (op1.object, temp);
    }
  else
    {
      mpz_init_set (temp, op1.object);
      power_of_ten (temp, op2.precision - op1.precision);

      return mpz_cmp (temp, op2.object);
    }
}
Ejemplo n.º 5
0
INT32S string_to_number(char *array)
{
	INT8U i,j;
	INT32S value = 0;

	for(i = 0 ; array[i] != 0 ; i++);
	j = i - 1;
	while(i)
	{
		i--;
		value += (array[i] - '0')*power_of_ten(j-i);
	}

	return value;
}
Ejemplo n.º 6
0
char *
mpc_get_str (mpc_t op)
{
  char *return_value;
  mpz_t temp_op;
  mpz_t temp1;
  mpz_t temp2;

  if (op.precision == 0)
    return_value = mpz_get_str (NULL, 10, op.object);
  else
    {
      mpz_init (temp_op);
      mpz_abs (temp_op, op.object);
      
      mpz_init_set (temp1, temp_op);
      mpz_init_set_ui (temp2, 1);

      power_of_ten (temp2, op.precision);
      mpz_tdiv_q (temp1, temp1, temp2);

      return_value = concatinate_free (mpz_get_str (NULL, 10, temp1), ".", true, false);

      mpz_mul (temp1, temp1, temp2);
      mpz_sub (temp1, temp_op, temp1);

      mpz_abs (temp1, temp1);

      mpz_tdiv_q_ui (temp2, temp2, 10);

      while (mpz_cmp (temp2, temp1) > 0)
	{
	  return_value = concatinate_free (return_value, "0", true, false);
	  mpz_tdiv_q_ui (temp2, temp2, 10);
	}

      return_value = concatinate_free (return_value, mpz_get_str (NULL, 10, temp1), true, true);

      if (mpz_sgn (op.object) < 0)
	return_value = concatinate_free ("-", return_value, false, true);

      mpz_clear (temp1);
      mpz_clear (temp2);
      mpz_clear (temp_op);
    }

  return return_value;
}
Ejemplo n.º 7
0
/*NUMPY_API
 * Round
 */
NPY_NO_EXPORT PyObject *
PyArray_Round(PyArrayObject *a, int decimals, PyArrayObject *out)
{
    PyObject *f, *ret = NULL, *tmp, *op1, *op2;
    int ret_int=0;
    PyArray_Descr *my_descr;
    if (out && (PyArray_SIZE(out) != PyArray_SIZE(a))) {
        PyErr_SetString(PyExc_ValueError,
                        "invalid output shape");
        return NULL;
    }
    if (PyArray_ISCOMPLEX(a)) {
        PyObject *part;
        PyObject *round_part;
        PyObject *arr;
        int res;

        if (out) {
            arr = (PyObject *)out;
            Py_INCREF(arr);
        }
        else {
            arr = PyArray_Copy(a);
            if (arr == NULL) {
                return NULL;
            }
        }

        /* arr.real = a.real.round(decimals) */
        part = PyObject_GetAttrString((PyObject *)a, "real");
        if (part == NULL) {
            Py_DECREF(arr);
            return NULL;
        }
        part = PyArray_EnsureAnyArray(part);
        round_part = PyArray_Round((PyArrayObject *)part,
                                   decimals, NULL);
        Py_DECREF(part);
        if (round_part == NULL) {
            Py_DECREF(arr);
            return NULL;
        }
        res = PyObject_SetAttrString(arr, "real", round_part);
        Py_DECREF(round_part);
        if (res < 0) {
            Py_DECREF(arr);
            return NULL;
        }

        /* arr.imag = a.imag.round(decimals) */
        part = PyObject_GetAttrString((PyObject *)a, "imag");
        if (part == NULL) {
            Py_DECREF(arr);
            return NULL;
        }
        part = PyArray_EnsureAnyArray(part);
        round_part = PyArray_Round((PyArrayObject *)part,
                                   decimals, NULL);
        Py_DECREF(part);
        if (round_part == NULL) {
            Py_DECREF(arr);
            return NULL;
        }
        res = PyObject_SetAttrString(arr, "imag", round_part);
        Py_DECREF(round_part);
        if (res < 0) {
            Py_DECREF(arr);
            return NULL;
        }
        return arr;
    }
    /* do the most common case first */
    if (decimals >= 0) {
        if (PyArray_ISINTEGER(a)) {
            if (out) {
                if (PyArray_AssignArray(out, a,
                            NULL, NPY_DEFAULT_ASSIGN_CASTING) < 0) {
                    return NULL;
                }
                Py_INCREF(out);
                return (PyObject *)out;
            }
            else {
                Py_INCREF(a);
                return (PyObject *)a;
            }
        }
        if (decimals == 0) {
            if (out) {
                return PyObject_CallFunction(n_ops.rint, "OO", a, out);
            }
            return PyObject_CallFunction(n_ops.rint, "O", a);
        }
        op1 = n_ops.multiply;
        op2 = n_ops.true_divide;
    }
    else {
        op1 = n_ops.true_divide;
        op2 = n_ops.multiply;
        decimals = -decimals;
    }
    if (!out) {
        if (PyArray_ISINTEGER(a)) {
            ret_int = 1;
            my_descr = PyArray_DescrFromType(NPY_DOUBLE);
        }
        else {
            Py_INCREF(PyArray_DESCR(a));
            my_descr = PyArray_DESCR(a);
        }
        out = (PyArrayObject *)PyArray_Empty(PyArray_NDIM(a), PyArray_DIMS(a),
                                             my_descr,
                                             PyArray_ISFORTRAN(a));
        if (out == NULL) {
            return NULL;
        }
    }
    else {
        Py_INCREF(out);
    }
    f = PyFloat_FromDouble(power_of_ten(decimals));
    if (f == NULL) {
        return NULL;
    }
    ret = PyObject_CallFunction(op1, "OOO", a, f, out);
    if (ret == NULL) {
        goto finish;
    }
    tmp = PyObject_CallFunction(n_ops.rint, "OO", ret, ret);
    if (tmp == NULL) {
        Py_DECREF(ret);
        ret = NULL;
        goto finish;
    }
    Py_DECREF(tmp);
    tmp = PyObject_CallFunction(op2, "OOO", ret, f, ret);
    if (tmp == NULL) {
        Py_DECREF(ret);
        ret = NULL;
        goto finish;
    }
    Py_DECREF(tmp);

 finish:
    Py_DECREF(f);
    Py_DECREF(out);
    if (ret_int) {
        Py_INCREF(PyArray_DESCR(a));
        tmp = PyArray_CastToType((PyArrayObject *)ret,
                                 PyArray_DESCR(a), PyArray_ISFORTRAN(a));
        Py_DECREF(ret);
        return tmp;
    }
    return ret;
}
Ejemplo n.º 8
0
void power_of_ten_tests(void) {
  CU_ASSERT_EQUAL(power_of_ten(1398), 3);
  CU_ASSERT_EQUAL(power_of_ten(139.8), 2);
  CU_ASSERT_EQUAL(power_of_ten(13.98), 1);
  CU_ASSERT_EQUAL(power_of_ten(1.398), 0);
}