Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a few additional units #859

Merged
merged 8 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions htdocs/helpFiles/Entering-Units.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ <h2 style="text-align: center">Units Available in WeBWorK</h2>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Seconds</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">s</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">s, sec, second or seconds</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Minutes</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">min</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">min, minute or minutes</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Hours</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">hr</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">hr, hrs, h, hour or hours</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Days</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">day</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">day or days</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Years</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">yr</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">yr, year or years</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Milliseconds</td>
Expand All @@ -43,19 +43,19 @@ <h2 style="text-align: center">Units Available in WeBWorK</h2>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Feet</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">ft</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">ft, foot or feet</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Inches</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">in</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">in, inch or inches</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Miles</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">mi</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">mi, mile or miles</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Meters</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">m</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">m, meter, metre, meters or metres</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Centimeters</td>
Expand Down Expand Up @@ -109,6 +109,27 @@ <h2 style="text-align: center">Units Available in WeBWorK</h2>
<td style="padding: 0.5rem; border: 1px solid black">Milliliters</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">ml</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Deciliters</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">dL</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">US Cups</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">cup or cups</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">US Pints</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">pt, pint or pints</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">US Quarts</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">qt, quart or quarts</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">US Gallons</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">gal, gallon or gallons</td>
</tr>


<tr>
<td colspan="2" style="padding: 0.5rem; border: 1px solid black; text-align: center"><b>Force</b></td>
Expand All @@ -123,11 +144,11 @@ <h2 style="text-align: center">Units Available in WeBWorK</h2>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Pounds</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">lb</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">lb, lbs, pound or pounds</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Tons</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">ton</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">ton or tons</td>
</tr>

<tr>
Expand Down Expand Up @@ -191,11 +212,11 @@ <h2 style="text-align: center">Units Available in WeBWorK</h2>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Angle degrees</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">deg</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">deg, &#x00B0;, degree or degrees</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Angle radians</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">rad</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">rad, radian or radians</td>
</tr>
</table>
<p style="text-align: center">
Expand Down
41 changes: 30 additions & 11 deletions lib/Units.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ our $PI = 4 * atan2(1, 1);

