Newbie quesion: Scientific notation

PERL

    Next

  • 1. 2 examples of packages 1 has error the other does not
    I'm writing and testing a module that can be executed as a standalone script for debugging purposes. It's part of a very large system of regression testing. Messages are lost along the way. So with the caller() function I transform a module into an executable. With example #1 my $x seems to have lost it's scope. What I'm worried about, is what is goinf to happen when in the big system it does a : use foo; or require "foo.pm"; example #1 #!/usr/local/bin/perl use strict; use warnings; if ( !defined caller ) { foo::bar();} package foo; my $x = 'foo.bar'; sub bar { print "$x\n"; } <<<<------ line 13 blank lines removed 1; executing gives this : Use of uninitialized value in concatenation (.) or string at z line 13. example #2 works fine #!/usr/local/bin/perl package foo; use strict; use warnings; my $x = 'foo.bar'; sub bar { print "$x\n"; } if ( !defined caller ) { # enter package main package main; use strict; use warnings; foo::bar(); } # leave package main 1;
  • 2. Search and Replace with perl
    Hi, I have a file with entries like ======================================= _RWSTD_VALUE_ALLOC(test1, test5); _RWSTD_VALUE_ALLOC(test1,snm); _RWSTD_VALUE_ALLOC(xyz, 789); _RWSTD_VALUE_ALLOC(test1,sbk); _RWSTD_VALUE_ALLOC(abc, 123); : =============================== I want to put 2nd argument to _RWSTD_VALUE_ALLOC as *this . So my resultant file should be ======================================= _RWSTD_VALUE_ALLOC(test1,*this, test5); _RWSTD_VALUE_ALLOC(test1,snm); _RWSTD_VALUE_ALLOC(xyz,*this, 789); _RWSTD_VALUE_ALLOC(test1,sbk); _RWSTD_VALUE_ALLOC(abc,*this, 123); : =============================== Please help me to write this script....
  • 3. improper return value from net::ping?
    Help! I have a script that does a pingprobe on a list of servers. I works fine with perl v5.6.1, but does not work with 5.8.8. It looks like the return value from net::ping has changed. In 5.6.1 I always get a return of either 1 or 0. In 5.8.8 I get a 1 or 0 with a bunch of other data including the ip being pinged appended, meaning my return value is alway going to be non-0. As a result a simple test like use Net::Ping; my $p = Net::Ping->new("icmp"); print $p->ping("somehost"); $p->close(); is never going to work. Why the change in the return value, and how do I fix it?
  • 4. How to erase Hex value 13 from string?
    How to erase Hex value 13 from string? Perl line : $b =~ tr/\013//; dont work?! Thanks

Newbie quesion: Scientific notation

Postby mdfoster44 » Thu, 03 Feb 2005 08:18:37 GMT

Hi,

I'm trying to upload some data into a MySQL database.

Could someone please advise me how I handle scientific notation?

What I have sofar:

-------
The data (pt, x)

P1   4.50015068000000D-004
-------

My regex expressions, I've been experimenting with:

regex1: P\d+\s+(-?([0-9]+(\.[0-9]*)?|\.[0-9]+))/)
does not catch the scientific notation

regex2: P\d+\s+([+-]?(\d+(\.\d*)?|\.\d+)([eEdD][+-]?\d+))/)
does catch the scientific notation.  I then say,
$number = $1;

-------
# Insert data
$dbh->do("INSERT INTO data(x) VALUES (?)",undef, $number);
-------

How do I upload the number to the database?  If I do it as above it is
incorrect.

Thanks your help.

Martin.


Re: Newbie quesion: Scientific notation

Postby anno4000 » Thu, 03 Feb 2005 08:51:00 GMT

 < XXXX@XXXXX.COM > wrote in comp.lang.perl.misc:
          ^                                      ^^
Are these supposed to be regex delimiters?


Of course not.  What makes you think it would?


For matching numbers with regular expressions, see perldoc -q "is a number".
Follow the pointers you find there.


Incorrect how?  Is the data rejected by the database?  Is it accepted
but not the values you expect it to be?  Be specific.

Anno

Similar Threads:

1.Newbie quesion: Scientific notation

Anno Siegel wrote:
> < XXXX@XXXXX.COM > wrote in comp.lang.perl.misc:
> > Hi,
> >
> > I'm trying to upload some data into a MySQL database.
> >
> > Could someone please advise me how I handle scientific notation?
> >
> > What I have sofar:
> >
> > -------
> > The data (pt, x)
> >
> > P1   4.50015068000000D-004
> > -------
> >
> > My regex expressions, I've been experimenting with:
> >
> > regex1: P\d+\s+(-?([0-9]+(\.[0-9]*)?|\.[0-9]+))/)
>           ^                                      ^^
> Are these supposed to be regex delimiters?
>
> > does not catch the scientific notation
>
> Of course not.  What makes you think it would?
>
> > regex2: P\d+\s+([+-]?(\d+(\.\d*)?|\.\d+)([eEdD][+-]?\d+))/)
> > does catch the scientific notation.  I then say,
> > $number = $1;
>
> For matching numbers with regular expressions, see perldoc -q "is a
number".
> Follow the pointers you find there.
Ah, this helps alot thanks!  I just didn't find that before.
>
> > -------
> > # Insert data
> > $dbh->do("INSERT INTO data(x) VALUES (?)",undef, $number);
> > -------
> >
> > How do I upload the number to the database?  If I do it as above it
is
> > incorrect.
>
> Incorrect how?  Is the data rejected by the database?  Is it accepted
> but not the values you expect it to be?  Be specific.
Well the number it uploads is not the float, in this case double, that
I want it to be.  I'll need to do a strtod.
> 
> Anno
Martin.

