Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Math::SnapTo

by tachyon (Chancellor)
on Dec 25, 2002 at 15:14 UTC ( [id://222205]=sourcecode: print w/replies, xml ) Need Help??
Category: Miscellaneous
Author/Contact Info Dr James Freeman james.freeman@id3.org.uk
Description:

The Math::SnapTo module provides several methods to snap numeric values to desired values according to various criteria.

Update

Further research has revealed that this functionality is already available in Math::Round with the nearest() function and Math::SigFigs so this module has been slated for deletion :-(

Replies are listed 'Best First'.
Re: Math::SnapTo
by jryan (Vicar) on Dec 26, 2002 at 01:09 UTC
    package Math::SnapTo; #use strict; # used in testing but not forced on you #use warnings;

    Do you realize that strict is a lexical pragma, and therefore only affects the package Math::SnapTo? :-)

      Suprisingly I do. Strict has an overhead (needs to be loaded as does warnings). For testing I use them but they offer little benefit in a module which should be fully tested and strict and warnings compliant (which this is), If you want strict and warnings operational put it in your code and they will then be operational on the imported functions. The forced on you comment refers to the overhead not the scope.

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Math::SnapTo
by mojotoad (Monsignor) on Dec 26, 2002 at 21:12 UTC
    Ah. My favorite here is the snap_units() function. There was a time when I was playing around with rounding fractions of stock shares -- to the nearest 1/8 or 1/16 value -- before the exchanges I was focused upon switched to arbitrary decimal representations of stock shares.

    In the CB, tye suggested the following change in snap_units() to avoid potential rounding errors:

    from:

    my $factor = 1/$units;
    to:
    my $factor = int( 1/$units + 0.5 );

    The drawback is that for fractional cases, $units must be 1/N, where N is an integer. I'm not super clear on the potential rounding errors; nor am I clear on the potential utility of arbitrary $units. Perhaps certain people can elucidate for our edification.

    Matt

    P.S. For some prior discussion of this stuff, check out Fix floats like you do in your head.

      As it happens Math::Round offers the nearest(UNITS, NUM) function that does exactly this. As the snap_basic() function is legacy dross and significant figures are handled by Math::SigFigs I have decided to delete this module as it does not offer anything not already available. This is a pity as I really liked the name. See Fix floats like you do in your head for more discussion and rationale.

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-28 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found