our %known_units = (
m => {
factor => 1,
m => 1
factor => 1,
m => 1,
aliases => [ 'meter', 'meters', 'metre', 'metres' ]
},
kg => {
factor => 1,
Expand Down Expand Up @@ -122,11 +123,12 @@ our %known_units = (
hour => {
factor => 3600,
s => 1,
aliases => [ 'hours', 'hr', 'h' ]
aliases => [ 'hours', 'hrs', 'hr', 'h' ]
},
day => {
factor => 86400,
s => 1
factor => 86400,
s => 1,
aliases => ['days']
},
month => { # 60 * 60 * 24 * 30
factor => 2592000,
Expand Down Expand Up @@ -228,19 +230,35 @@ our %known_units = (
factor => 0.0001,
m => 3
},
# U.S./English volume units
cup => {
factor => 0.000236588,
m => 3,
aliases => ['cups']
},
pint => {
factor => 0.000473176473,
m => 3,
aliases => [ 'pt', 'pints' ]
},
quart => {
factor => 0.000946352946,
m => 3,
aliases => [ 'qt', 'quarts' ]
},
gallon => {
factor => 0.00378541,
m => 3,
aliases => [ 'gallons', 'gal' ]
},
Comment on lines +249 to +253
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are some other related units that could be added here. Pint and quart go with these volume measurements, and are equally standard with cup and gallon. There are also teaspoon and tablespoon, that are rather common. If you really want to go the distance there are also pottle, barrel, shot, gill, hogshead, etc. I haven't heard of some of those before. Some fun names too! See customary US units.

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have a hogsheads of mead with lunch today. 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For those that like extreme edge cases, the Gal is the acceleration of 1 cm/s^2, but was deprecated by the Int. Ast. Union in 1989. Probably an idea to include a comment that the gallon is US, not Imperial.

https://en.wikipedia.org/wiki/Gal_(unit)
https://www.iau.org/publications/proceedings_rules/units/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@duffee In the help file, I put "US " in front of the title for each of gallon, quart, pint, and cup.


# VELOCITY: fundamental unit m/s (meters per second)
knots => { # nautical miles per hour
knots => { # nautical miles per hour
factor => 0.5144444444,
m => 1,
s => -1
},
c => { # exact speed of light
c => { # exact speed of light
factor => 299792458,
m => 1,
s => -1
Expand Down Expand Up @@ -335,10 +353,11 @@ our %known_units = (
aliases => [ 'pound', 'pounds', 'lbs' ]
},
ton => {
factor => 8900,
m => 1,
kg => 1,
s => -2
factor => 8900,
m => 1,
kg => 1,
s => -2,
aliases => ['tons']
},

# ENERGY: fundamental unit m^2 kg / s^2
Expand Down
74 changes: 74 additions & 0 deletions t/units/volume.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env perl

use Test2::V0 '!E', { E => 'EXISTS' };

die "PG_ROOT not found in environment.\n" unless $ENV{PG_ROOT};
do "$ENV{PG_ROOT}/t/build_PG_envir.pl";

use lib "$ENV{PG_ROOT}/lib";

use Units qw(evaluate_units);
use Parser::Legacy::NumberWithUnits;

loadMacros('parserNumberWithUnits.pl');

my $cubic_meter = NumberWithUnits(1, 'm^3');
my $liter = NumberWithUnits(1, 'L');
my $milliliter = NumberWithUnits(1, 'ml');
my $deciliter = NumberWithUnits(1, 'dL');

subtest 'metric LaTeX output' => sub {
is $cubic_meter->TeX, '1\ {\rm m^{3}}', 'LaTeX output for 1 cubic meter';
is $liter->TeX, '1\ {\rm L}', 'LaTeX output for 1 liter';
is $deciliter->TeX, '1\ {\rm dL}', 'LaTeX output for 1 deciliter';
is $milliliter->TeX, '1\ {\rm ml}', 'LaTeX output for 1 milliliter';
};

subtest 'metric unit aliases' => sub {
is { evaluate_units('ml') }, { evaluate_units('cc') }, '1 mL = 1 cc';
};

subtest 'metric volume conversion' => sub {
is multiply_by(1000, evaluate_units('L')), { evaluate_units('m^3') }, '1000 L = 1 m^3';
is multiply_by(1000, evaluate_units('ml')), { evaluate_units('L') }, '1000 ml = 1 L';
is multiply_by(10, evaluate_units('dL')), { evaluate_units('L') }, '10 dL = 1 L';
};

my $gallon = NumberWithUnits(1, 'gal');
my $quart = NumberWithUnits(1, 'qt');
my $pint = NumberWithUnits(1, 'pt');
my $cup = NumberWithUnits(1, 'cup');

subtest 'U.S. Units LaTeX output' => sub {
is $gallon->TeX, '1\ {\rm gal}', 'LaTeX output for 1 gallon';
is $quart->TeX, '1\ {\rm qt}', 'LaTeX output for 1 quart';
is $pint->TeX, '1\ {\rm pt}', 'LaTeX output for 1 pint';
is $cup->TeX, '1\ {\rm cup}', 'LaTeX output for 1 cup';
};

subtest 'metric unit aliases' => sub {
is { evaluate_units('cup') }, { evaluate_units('cups') }, 'cups alias';
is { evaluate_units('pt') }, { evaluate_units('pint') }, 'pint alias';
is { evaluate_units('pt') }, { evaluate_units('pints') }, 'pints alias';
is { evaluate_units('qt') }, { evaluate_units('quart') }, 'quart alias';
is { evaluate_units('qt') }, { evaluate_units('quarts') }, 'pint alias';
is { evaluate_units('gallon') }, { evaluate_units('gal') }, 'gal alias';
is { evaluate_units('gallon') }, { evaluate_units('gallons') }, 'gallons alias';
};

subtest 'U.S. volume conversion' => sub {
is multiply_by(3.78541, evaluate_units('L')), { evaluate_units('gal') }, '3.785412 L = 1 gal';
# Switch to check_score to do fuzzy comparison since cups/pints/quart/gallons are defined in
# terms of cubic meters.
is check_score(NumberWithUnits(2, 'cup'), $pint), 1, '2 cups = 1 pint';
is check_score(NumberWithUnits(2, 'pint'), $quart), 1, '2 pints = 1 quart';
is check_score(NumberWithUnits(4, 'quart'), $gallon), 1, '4 quarts = 1 gallon';
};

sub multiply_by {
my ($conversion, %unit) = @_;
$unit{factor} *= $conversion;
return \%unit;
}

done_testing();