2.Convert Scientific Notation to decimal equivalent

How can I detect this, I have been running some code for a few days to develop some files and ran into the situation where I am getting the following data for input:

14.95313 14.45312 0
14.95313 1.570813E-015 0
14.95313 -14.45313 0
-14.95313 -28.90625 0
-14.95313 -14.45313 0
-14.95313 1.570813E-015 0
-14.95313 14.45312 0
14.95313 -28.90625 0
0 -28.90625 0
-14.95313 28.90625 0
0 28.90625 0
14.95313 28.90625 0

And my code is skipping some lines as it checks for any erroneous data:
next if grep (/[^0-9.-]/, @data);
But that thinks the scientific notation is bad. I searched the net and didn't find anything. How can I match this specific pattern and convert it?

Thanks!
jlc



3.print flips to scientific notation when integer exceeds 15 decimal digits

Philip Potter wrote:

> I would guess that these numbers are being stored in floats, and that
> these floats are 64-bit double precision, with 53 bits of mantissa.
> That means that there are just under 16 decimal digits of precision in
> these numbers. print and friends seem to automatically print no more
> than 15 digits of precision:
> [...]
> You probably want to use bignums instead. If you are dealing with
> large integers, where precision down to the units place is important,
> you definitely want bignums and not floats.
> 
> H:\>perl -e "use bignum; my $i = 999999999999997; print $i, ' ',$i+4,qq[\n];"
> 999999999999997 1000000000000001
> 
> the bignum pragma makes $i into a Math::BigInt object rather than a
> floating-point value.
> 
> Your solution (a non-obvious printf format) is a bad one because while
> it solves the problem of output, it doesn't solve the problem of
> representation. As soon as you try to store an integer bigger than
> 2^53 (approximately 9e15) you will lose significance:
> [...]
> In answer to your question, the behaviour of print is probably the
> correct behaviour, since there's no point printing more precision than
> you store. So it's a self bug.

Many thanks for the reply.

Following the initial surprise, my main concern was that attempts to 
unearth a description or explanation (i.e. documentation) for the 
observed behaviour was so tricky.  For instance, there was nothing 
obvious in the relevant parts of "Programming Perl".

So I'm happy to categorise it as "self bug", although I'd like to 
include an element of "documentation weakness" in there and I'd be happy 
to assist someone in trying to improve the latter.

Anyway, your explanation was useful and gives us sufficient to decide 
how to address our local use of these numbers.  (In our case, they are 
human-oriented accumulated byte-counts, for which we don't actually need 
that significance/precision.)

Thanks.

-- 
: David Lee
: ECMWF (Data Handling System)
: Shinfield Park
: Reading  RG2 9AX
: Berkshire
:
: tel:    +44-118-9499 362
: email:   XXXX@XXXXX.COM 

4.print flips to scientific notation when integer exceeds 15 decimal digits

Although I've used perl for many years, I've just been surprised (in the 
unpleasant sense) by a recent event.  Given a variable, say "$int", 
which is a growing integer, I would expect "print $int" to print it as a 
simple integer; indeed it usually does so.  But when its size takes it 
from 15 decimal digits to 16 decimal digits, that "print" flips the 
output into scientific notation:
    999999999999997
    999999999999998
    999999999999999
    1e+15
    1e+15

Ouch.  The surprise is especially nasty when this output data is then 
used as input data for another program that expects an integer.

This is consistent across a variety of OSes (although all perl 5.8.8).

I eventually managed to track down a way to achieve the desired result 
with a non-obvious "printf" format.  (I leave that, and its clear 
user-oriented explanation, as an exercise for the reader!)

I'm not aware of any documentation about this surprise. Is this a 
program bug or a documentation bug?  (Or a self bug?)

Is there a more appropriate forum than "beginners@..." to raise this?

-- 
: David Lee
: ECMWF (Data Handling System)
: Shinfield Park
: Reading  RG2 9AX
: Berkshire
:
: tel:    +44-118-9499 362
: email:   XXXX@XXXXX.COM 

5.basic quesion reg file opening

why cant i specify like this to open a file

open AG, " < ../new/new_name*";

Here  i could open  the file only if i mention the full file name if i
give it as * i could not open the file ...is it possible to open the
file with out mentioning the full file name coz in my progrm the file
name new_name is only constant after that i append date to it so varies
?

thanks for the help

6. Scientific Number problem again!

7. an Invitation to be Involved in a Survey on Developing Scientific Computing Software

8. Using the () list notation with $scalars



Return to PERL

 

Who is online

Users browsing this forum: No registered users and 62 guest