Convert Scientific Notation to decimal equivalent

PERL

    Next

  • 1. check if files are older then 5 minutes
    Hi, Can someone please show me an example what can do the following: In a directory check all files If a file is older then 15 minutes send a message (email) Reg. Patrick.
  • 2. changing the group that files are created with
    I have a lot of files created by a (much too) complex script and the user I am running with has a default group of 'staff' but I want all files created to have <client>grp which we create to ensure that only authorised people have access to a particular clients data. I have a command in Linux that does this but I am running on AIX (I want it portable so a pure perl method is better...) MY solutions are chgrp on the file after creation. Great initially but then someone will forget for a new file and besides it adds a lot more code and the complexity goes up. I ended up adding the group to the directory with g+s to force the group but there may be a time were we use common directories and the file should be protect there as well. I cannot google an answer because there are too many answers and I cannot figure out how to fine tune my query. I want to change the group files are created with and I cannot google an answer because I get too many answers. Thanks Ken Foskey
  • 3. reading data from 2 files and writing to an another file
    hi all i had 2 files as file 1 1 1.12 2 3.45 3 4.67 file 2 1 5.78 2 1.56 3 3.90 and output file as 1 1.12 5.78 2 3.45 1.56 3 4.67 3.90 i had tried but i am failed to get the output i am very thankful if any one suggests me
  • 4. prevent duplicate child processes
    Greetings, I have a Perl/Tk script which has a button used to launch an independent Perl/Tk program in WinXP. To do this, I used 'system' like: $button = $top->Button(-command => sub{system("start /min perl gascontrol.pl")}); which launches the perl/tk program gascontrol.pl just fine. However, nothing prevents it from being launched again, creating a duplicate process. Is there a way to capture the pid of the new process, so that the button can be configured to only launch the program again if the earlier pid has been terminated? Thanks, Bruce

Convert Scientific Notation to decimal equivalent

Postby JCasale » Fri, 20 Jul 2007 06:19:34 GMT

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




Re: Convert Scientific Notation to decimal equivalent

Postby chas.owens » Fri, 20 Jul 2007 06:27:05 GMT



Don't write the regex yourself, use one of the ones in
Regexp::Common*.  $RE{num}{real} is probably what you want.

 As for how to make "1e3" into 1000, just add 0:

perl -e 'my $i = "1e3"; print $i, " ", $i+0, "\n"';


*  http://www.**--****.com/ ~abigail/Regexp-Common-2.120/lib/Regexp/Common.pm

Re: Convert Scientific Notation to decimal equivalent

Postby krahnj » Fri, 20 Jul 2007 06:36:08 GMT



perldoc -q "How do I determine whether a scalar is a number"



John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

Re: Convert Scientific Notation to decimal equivalent

Postby fxn » Fri, 20 Jul 2007 06:38:09 GMT

El Jul 18, 2007, a las 11:19 PM, Joseph L. Casale escribi
>> 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?

I am not sure I understand the problem to solve.

You need to filter out lines that contain something that's *not* a  
number? If that's the case, is @data a split on whitespace for each  
line? If that's the case in turn, have a look at

   perldoc -q determine

or delegate the job to Regexp::Common:

   $ perl -MRegexp::Common -wle 'print 1 if "1.570813E-015" =~ /\A$RE 
{num}{real}\z/'
   1

-- fxn


Re: Convert Scientific Notation to decimal equivalent

Postby chas.owens » Fri, 20 Jul 2007 07:20:05 GMT





It is important to note that the print function* will use scientific
notation on numbers that are very large or very small.  For instance
0.000000000000001570813 will be printed as "1.570813e-15".  If you
absolutely must have 0.000000000000001570813 then you will need to use
printf or sprintf:

#!/usr/bin/perl

use strict;
use warnings;

my @n = (
        0.000000000000001570813,
        10000000000000000000000,
        "1e4",
        "1e-4"
);

for my $n (@n) {
        my $s = expand($n);
        print "$n is $s\n";
}

sub expand {
        my $n = shift;
        return $n unless $n =~ /^(.*)e([-+]?)(.*)$/;
        my ($num, $sign, $exp) = ($1, $2, $3);
        my $sig = $sign eq '-' ? "." . ($exp - 1 + length $num) : '';
        return sprintf "%${sig}f", $n;
}


* actually any conversion of a number to string for very large or
small numbers, that is why the unless in expand works.

