Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#55 PDL::Core::convert default return type

closed
None
5
2011-11-12
2011-08-23
Tim
No

The default datatype returned by PDL::Core::convert is 'byte' if the $newtype parameter is not one of long(), etc. This is not mentioned in the documentation. I recommend adding a small blurb about this in the Pod and barf when $newtype is not a PDL::Type object. Ultimately, it would be nice to have a parse method in PDL::Type that would allow a string corresponding to the ppforcetype field of the entry in @types to be converted to its enum value, but would probably be better put into a "wishlist."

<code>
use PDL;
my $pdl_bad = (random(4,4)*100)->convert('ushort');
my $pdl_good = (random(4,4)*100)->convert(ushort);
print 'bad type: ', $pdl_bad->type, "\n";
print 'good type: ', $pdl_good->type, "\n";
</code>

Under 'use warnings;', this will emit a warning of "Argument "ushort" isn't numeric in numeric eq (==) at /usr/local/lib/perl/5.10.1/PDL/Core.pm line 2237," but is not descriptive and gives no hint as to the actual behavior that occurred.

Diff of proposed changes against $PDL::Core::FOO_FOO_::VERSION = '1.56';

2222a2223,2225
> Default datatype is C<byte> unless $newtype is one of
> C<long()> etc.
>
2233c2236,2245
< $type = $type->enum if ref($type) eq 'PDL::Type';
---
>
> if(ref($type) eq 'PDL::Type')
> {
> $type = $type->enum;
> }
> else
> {
> barf '$newtype must be one of long(), etc'."\n";
> }

Thanks.

- Tim

Output of perldl -V

perlDL shell v1.354
PDL comes with ABSOLUTELY NO WARRANTY. For details, see the file
'COPYING' in the PDL distribution. This is free software and you
are welcome to redistribute it under certain conditions, see
the same file for details.

Summary of my PDL configuration

VERSION: PDL v2.4.9 (supports bad values)

$%PDL::Config = {
'BADVAL_PER_PDL' => '0',
'WITH_PROJ' => '0',
'FFTW_TYPE' => 'double',
'FFTW_LIBS' => [
'/lib',
'/usr/lib',
'/usr/local/lib'
],
'WITH_FFTW' => '0',
'GSL_LIBS' => undef,
'WITH_IO_BROWSER' => '0',
'PROJ_INC' => undef,
'WHERE_PLPLOT_INCLUDE' => undef,
'HTML_DOCS' => '1',
'SKIP_KNOWN_PROBLEMS' => '0',
'WHERE_PLPLOT_LIBS' => undef,
'WITH_3D' => '0',
'FFTW_INC' => [
'/usr/include/',
'/usr/local/include'
],
'WITH_POSIX_THREADS' => '1',
'POGL_VERSION' => '0.63',
'HIDE_TRYLINK' => '1',
'WITH_HDF' => '0',
'HDF_INC' => undef,
'POGL_WINDOW_TYPE' => 'glut',
'WITH_BADVAL' => '1',
'WITH_GD' => '0',
'FITS_LEGACY' => '1',
'WITH_SLATEC' => '1',
'BADVAL_USENAN' => '0',
'WITH_DEVEL_REPL' => '1',
'TEMPDIR' => '/tmp',
'PROJ_LIBS' => undef,
'USE_POGL' => '0',
'GD_LIBS' => undef,
'GSL_INC' => undef,
'GD_INC' => undef,
'WITH_GSL' => '0',
'OPTIMIZE' => undef,
'HDF_LIBS' => undef,
'MALLOCDBG' => {},
'WITH_MINUIT' => '1',
'WITH_PLPLOT' => '0',
'MINUIT_LIB' => undef
};
Summary of my perl5 (revision 5 version 10 subversion 1) configuration:

Platform:
osname=linux, osvers=2.6.24-29-server, archname=x86_64-linux-gnu-thread-multi
uname='linux crested 2.6.24-29-server #1 smp wed mar 16 19:04:28 utc 2011 x86_64 x86_64 x86_64 gnulinux '
config_args='-Dusethreads -Duselargefiles -Dccflags=-DDEBIAN -Dcccdlflags=-fPIC -Darchname=x86_64-linux-gnu -Dprefix=/usr -Dprivlib=/usr/share/perl/5.10 -Darchlib=/usr/lib/perl/5.10 -Dvendorprefix=/usr -Dvendorlib=/usr/share/perl5 -Dvendorarch=/usr/lib/perl5 -Dsiteprefix=/usr/local -Dsitelib=/usr/local/share/perl/5.10.1 -Dsitearch=/usr/local/lib/perl/5.10.1 -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3 -Dsiteman1dir=/usr/local/man/man1 -Dsiteman3dir=/usr/local/man/man3 -Dman1ext=1 -Dman3ext=3perl -Dpager=/usr/bin/sensible-pager -Uafs -Ud_csh -Ud_ualarm -Uusesfio -Uusenm -DDEBUGGING=-g -Doptimize=-O2 -Dplibpth=/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu -Duseshrplib -Dlibperl=libperl.so.5.10.1 -Dd_dosuid -des'
hint=recommended, useposix=true, d_sigaction=define
useithreads=define, usemultiplicity=define
useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
use64bitint=define, use64bitall=define, uselongdouble=undef
usemymalloc=n, bincompat5005=undef
Compiler:
cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
optimize='-O2 -g',
cppflags='-D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include'
ccversion='', gccversion='4.5.2', gccosandvers=''
intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
alignbytes=8, prototype=define
Linker and Libraries:
ld='cc', ldflags =' -fstack-protector -L/usr/local/lib'
libpth=/usr/local/lib /lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu /lib /usr/lib /lib64 /usr/lib64
libs=-lgdbm -lgdbm_compat -ldb -ldl -lm -lpthread -lc -lcrypt
perllibs=-ldl -lm -lpthread -lc -lcrypt
libc=, so=so, useshrplib=true, libperl=libperl.so.5.10.1
gnulibc_version='2.13'
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
cccdlflags='-fPIC', lddlflags='-shared -O2 -g -L/usr/local/lib -fstack-protector'

Discussion

  • Tim
    Tim
    2011-08-23

    Sorry, I didn't test my "fix" very well. This should be a little more robust and has the added benefit of allowing someone to pass in a string '0', '1', etc for the datatype. Though, I doubt that is much of a feature.

    2230a2234,2235
    > use Scalar::Util qw(looks_like_number);
    >
    2233a2239,2240
    > barf '$newtype must be one of long(), etc'."\n" unless looks_like_number($type) && pdl($type)->type > float();

    Thanks.

    - Tim

     
  • Chris Marshall
    Chris Marshall
    2011-08-23

    I would like to see some clearer documentation for the
    convert() routine. The current behavior is from a non-PDL::Type
    second argument in a numeric test against the pdl type as
    a number. Most [A-z] strings convert to 0 from atoi().

    I'm moving this to the Feature Request tracker as that
    seems to be where it best fits in. I'm not sure the best way
    forward but I recommend discussion on one of the PDL
    mailing lists to sort this out better. See this URL for
    signup information:

    http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

     
  • Chris Marshall
    Chris Marshall
    2011-11-12

    Clarified the convert() documentation and added a barf
    if the type argument was not a number. This appears to
    fix this problem.

     
  • Chris Marshall
    Chris Marshall
    2011-11-12

    • assigned_to: nobody --> marshallch
    • status: open --> closed