### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Type checking of units of measurement  (Read 6357 times)

#### circular

• Hero Member
• Posts: 3961
##### Re: Type checking of units of measurement
« Reply #60 on: February 05, 2023, 05:49:22 pm »
... but TFactoredNumeratorUnitIdentifier and TFactoredNumeratorUnitQuantity need TUnit not TFactoredUnit.
That's because unfactored versions of the units come first as generic parameters. But anyway let's simplify.

Quote
and then can a user combine units derived from these the two kg units ?
:'(
Yes. Basically the "kg" identifier will still be the one derived from the gram, allowing to do conversions to g, mg... But when combined, it will become the new TBaseKilogram unit.

I've added some implicit conversions to do that.

So now, TNewton is much simpler:
Code: Pascal  [Select][+][-]
1. type
2.   TNewton = specialize TUnitProduct<TBaseKilogram, TMeterPerSecondSquared>;
3.   TNewtonIdentifier = specialize TUnitProductIdentifier<TBaseKilogram, TMeterPerSecondSquared>;
4.   TNewtons = specialize TDimensionedQuantityProduct<TBaseKilogram, TMeterPerSecondSquared>;
5.
6. // combining units
7. operator *(const {%H-}kg: TKilogramIdentifier; const {%H-}m_s2: TMeterPerSecondSquaredIdentifier): TNewtonIdentifier; inline;
8. operator /(const {%H-}kg: TKilogramMeterIdentifier; const {%H-}s2: TSquareSecondIdentifier): TNewtonIdentifier; inline;
9.
10. // combining dimensioned quantities
11. operator *(const AWeight: TGrams; const AAcceleration: TMetersPerSecondSquared): TNewtons; inline;
12. operator *(const AAcceleration: TMetersPerSecondSquared; const AWeight: TGrams): TNewtons; inline;
13. operator *(const AWeight: TBaseKilograms; const AAcceleration: TMetersPerSecondSquared): TNewtons; inline;
14. operator *(const AAcceleration: TMetersPerSecondSquared; const AWeight: TBaseKilograms): TNewtons; inline;

The operator to combine TBaseKilograms is not strictly necessary but it can help prevent useless conversion of kg to g and then back to base kg.
Conscience is the debugger of the mind

#### qk

• New Member
• Posts: 13
##### Re: Type checking of units of measurement
« Reply #61 on: February 05, 2023, 11:33:00 pm »
Pull request done!

Regards.

#### avra

• Hero Member
• Posts: 2443
##### Re: Type checking of units of measurement
« Reply #62 on: February 06, 2023, 12:31:21 am »
... the "kg" identifier will still be the one derived from the gram...
Huh, shouldn't it be the other way around (g derived from kg)? kg (not g) is base SI unit:
https://www.nist.gov/pml/owm/metric-si/si-units
https://www.nist.gov/sites/default/files/styles/2800_x_2800_limit/public/images/2021/08/23/NIST.SP_.1247.png
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

#### circular

• Hero Member
• Posts: 3961
##### Re: Type checking of units of measurement
« Reply #63 on: February 06, 2023, 07:53:05 am »
Yes and no. If "kg" is the base unit, then the "k" is just part of its name. But we want to be able to convert from kg to g to mg etc. So it is better if kg is the kilo-unit of the gram.

But when combining kg with other units to make a special unit like newton, volt, ohm, etc. we don't want this unit to be a kilo-unit. So then the base unit is the "kg". And from the new unit, we can make a kilo-unit, for example kN.

So there are two base units of mass, in the sense of non-factored units.

A similar thing happens with cubic meters and litres.
1 L = 1 dm3 = 0.001 m3
1 kL = 1 m3

We want to be able to have cL, etc. and this cannot be done with SI base unit. Indeed what we can only do by factors of 1000 due to the cube:
1 cm3 = 0.001 dm3 = 0.001 L = 1 mL
Conscience is the debugger of the mind

#### circular

• Hero Member
• Posts: 3961
##### Re: Type checking of units of measurement
« Reply #64 on: February 12, 2023, 10:29:06 am »
Some update about the topic: new units were added, it was not obvious but we did it.
https://github.com/circular17/DimPas/pull/1#issuecomment-1426892616