Re: Convert Scientific Notation to decimal equivalent

Postby chas.owens » Fri, 20 Jul 2007 07:31:29 GMT



Is "foo10bar" valid?  /^$RE{num}{real}$/ says no, but /$RE{num}{real}/
says yes.  \A and \z are similar to ^ and $, but are not effected by
the m and s flags (which is not an issue in your case since you are
splitting on whitespace).


Not in Perl 5 (Perl 6 will have the smart match operator ~~).  If you
want to bail if any of the values in @data are not numbers then you
should say

next if grep { not /^$RE{num}{real}$/ } @data;

Or if you want to reduce @data to just the numbers

next unless my @num = grep { /^$RE{num}{real}$/ } @data;

Re: Convert Scientific Notation to decimal equivalent

Postby chas.owens » Fri, 20 Jul 2007 11:23:24 GMT


snip

Sort of, the code doesn't modify the original array, it creates a new
array with only the values that are numbers.

Re: Convert Scientific Notation to decimal equivalent

Postby fxn » Fri, 20 Jul 2007 17:44:15 GMT

El Jul 19, 2007, a las 12:19 AM, Joseph L. Casale escribi

>> Interesting,
>> I see from your regexp you use a \A and \z, from Perldoc this means:
>> \A      Match only at beginning of string
>> \z      Match only at end of string
>>
>> I am not sure I understand this requirement?

^ and $ depend on flags, and $ allows an optional trailing newline.  
When I want to match a complete string exactly, I tend to use \A and  
\z because they convey that intention clearly.

If your code processes line by line and splits on whitespace, \A ...  
\z is equivalent to ^ ... $.

>> In my case, I am checking an array of 3 scalars. Does this make sense:
>> next unless @data =~ /$RE {num}{real}/;
>> Does the regexp know to evaluate each element in the array  
>> implicitly? Or do I need to tell it this?

Detecting whether something holds in an array is the job of grep:

   my $numbers = grep /\A$RE{num}{real}\z/, @data;
   next unless $numbers == @data;

-- fxn


Re: Convert Scientific Notation to decimal equivalent

Postby rvtol+news » Sun, 22 Jul 2007 15:21:50 GMT

Xavier Noria schreef:



Alternative:

  die if grep !/\A$RE{num}{real}\z/, @data; 
  my $numbers = scalar @data; 

-- 
Affijn, Ruud

"Gewoon is een tijger."

Similar Threads:

1.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 

2.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 

3.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.

4.Newbie quesion: Scientific notation

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.

5.Need help converting Perl to Ruby (detecting integers and decimals in strings)

I am trying to convert a Perl script to Ruby and have been having some
difficulty along the way (mostly because I don't know Perl).

I am currently stumped on one particular line in the old Perl script:
---
	foreach (@rawfields) {if ($_ > 0){$_ = int($_*100)/100}}
---

Background context:  @rawfields is an array that holds the contents of
an imported line from a text file.  Here are some sample lines from
the input file:
---
"0"	"0"	"0"	"0"	"0"	"0"	"Bar"
"5.66666"	"0"	"3.566662"	"1.383332"	"6"	"0"	"Foo"
---

My guess is that the Perl line iterates through each of the elements
in the array and rounds any integers to 2 decimals.

This raises a few problems for me.

1) How can I easily tell if the (string) value in each array element
is an integer or has a decimal value?

=> The desired output for text file line 2 is :
---
"5.67"   "0"   "3.57"   "1.38"   "6"   "0"   "Foo"
---


I tried the following in Ruby:
---
@rawfields.each_index do |x|
	if ( @rawfields[x].to_f > 0 )
		@rawfields[x] = ( (( @rawfields[x].to_f * 100 ).round.to_i ).to_f /
100 ).to_s
	end
end
---

But it produces the following output:
---
"5.67"  "0"  "3.57"  "1.38"  "6.0"  "0"  "Foo"
---

...which is close, but not quite what I want.  My output file now has
values like 1.0, 5.0, 62.0, etc.  If it's an integer, I don't want to
see the decimal.

Can anyone suggest an improvement to my code or a better translation
from Perl?

TIA.  Paul.

6. how to convert decimal to hexadecimal ??

7. how to convert decimal to hexadecimal in perl

8. converting a line of ascii to decimal



Return to PERL

 

Who is online

Users browsing this forum: No registered users and 18 guest