Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Re^2: The Definitive Unit Conversion Script

by BrowserUk (Pope)
on Dec 13, 2002 at 20:58 UTC ( #219748=note: print w/ replies, xml ) Need Help??


in reply to Re^2: The Definitive Unit Conversion Script
in thread The Definitive Unit Conversion Script

I haven't looked at your code in detail yet, nor tried to use the interface you've got already, specifically so that I would not be influenced.

My first thought on how I would like to use a conversions module is that I would pass the source and destination units and it would create a named sub in my package namespace (like use constant does).

In use, it might look something like this:

# if (units) specified after text unit description # (which should understand most normal abbrevs.) # then if the input is a string it is inspected for units, # and the conversion done in the appropriate direction # If the input is purely numeric (if ONLY Perl_looks_like_number() was + accessible!) # then the conversion is in the direction specified by the order of de +claration time parameters. use Units::Convert FT_IN_2_MTRS => q[ft(')inches(") meters(m)]; print FT_IN_2_MTRS q{5'10"}; # prints '1.7773m' print FT_IN_2_MTRS 5.8333; # prints 1.77773 # No (units) specified on delclaration, input must be numeric, convers +ion works in 1 direction only. use Units::Convert MPH_2_KPH => q[mph kph]; print MPH_2_KPH 70; # prints 112 print MPH_2_KPH '70mph'; # Causes warn or die my @limits = qw(30 40 50 60 70); print "@{[ MPH_2_KPH @limits ]}"; # prints 50 64 80 96 112 # An extension would be for the user to supply a sprintf-style format +string # that is used for the formating/precision of the output. # Once we get string .v. numeric contexts, the sub could determine whe +n to append the units or not use Units::Convert CI_2_CC => 'inch^3(%.2f ci) cm^3(%.f cc)'; print CI_2_CC 500; # prints 8183 print CI_2_CC '500 ci'; # prints '8183 cc' # If an itermediate conversion is required, this could be specified on + the declaration # I'm not sure this is a good example, but it's the one that came to m +ind. use Units::Convert UK_2_METRIC_WEIGHTS => 'stones(st)_pounds(lbs) lbs +kilograms(%.1f kilos)'; print UK_2_METRIC_WEIGHTS '11st 7lbs'; # prints '73.2 kilos' print UK_2_METRIC_WEIGHTS 11.5; # prints 73.2 print UK_2_METRIC_WEIGHTS '11.5_'; # prints '73.2 kilos' maybe? # The presence of an underscore forces output formattting (if supplied +)?

Final thought on the precision and under/overflow thing. Perhaps, if a flag is set, the routines could return BigInt/Floats if the standrad precisions will cause accuracy loss? I haven't thought that through, so I don't know what the implications are.

Now I'll read your code and see if I'm completly off-base, but I like to look at things from my own perspective first when I can :^).

If you decide not to go ahead with teh module, let me know and I will.


Examine what is said, not who speaks.


Comment on Re: Re^2: The Definitive Unit Conversion Script
Download Code
Re^4: The Definitive Unit Conversion Script
by Aristotle (Chancellor) on Dec 13, 2002 at 21:06 UTC
    Well, I'm definitely not going to go forward with the interface you described. :) That's entirely different from what I'm trying to achieve. Although I guess a ::Subs add-on module could handle that well enough. I don't see the point in a UK_2_METRIC routine if you still have to specify the units though.

    Makeshifts last the longest.

      You don't have to specify the units on input. If you supply a number (or array of numbers), then they just get converted to numbers.

      The idea was that of you were reading stuff from another source with the units specified, then the direction of the conversion would be determined for you. Just a thought.

      Also, if you don't specify the units at declaration time, units are not even looked for. If they are supplied at runtime, its just results in a runtime "non-numeric" error. I'll look at your code now ;^).


      Examine what is said, not who speaks.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-10-26 04:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (151 votes), past polls