librasteve / raku-Physics-Measure

do calculations on objects with value, units and error
Artistic License 2.0
11 stars 2 forks source link

Fix cm unit #25

Closed wayland closed 3 years ago

wayland commented 3 years ago

The following fails with an error:

#!/usr/bin/raku

use     Physics::Unit;
use     Physics::Measure;

my $a = Length.new(value => 21.006, units => 'cm');

say $a;

Metres work fine, cm don't.

Version 0.0.3

Thanks for your work on this!

librasteve commented 3 years ago

Yeah - this is a bug - I will squish it and add to test! Apologies...

librasteve commented 3 years ago

Hi @wayland - on initial diagnosis I have found:

1) 'c' was in Unit.rakumod as a definition of 'speed-of-light' (ie as a unit name and not a prefix name)... this has now been moved out to https://github.com/p6steve/raku-Physics-Constants/blob/master/lib/Physics/MathConstantsWrapper.rakumod

2) The last big refactor was to move 'shorty' prefixes into Physics::UnitPostfix - so you CAN now say my $x = 21.006cm; (ie cm is a raku Postfix operator so no quotes needed). You can also still say my $xx = Length.new(value => 21.006, units => 'centim'); since the long form prefixes are still in place. Looks like I threw the baby out with the bathwater a bit - so need to find a way to bring 'cm' back into the non-Postfix world.

wayland commented 3 years ago

Looks good! I'm pulling in data which contains text like "21.006cm", which is why I ended up with the example that I did.

When are you expecting to release version 0.0.4?

librasteve commented 3 years ago

Hi @wayland I have had a thin about this and decided to keep the prefix/unit combinations in a separate module since they export up to 600 operators into your namespace - since this is the obvious place to load the definitions of all the unit objects that are generated it also seems best to me to keep items like 'cm' in this sister module. I considered changing the name of the module from Physics::UnitPostfix to Physics::UnitPrefixPostfix to show that it is needed WHEREVER you want prefix/unit combinations - but that seemed odd(!) Soooo in time honoured fashion, I have been reduced to changing the documentation.

you will need to use Physics::UnitPostfix anytime you want compound units to be defined - even if you do not need the postfix variant of operator - e.g. for my $x = Length.new(value => 21.006, units => 'cm'); my $y ♎️ '5e1 km';

wayland commented 3 years ago

Just in case it's useful, the correct term would probably be "Unit::Affix" (covers prefix, postfix, and infix).