Base units
Time: ms, s, s2, mn, h, day
Length: mm..mm4, cm..cm4, m..m4, km..km4, L
Weight: mg, g, kg, ton
Mole: mol
Ampere: A, A2
Temperature: K, ºC, ºF
Candela: cd

Special units
Frequency: Hz = s-1
Force: N = kg*m/s2, kN
Charge: C = A*s
Pressure: Pa, kPa, MPa
Energy: J = N*m
Illuminance: lux = cd/m2
Equivalent dose: Sv = m2/s2
Catalytic activity: kat = mol/s

Derived units
Stiffness: N/m
Density: g/m3, kg/m3

https://github.com/circular17/DimPas/blob/main/dim.pas
Conscience is the debugger of the mind

#### avra

• Hero Member
• Posts: 2443
##### Re: Type checking of units of measurement
« Reply #65 on: February 13, 2023, 12:35:02 am »
What I see missing are micro and nano (um, nm, us, ns)
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

#### dseligo

• Hero Member
• Posts: 953
##### Re: Type checking of units of measurement
« Reply #66 on: February 13, 2023, 01:00:50 am »
What I see missing are micro and nano (um, nm, us, ns)

And pico if electrical units will be included (needed for Farad, pF).

#### circular

• Hero Member
• Posts: 3961
##### Re: Type checking of units of measurement
« Reply #67 on: February 13, 2023, 06:18:47 am »
Indeed, well I invite you to have a look at the code and see if you can guess how to add that.
Conscience is the debugger of the mind

#### circular

• Hero Member
• Posts: 3961
##### Re: Type checking of units of measurement
« Reply #68 on: February 19, 2023, 08:03:49 pm »
I've added micro, nano and pico.

qk added Siemes, Weber, Tesla and Henry.

marcov, Bogen85, Warfley, ccrause, 440bx, avra, VisualLab you seemed interested by such package. I would say most of the obstacles have been dealt with.

Do you have an actual use of the package, suggestions or contributions to propose?
Conscience is the debugger of the mind

#### 440bx

• Hero Member
• Posts: 3325
##### Re: Type checking of units of measurement
« Reply #69 on: February 20, 2023, 01:07:22 am »
marcov, Bogen85, Warfley, ccrause, 440bx, avra, VisualLab you seemed interested by such package. I would say most of the obstacles have been dealt with.

Do you have an actual use of the package, suggestions or contributions to propose?
I'm not, at the moment, working on a physics problem, because of this I currently don't need to deal with units but, it's quite likely that I will sometime in the future.  At that time, I will take your unit out for a "walk"

The biggest problem I'm going to face is getting used to the fact that I can use units.  IOW, I'm completely used to using numbers and being responsible for using them in a way that is dimensionally sensible.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 SP1 64bit.

#### circular

• Hero Member
• Posts: 3961
##### Re: Type checking of units of measurement
« Reply #70 on: February 26, 2023, 12:08:23 pm »
I will be happy to know how it goes when you go for a "walk" with my unit.

There seem to be a bug with the generics, at least on the version of FPC that I am using. Can someone with trunk tell me if the bug still happens?

The problem is the following. I have commented out line 153 of Dim.pas (and its implementation on line 2269)
Code: Pascal  [Select][+][-]
1. class operator /(const ASelf: TSelf; const AQuantity1: TQuantity1): TQuantity2;

When uncommented, the compiler comlains on the next line that:
Quote
dim.pas(154,20) Error: Function is already declared Public/Forward "operator /(const TDimensionedQuantityProduct\$2;const TDimensionedQuantityProduct\$2.TDimensionedQuantity\$1\$crc92C318DE):<record type>; Static;
But the type of the parameter is not the same (TQuantity1 and TQuantity2).

A similar problem happens on line 386 with:
Code: Pascal  [Select][+][-]
1. class operator /(const {%H-}TheUnit: TSelf; const {%H-}Unit1: TIdentifier1): TIdentifier2;

Maybe I am missing something?
Conscience is the debugger of the mind