Retrieve a Quantity Unit from given BaseUnits #1014
-
I seen not all units in json have BaseUnits, for example VolumeUnit.CubicMillimeter lacks for BaseUnits declaration while other for example VolumeUnit.CubicMeter does. I understand that this is justified for unit like VolumeUnit.MetricCup because there isn't a proper defined base LengthUnit such that L^3 results in 1 cup. Btw I would to know if the lacks of BaseUnits for already mentioned CubicMillimeter is because simply json isn't yet stable for BaseUnits description on all derived measure units directly derived from base units or if there are some other reasons. I interested in this because actually I would to write a function ComputeVolumeUnit from given LengthUnit like following: public static VolumeUnit ComputeVolumeUnit(LengthUnit lengthUnit)
{
var res = Volume.Info.UnitInfos.FirstOrDefault(w => w.BaseUnits.Length == lengthUnit)?.Value;
if (!res.HasValue)
throw new InternalError($"not implemented Volume from length unit = {lengthUnit}");
return res.Value;
} while actually, cause the problem exposed, I have to write this: public static VolumeUnit ComputeVolumeUnit(LengthUnit lengthUnit)
{
VolumeUnit res;
switch (lengthUnit)
{
case LengthUnit.Meter: res = VolumeUnit.CubicMeter; break;
case LengthUnit.Centimeter: res = VolumeUnit.CubicCentimeter; break;
case LengthUnit.Millimeter: res = VolumeUnit.CubicMillimeter; break;
case LengthUnit.Foot: res = VolumeUnit.CubicFoot; break;
case LengthUnit.Inch: res = VolumeUnit.CubicInch; break;
default: throw new InternalError($"not implemented Volume from length unit = {lengthUnit}");
}
return res;
} the same manner a ComputeDensityVolumeUnit(MassUnit massUnit, LengthUnit lenghUnit) would more longer than a version using the search through UnitInfos. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hi, the idea with BaseUnits is to describe the 7 SI base units from which all other SI units can be derived. I believe it was introduced as an experiment to be able to configure default units in an application (meter, centimeter, foot). If Length was configured to centimeters, then derived units like area and volume would also automatically be square/cubic centimeters. It also somewhat helps you represent that Area * Area / Length = Volume by defining arithmetic for the dimensions, but you currently can't multiply two The problem is that we have many non-SI units. In addition, multiple units map to the same 7 base units. The usefulness of this base units feature is debatable and it is often confusing for contributors what base units to define. In the upcoming v5 release, it is considered to be removed until the idea is more mature and supports non-SI units. So long story, but yes, I believe you have to write the piece of code you already did as it is now. |
Beta Was this translation helpful? Give feedback.
-
It is simply due to it missing in the json files. We should add where it's missing. |
Beta Was this translation helpful? Give feedback.
Hi, the idea with BaseUnits is to describe the 7 SI base units from which all other SI units can be derived.
https://en.wikipedia.org/wiki/SI_base_unit
I believe it was introduced as an experiment to be able to configure default units in an application (meter, centimeter, foot). If Length was configured to centimeters, then derived units like area and volume would also automatically be square/cubic centimeters.
It also somewhat helps you represent that Area * Area / Length = Volume by defining arithmetic for the dimensions, but you currently can't multiply two
IQuantity
of unknown quantity type and unit and get a result back.The problem is that we have many non-SI units. In addition, mul…