RFC: Module Physics::Measurement

J

John

Physics::Measurement is a subclass of Physics::Unit::Scalar, but with
overloaded operators. Like it's superclass, the module allows the creation
of objects that represent physical quantities with encapsulated dimensions;
but Physics::Measurment objects provide some nifty features

I'm looking for input from the list: What do you think? Do we need it on
CPAN? Is it apty named? Other comments?

Features of Physics::Measurement objects
-Overloaded arithmetic and inequality operators
-Dies on operations on two measurments with incompatible quantities. So
you can't add 1 foot to 1 second, although you can divide 1 foot by 1
second.
-Automaticly stringified when interpolated into a string.
-Produces warning when used in a string comparison (cmp, gt, etc).
Otherwise things like
"2 minutes" lt new Physics::Measurement("40 seconds");
will cause hard-to-find bugs.
-Dies when used in numeric comparison with non-measurements. So you can't
compare "3 ounces" to 12.
-Automatically converts strings to measurements, when a measurement and a
string are used in a numeric operation:
(new Measurement("3 meters")) + "1 meter" == "4 meter"
-Always true in boolean expressions (in contrast to regular numbers, where
0 is false).
-(Not yet implemented) Significant figures and scientific notation.
I would like to use the Math::SigFigs module to handle this.

Examples

#Overloaded arithmetic and inequality operators
my $distance = new Physics::Measurement("3.0e8 m");
my $time = new Physics::Measurement("1 s");

my $speed = $distance / $time;
my $warp10 = $speed*10;

#Automatic stringification
print "Warp 10 is: $warp10\n";

###String comparison with strings produces warning.
if(1*meter gt "1 yard"); #Will compare the string "1 m" to "1 yard", and
produces false and a warning.

#Incomperable to other numbers
my $m = new Physics::Measurement("5 m");

$m + 10; #All these raise an exception
$m == 5; #...
$m < 10; #...
log($m) #...

#Numeric comparison with strings does automatic upgrade
my $speed = new Physics::measurement("60 mph");
$speed == "1 mile/minute"; #True.
$speed == "blah blah"; #Tries to parse blah blah and dies.
1*meter > "1 yard"; #True

###boolean always true
my $m = new Physics::Measurement('0 m');
$m and $m; #True...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top