oct file ; problem with argument checking

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

oct file ; problem with argument checking

arnaudl
Hello,

Using octave version = 3.8.0, I begin to play with octfiles.

I would check if the first argument of my function is an integer.

My toto.cc file contains :

DEFUN_DLD ( toto, args, nargout , "AIDE fonction toto")
{

     if ( ! args(0).is_integer_type() )
         error("Non integer value \n");

     const int dimension = args(0).int_value();
     octave_stdout << " DIMENSION " << dimension << std::endl;

}

When I run the .oct version of toto in octave with :

[x,y] = toto(2)

I get :

error: Non integer value
  DIMENSION 0

Obviously, that's not what I expect.

Being less ambitious, I check if my first arg is a scalar with
"is_scalar_type()" instead of "is_integer_type"

DEFUN_DLD ( toto, args, nargout , "AIDE fonction toto")
{

     if ( ! args(0).is_scalar_type() )
         error("Non scalar value \n");

     const int dimension = args(0).int_value();
     octave_stdout << " DIMENSION " << dimension << std::endl;

}

Runing

[x,y] = toto(2)

Then It works and I get as expected

  DIMENSION 2

But I want to be sure my scalar type is an integer one.

Please could you help me and tell me what's wrong in my integer type
checking with "is_integer_type" ?


Thanks for your help


Arnaud



_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: oct file ; problem with argument checking

Doug Stewart-4


On Fri, Sep 22, 2017 at 6:53 AM, arnaud Lejeune <[hidden email]> wrote:
Hello,

Using octave version = 3.8.0, I begin to play with octfiles.

I would check if the first argument of my function is an integer.

My toto.cc file contains :

DEFUN_DLD ( toto, args, nargout , "AIDE fonction toto")
{

    if ( ! args(0).is_integer_type() )
        error("Non integer value \n");

    const int dimension = args(0).int_value();
    octave_stdout << " DIMENSION " << dimension << std::endl;

}

When I run the .oct version of toto in octave with :

[x,y] = toto(2)

I get :

error: Non integer value
 DIMENSION 0

Obviously, that's not what I expect.

Being less ambitious, I check if my first arg is a scalar with "is_scalar_type()" instead of "is_integer_type"

DEFUN_DLD ( toto, args, nargout , "AIDE fonction toto")
{

    if ( ! args(0).is_scalar_type() )
        error("Non scalar value \n");

    const int dimension = args(0).int_value();
    octave_stdout << " DIMENSION " << dimension << std::endl;

}

Runing

[x,y] = toto(2)

Then It works and I get as expected

 DIMENSION 2

But I want to be sure my scalar type is an integer one.

Please could you help me and tell me what's wrong in my integer type checking with "is_integer_type" ?


Thanks for your help


Arnaud



_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave



I am not sure of my answer but you can try
help int8

[x,y] = toto(int8(2))


--
DASCertificate for 206392


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: oct file ; problem with argument checking

arnaudl
Le 22/09/2017 à 13:24, Doug Stewart a écrit :


On Fri, Sep 22, 2017 at 6:53 AM, arnaud Lejeune <[hidden email]> wrote:
Hello,

Using octave version = 3.8.0, I begin to play with octfiles.

I would check if the first argument of my function is an integer.

My toto.cc file contains :

DEFUN_DLD ( toto, args, nargout , "AIDE fonction toto")
{

    if ( ! args(0).is_integer_type() )
        error("Non integer value \n");

    const int dimension = args(0).int_value();
    octave_stdout << " DIMENSION " << dimension << std::endl;

}

When I run the .oct version of toto in octave with :

[x,y] = toto(2)

I get :

error: Non integer value
 DIMENSION 0

Obviously, that's not what I expect.

Being less ambitious, I check if my first arg is a scalar with "is_scalar_type()" instead of "is_integer_type"

DEFUN_DLD ( toto, args, nargout , "AIDE fonction toto")
{

    if ( ! args(0).is_scalar_type() )
        error("Non scalar value \n");

    const int dimension = args(0).int_value();
    octave_stdout << " DIMENSION " << dimension << std::endl;

}

Runing

[x,y] = toto(2)

Then It works and I get as expected

 DIMENSION 2

But I want to be sure my scalar type is an integer one.

Please could you help me and tell me what's wrong in my integer type checking with "is_integer_type" ?


Thanks for your help


Arnaud



_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave



I am not sure of my answer but you can try
help int8

[x,y] = toto(int8(2))


--
DASCertificate for 206392
ok

it works with int8(2) but isn't any member of "octave_value" that recognizes "2" as an integer when the user writes
[x,y] = toto( 2 )

Thanks for your help

Arnaud



_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: oct file ; problem with argument checking

Mike Miller-4
On Fri, Sep 22, 2017 at 13:52:20 +0200, arnaud Lejeune wrote:
> it works with int8(2) but isn't any member of "octave_value" that recognizes
> "2" as an integer when the user writes
> [x,y] = toto( 2 )

It sounds like want your function to accept floating point values that
happen to have integer values, is that correct? And you want to issue an
error when the floating point value is not exacty an integer?

If that's the case then you want to do something like the following

    double a = args(0).scalar_value ();
    if (a != std::floor (a))
      error ("myfunc: A must be an integer scalar value");
    int value = static_cast<int> (a);

If you are working with an older version of Octave, I think you'll need
a return statement after the error call.

--
mike

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

signature.asc (849 bytes) Download Attachment