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


In reply to Re: place floats into array by Perlbotics
in thread place floats into array by baxy77bax

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.