develooper Front page | perl.perl5.porters | Postings from December 2008

Re: [perl #61754] 'Data::Dumper' reports double value as string.

Thread Next
From:
=?utf-8?B?0JDQvdCw0YLQvtC70LjQuSDQk9GA0LjRiNCw0LXQsg==?=
Date:
December 31, 2008 12:46
Subject:
Re: [perl #61754] 'Data::Dumper' reports double value as string.
Message ID:
000a01c96b2b$d3a484b0$0300000a@zion
And what is a bug?

My only motivation to see numerical scalars without single quotes if this 
don't cause lost precision.
And every number is a string. But not every string is a number.

A thus examle of is ok  then big numbers dump with quotes
> C:\>perl -MData::Dumper -e "$Data::Dumper::Useperl=1; print Dumper
> 1234567890.0"
> $VAR1 = '1234567890';
>


Desired behaviour
> C:\>perl -MData::Dumper -e "$Data::Dumper::Useperl=1; print Dumper 
> 1234567890.0"
> $VAR1 = '1234567890';
> C:\>perl -MData::Dumper -e "print Dumper 1234567890.0"
> $VAR1 = 1234567890;

and

> C:\>perl -MData::Dumper -e "$Data::Dumper::Useperl=1; print Dumper 1.0"
> $VAR1 = 1;
> C:\>perl -MData::Dumper -e "print Dumper 1.0"
> $VAR1 = 1;
and for string

> C:\>perl -MData::Dumper -e "$Data::Dumper::Useperl=1; print Dumper '1.0'"
> $VAR1 = '1.0';
> C:\>perl -MData::Dumper -e "print Dumper '1.0'"
> $VAR1 = '1.0';

and may be

> C:\>perl -MData::Dumper -e "$Data::Dumper::Useperl=1; print Dumper ('1.5', 
> 1.5)"
> $VAR1 = 1.5;
> $VAR2 = 1.5;
> C:\>perl -MData::Dumper -e "print Dumper '1.0'"
> $VAR1 = '1.5';
> $VAR2 = 1.5;

and may be
> C:\>perl -MData::Dumper -e "$Data::Dumper::Useperl=1; print Dumper 
> ('1.5123456', 1.5123456)"
> $VAR1 = '1.5123456';
> $VAR2 = '1.5123456';
> C:\>perl -MData::Dumper -e "print Dumper '1.0'"
> $VAR1 = '1.5123456';
> $VAR2 = 1.5123456;

In last case xs implementation will be more correct

maybe change this string
 elsif ($val =~ /^(?:0|-?[1-9]\d{0,8})\z/) { # safe decimal number
      $out .= $val;
    }
on
 elsif ($val =~ /^(?:0|-?[1-9]\d{0,8})(?:\.\d*[1-9]\z/) { # safe decimal 
number

and appropriate change in xs, and tests

I can provide a patch for this change if anybody tell me how do it( I use 
svn, and may be can use git)

I think differences with perl and xs implementation will be less, and 
results will be more accurate and nothing will be broken. IMHO


Happy New Year.



----- Original Message ----- 
From: "reneeb via RT" <perlbug-followup@perl.org>
To: <0body0@rambler.ru>
Sent: Monday, December 29, 2008 5:27 PM
Subject: [perl #61754] 'Data::Dumper' reports double value as string.


> On Sa. 27. Dez. 2008, 02:40:54, grian wrote:
>> [Please enter your report here]
>>
>> $ perl -MData::Dumper -e'print Dumper(1.0)'
>> $VAR1 = '1';
>> must be:
>> $VAR1 = 1;
>>
>> ===Note single quotes===
>>
>
> Is this really a bug?

> You can see that even with the Perl implementation of Dumper it uses
> single quotes if the number has more than 9 digits. This is due to this
> line:
>
>    elsif ($val =~ /^(?:0|-?[1-9]\d{0,8})\z/) { # safe decimal number
>      $out .= $val;
>    }
>
> If you want to get rid of the single quotes for decimals that ends with
> ".0" you have to remove the differentiation of Perl < 5.6 and Perl > 5.6
> for the "DD_is_integer" definition in Dumper.xs.
>

>
> I know that the behaviour differs with Dumper.xs and Dumper.pm.
>
> This is current behaviour:
>
> C:\>perl -MData::Dumper -e "print Dumper 1.0"
> $VAR1 = '1';
>
> C:\>perl -MData::Dumper -e "print Dumper 1.01"
> $VAR1 = '1.01';
>
> C:\>perl -MData::Dumper -e "$Data::Dumper::Useperl=1; print Dumper 1.01"
> $VAR1 = '1.01';
>
> C:\>perl -MData::Dumper -e "$Data::Dumper::Useperl=1; print Dumper 1.0"
> $VAR1 = 1;
>
> C:\>perl -MData::Dumper -e "$Data::Dumper::Useperl=1; print Dumper
> 123456789.0"
> $VAR1 = 123456789;
>
> C:\>perl -MData::Dumper -e "$Data::Dumper::Useperl=1; print Dumper
> 1234567890.0"
> $VAR1 = '1234567890';
>


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About