help-octave
[Top][All Lists]
Advanced

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

Re: How to convert octave_value to string?


From: Jaroslav Hajek
Subject: Re: How to convert octave_value to string?
Date: Wed, 3 Sep 2008 20:43:47 +0200

On Wed, Sep 3, 2008 at 5:03 PM, Tamara Bechtold
<address@hidden> wrote:
> Dear all,
> many thanks to your responses so far. Here is my code, that does not
> work properly:
>
> DEFUN_DLD (name, args, ,"[Ar,Br,Cr,HSV] =
> name(A,B,C,r/tol,method,-equil,-job ") {
> //note the 4th argument can be either int (I  call it r) or double (I
> call it tol)
> //and there should be difference if it is 1 or 1.0 as in each case
> different action should be performed
> octave_value_list retval;
> //now I want to read-in the arguments and try the following
> int r=0;
> double tol=0.0
> if (args(3).is_integer_type())
> {
> r= args(3).int_value();
> //call to function a(r)
> }
> if (args(3).is_double_type())
> {
> tol= args(3).double_value();
> //call to funtion b(tol)
> }
> However the function .is_integer_type() gives always false and the
> function .is_double_type() always true. I think this is because all
> numerical data in Octave are written in double precision. In other
> words, this functions do not do what I want them to.
> Hence, I came to the conclusion, that the best would be to somehow turn
> the input arguments into strings and search if there is a dot in it.
>
> Does anybody knows how?

Your problem is that in Octave,  when you write the constant `1', it
is a double value, not int. To get an int, you need to do, for
instance, `int32(1)'.
This is Matlab heritage from the days when Matlab had no integers; and
it's necessary for compatibility, but it's pain the ass because in
almost all other languages integers are the default and are converted
when they meet reals.

Note that in Matlab and Octave, int32(1) + 1 yields int32(2). Worse
yet, int32(2) * 0.3 yields (silently) int32(1) - i.e. first the int32
operand is promoted to double, the operation is calculated, and *the
result is converted back to int32*. Terrible.
I understand that the motivation was that something like `a = a+1'
should work for a an integer, but the price maybe too high.

Consider:

octave:1> class (1 + 1)
ans = double
octave:2> class (single(1) + 1)
ans = single
octave:3> class (int32(1) + 1)
ans = int32
octave:4> class (logical(1) + 1)
ans = double
octave:5> class (char(1) + 1)
ans = double

I bet that novices to both Matlab and Octave will think "wtf?" here...
yeah and watch this:
octave:6> class (logical(1) + char(1))
ans = double

amazing, right?

> Sincerely
> Tamara
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
>



-- 
RNDr. Jaroslav Hajek
computing expert
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz


reply via email to

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