Commit 2299a7f1 authored by Vesa Oikonen's avatar Vesa Oikonen
Browse files

added unitMultiply(); fixed error in tpc_unit[]

parent be044821
......@@ -96,7 +96,7 @@ static TPC_UNIT tpc_unit[]={
{"mmol/L", UNIT_MMOL, UNIT_UNITLESS, UNIT_L, UNIT_UNITLESS},
{"mol/L", UNIT_MOL, UNIT_UNITLESS, UNIT_L, UNIT_UNITLESS},
{"sec*kBq/mL", UNIT_KBQ, UNIT_SEC, UNIT_ML, UNIT_UNITLESS},
{"min*kBq/mL", UNIT_KBQ, UNIT_ML, UNIT_ML, UNIT_UNITLESS},
{"min*kBq/mL", UNIT_KBQ, UNIT_MIN, UNIT_ML, UNIT_UNITLESS},
{"sec*Bq/mL", UNIT_BQ, UNIT_SEC, UNIT_ML, UNIT_UNITLESS},
{"min*Bq/mL", UNIT_BQ, UNIT_MIN, UNIT_ML, UNIT_UNITLESS},
{"%", UNIT_PERCENTAGE, UNIT_UNITLESS, UNIT_UNITLESS, UNIT_UNITLESS},
......@@ -802,3 +802,73 @@ int unitDividerMassVolumeConversion(
/*****************************************************************************/
/*****************************************************************************/
/** Multiply two units.
@return enum unit, or 0 (enum UNIT_UNKNOWN) if combination is not supported.
@sa unitConversionFactor, unitCombination
*/
int unitMultiply(
/** Unit A */
int ua,
/** Unit B */
int ub
) {
/* Verify validity of input units */
if(ua<1 || ub<1) return(UNIT_UNKNOWN);
int n=0; while(strlen(tpc_unit[n].name)>0) n++;
if(ua>n-1 || ub>n-1) return(UNIT_UNKNOWN);
/* Collect actual components of units */
int u[4], v[4], nu=0, nv=0;
for(int i=0; i<4; i++) u[i]=v[i]=UNIT_UNITLESS;
if(tpc_unit[ua].u1!=UNIT_UNITLESS) u[nu++]=tpc_unit[ua].u1;
if(tpc_unit[ua].u2!=UNIT_UNITLESS) u[nu++]=tpc_unit[ua].u2;
if(tpc_unit[ua].v1!=UNIT_UNITLESS) v[nv++]=tpc_unit[ua].v1;
if(tpc_unit[ua].v2!=UNIT_UNITLESS) v[nv++]=tpc_unit[ua].v2;
if(tpc_unit[ub].u1!=UNIT_UNITLESS) u[nu++]=tpc_unit[ub].u1;
if(tpc_unit[ub].u2!=UNIT_UNITLESS) u[nu++]=tpc_unit[ub].u2;
if(tpc_unit[ub].v1!=UNIT_UNITLESS) v[nv++]=tpc_unit[ub].v1;
if(tpc_unit[ub].v2!=UNIT_UNITLESS) v[nv++]=tpc_unit[ub].v2;
#if(0)
printf("units: (");
for(int i=0; i<nu; i++) printf(" %s", unitName(u[i]));
printf(" ) / (");
for(int j=0; j<nv; j++) printf(" %s", unitName(v[j]));
printf(" )\n");
#endif
/* Cancel out units if possible */
int cn=0;
for(int i=0; i<nu; i++) for(int j=0; j<nv; j++) if(u[i]==v[j]) {u[i]=v[i]=UNIT_UNITLESS; cn++;}
if(cn>0) {
#if(0)
printf("units after cancelling: (");
for(int i=0; i<nu; i++) printf(" %s", unitName(u[i]));
printf(" ) / (");
for(int j=0; j<nv; j++) printf(" %s", unitName(v[j]));
printf(" )\n");
#endif
for(int i=0; i<nu-1; i++) if(u[i]==UNIT_UNITLESS)
for(int j=i+1; j<nu; i++) if(u[j]!=UNIT_UNITLESS) {
int s=u[i]; u[i]=u[j]; u[j]=s;
}
for(int i=0; i<nv-1; i++) if(v[i]==UNIT_UNITLESS)
for(int j=i+1; j<nv; i++) if(v[j]!=UNIT_UNITLESS) {
int s=v[i]; v[i]=v[j]; v[j]=s;
}
nu-=cn; nv-=cn;
#if(0)
printf("units after removing gaps: (");
for(int i=0; i<nu; i++) printf(" %s", unitName(u[i]));
printf(" ) / (");
for(int j=0; j<nv; j++) printf(" %s", unitName(v[j]));
printf(" )\n");
#endif
}
/* Is the new combination a valid unit? */
if(nu>2 || nv>2) return(UNIT_UNKNOWN);
return(unitCombination(u[0], u[1], v[0], v[1]));
}
/*****************************************************************************/
/*****************************************************************************/
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment