Closed p5pRT closed 19 years ago
This is a bug report for perl from torsten-werner@assyst-intl.com\, generated with the help of perlbug 1.35 running under perl v5.8.4.
----------------------------------------------------------------- Posix::strtod at Windows has a problem if a large number of digits appears after decimal dot.
Example: G:\>perl use POSIX qw( strtod ); print join('\, '\,strtod("12.45006789"))\, "\n"; ^D 12\, 9
G:\>
Perl is able to create and handle this kind of numbers.
Just in the moment I use a own function. I post it inside a test program. It works for different decimal dots in different regional settings of Windows.
################ cut here #####################
use strict; use warnings;
BEGIN { if ($^O eq 'MSWin32') { require Win32::TieRegistry; } }
use POSIX qw( localeconv ); my $decimal = localeconv()->{decimal_point} || '.'; my $win_decimal=($^O eq 'MSWin32') ? $Win32::TieRegistry::Registry->{' \CUser\Control Panel\International\\sDecimal'} : undef;
sub strtod ($) { local $_=shift; my $dec_expr=''; my %chars; DECIMALS: foreach my $char ($decimal\, $win_decimal) { next DECIMALS if exists $chars{$char}; $dec_expr.=sprintf("\\x%x"\,ord($char)) if defined $char; $chars{$char}=1; }; $dec_expr="[$dec_expr]"; $dec_expr=qr/$dec_expr/; # replace ".123" by "0.123"\, the regexp for numbers is easier so: s/^([+-]{0\,1})$dec_expr/${1}0$decimal/; # find the leading number in 123.456 and 0.123456e3 notation: m/^((?-xism:[+-]{0\,1}\d+(?-xism:$dec_expr\d+){0\,1}(?i-xsm:e[+-]{0\,1} \d+){0\,1}){0\,1})(.*)$/; my $num=$1 ? $1 : 0; my $no_num=$2 ? $2 : ''; return ($num\,length($no_num)) if wantarray; return $num; }
sub show($) { my $string=shift; my ($num\, $no_num)=strtod($string); print "String: '$string' num: '$num'\, no_num: '$no_num'\n"; }
show 123 + 0.4560056789; show "123\,456.Hello"; show "Hallo"; show "-123"; show "-123."; show "+123"; show "+123."; show "+.2"; show ".2"; show "-.2"; show ".2e"; show ".2e3"; show ".2e.2"; show ".2e-.2"; show ".2E-2"; show "-.2e-.2"; show "-.2e+2"; show "-.2e+.2";
############## cut here ################
Torsten.Werner@assyst-intl.com (via RT) wrote:
Posix::strtod at Windows has a problem if a large number of digits appears after decimal dot.
Example: G:\>perl use POSIX qw( strtod ); print join('\, '\,strtod("12.45006789"))\, "\n"; ^D 12\, 9
Works fine for me:
C:\Temp>perl -MPOSIX -e "print join('\, '\,POSIX::strtod('12.45006789'));" 12.45006789\, 0
Just in the moment I use a own function. I post it inside a test program. It works for different decimal dots in different regional settings of Windows.
"Different decimal dots" could be the key there\, rather than the large number of digits after the decimal dot -- you're probably running in a locale other than English\, and that may be what's confusing POSIX::strtod(). For example\, if I use a "\," instead of a "." in the number then I get the same output as you:
C:\Temp>perl -MPOSIX -e "print join('\, '\,POSIX::strtod('12\,45006789'));" 12\, 9
However\, the POSIX::strtod() docs do say that it should respect any POSIX::setlocale() setting\, and if I change my locale to German then it does indeed work:
C:\Temp>perl -MPOSIX -e "setlocale(LC_ALL\, 'German'); print join('\, '\,POSIX::strtod('12\,45006789'));" 12\,45006789\, 0
So perhaps you just need to set the locale appropriately?
- Steve
This email has been scanned for viruses and content by the Radan Computational Webshield Appliances.
The RT System itself - Status changed from 'new' to 'open'
Hello\, you are right. With proper setlocale it works. Sorry for confusing. Torsten
"Steve Hay via
RT" To: Torsten.Werner@assyst-intl.com
\<perlbug-followup cc:
@perl.org> Subject: Re: [perl #32316] Bug in POSIX::strtod at Windows
04.11.2004 11:47
Please respond to
perlbug-followup
Torsten.Werner@assyst-intl.com (via RT) wrote:
Posix::strtod at Windows has a problem if a large number of digits appears after decimal dot.
Example: G:\>perl use POSIX qw( strtod ); print join('\, '\,strtod("12.45006789"))\, "\n"; ^D 12\, 9
Works fine for me:
C:\Temp>perl -MPOSIX -e "print join('\, '\,POSIX::strtod('12.45006789'));" 12.45006789\, 0
Just in the moment I use a own function. I post it inside a test program. It works for different decimal dots in different regional settings of Windows.
"Different decimal dots" could be the key there\, rather than the large number of digits after the decimal dot -- you're probably running in a locale other than English\, and that may be what's confusing POSIX::strtod(). For example\, if I use a "\," instead of a "." in the number then I get the same output as you:
C:\Temp>perl -MPOSIX -e "print join('\, '\,POSIX::strtod('12\,45006789'));" 12\, 9
However\, the POSIX::strtod() docs do say that it should respect any POSIX::setlocale() setting\, and if I change my locale to German then it does indeed work:
C:\Temp>perl -MPOSIX -e "setlocale(LC_ALL\, 'German'); print join('\, '\,POSIX::strtod('12\,45006789'));" 12\,45006789\, 0
So perhaps you just need to set the locale appropriately?
- Steve
This email has been scanned for viruses and content by the Radan Computational Webshield Appliances.
@iabyn - Status changed from 'open' to 'rejected'
Migrated from rt.perl.org#32316 (status was 'rejected')
Searchable as RT32316$