octave-maintainers
[Top][All Lists]
Advanced

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

make_int.cc and binary ops


From: CdeMills
Subject: make_int.cc and binary ops
Date: Sun, 14 Jul 2013 14:32:54 -0700 (PDT)

Hello,

I'm puzzled by the constructs in example/make_int.cc:

class
octave_integer : public octave_base_value
{
public:
 // constructors, operations, ...
int integer_value const { return scalar; }
private:
 int scalar;
}
#define DEFBINOP_OP(name, t1, t2, op) \
  BINOPDECL (name, a1, a2) \
  { \
    CAST_BINOP_ARGS (const octave_ ## t1&, const octave_ ## t2&); \
    return octave_value \
      (new octave_integer (v1.t1 ## _value () op v2.t2 ## _value ())); \
  }

// integer by integer ops.
DEFBINOP_OP (add, integer, integer, +)

This expands to the following:
static octave_value oct_binop_add (const octave_base_value& a1, const
octave_base_value& a2) 
{ const octave_integer& v1 = dynamic_cast<const octave_integer&> (a1); 
const octave_integer& v2 = dynamic_cast<const octave_integer&> (a2); 
return octave_value (new octave_integer (v1.integer_value () +
v2.integer_value ())); 
}

 INSTALL_BINOP (op_add, octave_integer, octave_integer, add);
this works : if both arguments are of class octave_integer, the dynamic cast
octave_integer->octave_base_value->octave_integer works

 INSTALL_BINOP (op_add, octave_scalar, octave_integer, add);
This fails as a the dynamic cast
octave_scalar->octave_base_value->octave_integer is not possible. But the
INSTALL_BINOP  line is required if we want to add ordinary scalar to this
newly defined class.

It looks like some converter is missing: the binop implementation should be
static octave_value oct_binop_add (const octave_base_value& a1, const
octave_base_value& a2) {
octave_integer v1, v2,
if (a1 is octave_integer) then v1 = a1 else v1 = new
octave_integer(a1.int_value());
if (a2 is octave_integer) then v2 = a2 else v2 = new
octave_integer(a2.int_value());
return octave_value (new octave_integer (v1.integer_value () +
v2.integer_value ())); 
}

I know this code is redundant with int8, int16, and so on. I'm trying to
understand the mechanisms to apply them to multi-precision numbers. What I
would like to achieve is to have operations like
octave_scalar OP mp; mp OP mp; mp OP octave_scalar handled internally.

Regards

Pascal





--
View this message in context: 
http://octave.1599824.n4.nabble.com/make-int-cc-and-binary-ops-tp4655633.html
Sent from the Octave - Maintainers mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]