octave-maintainers
[Top][All Lists]
Advanced

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

Re: cast to char


From: Richard
Subject: Re: cast to char
Date: Tue, 28 Jan 2014 09:00:45 +0000
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0

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 via email to

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