Klortho / Physics-Unit

Perl Physics::Unit module
http://search.cpan.org/perldoc?Physics::Unit
Do What The F*ck You Want To Public License
8 stars 3 forks source link

Wishlist: some way to control output serialization #11

Open Klortho opened 12 years ago

Klortho commented 12 years ago

This is another to-do item from the vault.

Here's an example from a post by John Clyman on comp.languages.perl.modules (http://tinyurl.com/7hy6p4u):

  use Number::Scale;
  my $basic_si = Number::Scale->SI;
  print $basic_si->scale(1024); '1 K'
  my $si_bytes = $basic_si->new(
     unit => ['byte', 'B'],
     trunc => 2,
  );
  print $si_bytes->(1024); # '1 KB'
  my $si_distance = $basic_si->new(
     unit => ['meter', 'M'],
     trunc => 2,
  );
  my $si_weight = $basic_si->new(
     unit => ['gram', 'G'],
     trunc => 2,
  );
  my $std_distance = Number::Scale->StandardDistance;
  print $std_distance->scale(25); # '2 feet 1 inch'
  my $std_weight = Number::Scale->StandardWeight;
  print $std_weight->scale(18); # '1 pound 2 ounces'
Klortho commented 12 years ago

This would involve some way to encode systems of units. Something like this, maybe:

InitSystem (
    'si', [ qw(m kg s newton square-meter cubic-meter watt joule
               pascal m/s coulomb volt farad ohm siemens tesla weber henry)
          ],
    'cgs', [ qw(cm gm s dyne square-centimeter cubic-centimeter watt
                erg dynes-per-square-centimeter centimeters-per-second)
           ],
    'english', [ qw(foot slug s pound square-foot cubic-foot horsepower
                    foot-pound pounds-per-square-inch feet-per-second)
               ],
);
jberger commented 12 years ago

A few things to note, your first system is not SI, but rather MKS. SI uses grams not kilograms. I think that if this was a goal, it would fit in with having loadable databases per instance. One would make a Physics::Unit object, load a units database and set the units system. I think this is a worthy goal, but a major undertaking.

Klortho commented 12 years ago

your first system is not SI, but rather MKS

According to Wikipedia, it is kilograms.

Another system that we'll definitely want support for is the Furlong/Firkin/Fortnight (FFF) system. :)

jberger commented 12 years ago

Well I stand corrected, NIST

Klortho commented 12 years ago

There are other very good ways to specify output serialization.

Here's how Math::Units::PhysicalValue does it:

print ($rate + "0 miles/hour"), "\n" # prints 103.07 miles/hour

Another would be a method to produce an output using the best SI prefix. Quoting jpierce on RT: "Since you already have prefix to exponent mapping, it would be mighty handy to have a method of taking a result like 1.055056e+18 (joules) and getting back: 1.055056, exa(joules)."