I'm trying to adapt a piece of JavaScript code for my purposes and it contains the following function:

Since the orignal JS version aparently raised hackles, here's my first attempt at a Perl version along with some code to exercise it:

#! perl -slw use strict; sub func { my $vv = shift; my $bb = 140*24*60*60*1000; ##140 days if( $vv >= $bb ) { ##140 days < 5 months $bb = 8766*60*60*1000;##1 year if( $vv < $bb ) { return( $bb/12 ); } ## 1 year + ##1 month if( $vv < $bb *2 ) { return( $bb/6 ); } ## 2 years + ##2 month if( $vv < $bb *5/2 ) { return( $bb/4 ); } ## 2.5 years + ##3 month if( $vv < $bb *5 ) { return( $bb/2 ); } ## 5 years + ##6 month if( $vv < $bb *10 ) { return( $bb ); } ## 10 years + ##1 year if( $vv < $bb *20 ) { return( $bb*2 ); } ## 20 years + ##2 years if( $vv < $bb *50 ) { return( $bb*5 ); } ## 50 years + ##5 years if( $vv < $bb *100 ) { return( $bb*10 ); } ## 100 years + ##10 years if( $vv < $bb *200 ) { return( $bb*20 ); } ## 200 years + ##20 years if( $vv < $bb *500 ) { return( $bb*50 ); } ## 500 years + ##50 years return( $bb*100 ); ##100 years } $bb /= 2; ##70 days if( $vv >= $bb ) { return( $bb/5 ); } ##14 days $bb /= 2; ##35 days if( $vv >= $bb ) { return( $bb/5 ); } ##7 days $bb /= 7; $bb *= 4; ##20 days if( $vv >= $bb ) { return( $bb/5 ); } ##4 days $bb /= 2; ##10 days if( $vv >= $bb ) { return( $bb/5 ); } ##2 days $bb /= 2; ##5 days if( $vv >= $bb ) { return( $bb/5 ); } ##1 day $bb /= 2; ##2.5 days if( $vv >= $bb ) { return( $bb/5 ); } ##12 hours $bb *= 3; $bb /= 5; ##1.5 day if( $vv >= $bb ) { return( $bb/6 ); } ##6 hours $bb /= 2; ##18 hours if( $vv >= $bb ) { return( $bb/6 ); } ##3 hours $bb *= 2; $bb /= 3; ##12 hours if( $vv >= $bb ) { return( $bb/6 ); } ##2 hours $bb /= 2; ##6 hours if( $vv >= $bb ) { return( $bb/6 ); } ##1 hour $bb /= 2; ##3 hours if( $vv >= $bb ) { return( $bb/6 ); } ##30 mins $bb /= 2; ##1.5 hours if( $vv >= $bb ) { return( $bb/6 ); } ##15 mins $bb *= 2; $bb /= 3; ##1 hour if( $vv >= $bb ) { return( $bb/6 ); } ##10 mins $bb /= 3; ##20 mins if( $vv >= $bb ) { return( $bb/4 ); } ##5 mins $bb /= 2; ##10 mins if( $vv >= $bb ) { return( $bb/5 ); } ##2 mins $bb /= 2; ##5 mins if( $vv >= $bb ) { return( $bb/5 ); } ##1 min $bb *= 3; $bb /= 2; ##3 mins if( $vv >= $bb ) { return( $bb/6 ); } ##30 secs $bb /= 2; ##1.5 mins if( $vv >= $bb ) { return( $bb/6 ); } ##15 secs $bb *= 2; $bb /= 3; ##1 min if( $vv >= $bb ) { return( $bb/6 ); } ##10 secs $bb /= 3; ##20 secs if( $vv >= $bb ) { return( $bb/4 ); } ##5 secs $bb /= 2; ##10 secs if( $vv >= $bb ) { return( $bb/5 ); } ##2 secs return( $bb/10 ); ##1 sec } printf "%f -> %f\n", $_, func( $_ ) for map 10**$_, -2 .. +14; __END__ [18:49:55.78] E:\Chart>junk 0.010000 -> 2500.000000 0.100000 -> 2500.000000 1.000000 -> 2500.000000 10.000000 -> 2500.000000 100.000000 -> 2500.000000 1000.000000 -> 2500.000000 10000.000000 -> 2500.000000 100000.000000 -> 12500.000000 1000000.000000 -> 120000.000000 10000000.000000 -> 900000.000000 100000000.000000 -> 10800000.000000 1000000000.000000 -> 172800000.000000 10000000000.000000 -> 1209600000.000000 100000000000.000000 -> 15778800000.000000 1000000000000.000000 -> 157788000000.000000 10000000000000.000000 -> 1577880000000.000000 100000000000000.000000 -> 3155760000000.000000

It is obviously ripe for refactoring, but can anyone

  1. work out what (beyond:"mapping one number to another") it is doing?
  2. perceive the mapping sufficiently to simplify (preferably:calculate) the mapping?

I'm not sure that you can take much guidance from the comments; as in context, the function does not seem to be being used for anything date or time related?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
li

In reply to [OT] Mysterious function. (Updated) by BrowserUk

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.