cast to char

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

cast to char

msander
I have been working on tests for cast().  i have run into some
inconsistent behaviour with casting to char type.    casting any numeric
type to any other numeric type (and logical) seems well behaved.  In
cases where the input value is outside the range of the output type, the
output is set to the min/max of the output type as would be expected.

With cast to char, the behavior is different.  Here are a couple of
simple examples using direct type conversion:


Case 1: expect char 0  (got undefined). equivalent to cast ( int8(-128),
"char")
 >> x=int8(-128)
x = -128
 >> z=char(x)
warning: range error for conversion to character value
error: value on right hand side of assignment is undefined

Case 2: expect char 0 (got 0.  this one works as expected). Equivalent
to cast(single(-128),"char")
 >> x=single(-128)
x = -128
 >> z=char(x)
warning: range error for conversion to character value
z =
 >> single(z)  # use this to output a printable value
ans = 0
 >>

Case 3: expect char 255  Equivalent to cast(single(1000),"char")
 >> x=single(1000)
x =  1000
 >> z=char(x)
warning: range error for conversion to character value
z =
 >> single(z) # use this to output a printable value
ans = 0


My initial question is whether cast to char should work as it does for
numeric types?    If not, is there information available on how this
should work?    If so, I see a couple of changes that should be made:

1) ov-base-int.cc octave_base_int_scalar<T>::convert_to_str_internal()
should compute retval in all cases to eliminate undefined in Case 1.
2) dont return 0 for all out of range inputs.  The various
convert_to_str_internal() functions should handle ival < min char value
& ival > max char value as separate cases & return 0/255 in the case of
out of range inputs.
3) none of the numeric conversions report range errors.  are these
necessary for char conversion?


With these changes, cast to char would behave similarly to numeric
types.    Any guidance is appreciated.

Are any of the above bugs that i should file?

Mike S

Reply | Threaded
Open this post in threaded view
|

Re: cast to char

Richard Crozier
On 28/01/2014 02:24, mike sander wrote:

> I have been working on tests for cast().  i have run into some
> inconsistent behaviour with casting to char type.    casting any numeric
> type to any other numeric type (and logical) seems well behaved.  In
> cases where the input value is outside the range of the output type, the
> output is set to the min/max of the output type as would be expected.
>
> With cast to char, the behavior is different.  Here are a couple of
> simple examples using direct type conversion:
>
>
> Case 1: expect char 0  (got undefined). equivalent to cast ( int8(-128),
> "char")
>  >> x=int8(-128)
> x = -128
>  >> z=char(x)
> warning: range error for conversion to character value
> error: value on right hand side of assignment is undefined
>
> Case 2: expect char 0 (got 0.  this one works as expected). Equivalent
> to cast(single(-128),"char")
>  >> x=single(-128)
> x = -128
>  >> z=char(x)
> warning: range error for conversion to character value
> z =
>  >> single(z)  # use this to output a printable value
> ans = 0
>  >>
>
> Case 3: expect char 255  Equivalent to cast(single(1000),"char")
>  >> x=single(1000)
> x =  1000
>  >> z=char(x)
> warning: range error for conversion to character value
> z =
>  >> single(z) # use this to output a printable value
> ans = 0
>
>


For info, Matlab R2013a returns:

 >> x=int8(-128)

x =

  -128

 >> z=char(x)
Warning: Out of range or non-integer values truncated during conversion
to character.
(Type "warning off MATLAB:nonIntegerTruncatedInConversionToChar" to
suppress this warning.)

z =



 >> x=single(-128)

x =

   -128

 >> z=char(x)
Warning: Out of range or non-integer values truncated during conversion
to character.
(Type "warning off MATLAB:nonIntegerTruncatedInConversionToChar" to
suppress this warning.)

z =



 >> single(z)

ans =

      0

 >> x=single(1000)

x =

         1000

 >> z=char(x)

z =

?

 >> single(z)

ans =

         1000

 >>


Richard


--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

Reply | Threaded
Open this post in threaded view
|

Re: cast to char

msander
On 14-01-28 04:00 AM, Richard wrote:
On 28/01/2014 02:24, mike sander wrote:
I have been working on tests for cast().  i have run into some
inconsistent behaviour with casting to char type.    casting any numeric
type to any other numeric type (and logical) seems well behaved.  In
cases where the input value is outside the range of the output type, the
output is set to the min/max of the output type as would be expected.

With cast to char, the behavior is different.  Here are a couple of
simple examples using direct type conversion:


Case 1: expect char 0  (got undefined). equivalent to cast ( int8(-128),
"char")
 >> x=int8(-128)
x = -128
 >> z=char(x)
warning: range error for conversion to character value
error: value on right hand side of assignment is undefined

Case 2: expect char 0 (got 0.  this one works as expected). Equivalent
to cast(single(-128),"char")
 >> x=single(-128)
x = -128
 >> z=char(x)
warning: range error for conversion to character value
z =
 >> single(z)  # use this to output a printable value
ans = 0
 >>

Case 3: expect char 255  Equivalent to cast(single(1000),"char")
 >> x=single(1000)
x =  1000
 >> z=char(x)
warning: range error for conversion to character value
z =
 >> single(z) # use this to output a printable value
ans = 0




For info, Matlab R2013a returns:

>> x=int8(-128)

x =

 -128

>> z=char(x)
Warning: Out of range or non-integer values truncated during conversion to character.
(Type "warning off MATLAB:nonIntegerTruncatedInConversionToChar" to suppress this warning.)

z =

This looks like 0.  Consistent with what i expected (but did not get from Octave).  Truncate negative to zero.


>> x=single(-128)

x =

  -128

>> z=char(x)
Warning: Out of range or non-integer values truncated during conversion to character.
(Type "warning off MATLAB:nonIntegerTruncatedInConversionToChar" to suppress this warning.)

z =



>> single(z)

ans =

     0

Consistent with what i did expect (and get from Octave).  negative is truncated to zero.
>> x=single(1000)

x =

        1000

>> z=char(x)

z =

?

>> single(z)

ans =

        1000

>>
Interesting.  No warning. No truncation.  maybe char is larger than 8 bit?   looking at matlab docs for char:

Description

S = char(X) converts array X of nonnegative integer codes into a character array. Valid codes range from 0 to 65535, where codes 0 through 127 correspond to 7-bit ASCII characters. The characters that MATLAB® can process (other than 7-bit ASCII characters) depend upon your current locale setting. To convert characters into a numeric array, use the double function.

Internally Octave uses unsigned 8 bit values (as far as I can tell)  for char.  At a minimum it should truncate larger values to 255.   I will try to patch.


Richard



Reply | Threaded
Open this post in threaded view
|

Re: cast to char

Richard Crozier
On 28/01/2014 12:30, mike sander wrote:
> On 14-01-28 04:00 AM, Richard wrote:
>> On 28/01/2014 02:24, mike sander wrote:

>>
>> x =
>>
>>         1000
>>
>> >> z=char(x)
>>
>> z =
>>
>> ?
>>

Incidentally, the question mark here is not the real character returned
by Matlab, it has been changed in the copy/paste process (test done on a
remote virtual machine).

Richard

--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.