Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: place floats into array

by Perlbotics (Archbishop)
on Sep 19, 2013 at 18:37 UTC ( [id://1054899]=note: print w/replies, xml ) Need Help??


in reply to place floats into array

And another one...

use strict; use warnings; my @nums = ( 1.3e-3, 3.7e-5, 102, 46, 1e-4, 1e-12); @nums = sort { $b <=> $a } @nums; # biggest number first my $exp_max = int log($nums[0]) / log(10); # first=biggest exponent my $exp = $exp_max; # start exponent my @res; while ( @nums ) { push @res, $nums[0] >= 10**$exp ? shift @nums : 0; $exp--; } printf( "\$res[%2d] = %8g #1e%d\n", $_, $res[$_], $exp_max-$_) for ( +0..$#res);

Result:

$res[ 0] = 102 #1e2 $res[ 1] = 46 #1e1 $res[ 2] = 0 #1e0 $res[ 3] = 0 #1e-1 $res[ 4] = 0 #1e-2 $res[ 5] = 0.0013 #1e-3 $res[ 6] = 0.0001 #1e-4 $res[ 7] = 3.7e-05 #1e-5 $res[ 8] = 0 #1e-6 $res[ 9] = 0 #1e-7 $res[10] = 0 #1e-8 $res[11] = 0 #1e-9 $res[12] = 0 #1e-10 $res[13] = 0 #1e-11 $res[14] = 1e-12 #1e-12

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1054899]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2024-04-18 08:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found