LAL  7.5.0.1-08ee4f4
UnitCompare.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007 Jolien Creighton, Kipp Cannon, John Whelan
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19 
20 #include <math.h>
21 #include <string.h>
22 
23 #include <lal/LALStdlib.h>
24 #include <lal/Units.h>
25 #include <lal/XLALError.h>
26 
27 /**
28  * \addtogroup UnitCompare_c
29  * \author J. T. Whelan <john.whelan@ligo.org>
30  *
31  * \brief Function to compare two \c LALUnit structures.
32  */
33 /** @{ */
34 
35 /** Return 1 if a unit is dimensionless, 0 otherwise */
37 {
38  int i;
39 
40  if(!unit)
42 
43  for(i = 0; i < LALNumUnits; i++)
44  if(unit->unitNumerator[i] != unit->unitDenominatorMinusOne[i])
45  return 0;
46  return 1;
47 }
48 
49 
50 /** Return the unit's prefactor */
52 {
53  if(!unit)
55  return pow(10.0, unit->powerOfTen);
56 }
57 
58 
59 /**
60  * Return the ratio unit1 / unit2
61  */
62 REAL8 XLALUnitRatio(const LALUnit *unit1, const LALUnit *unit2)
63 {
64  LALUnit tmp;
65 
66  if(!unit1 || !unit2)
68 
69  XLALUnitDivide(&tmp, unit1, unit2);
70  if(XLALUnitIsDimensionless(&tmp))
71  return XLALUnitPrefactor(&tmp);
73 }
74 
75 
76 
77 /**
78  * Returns 0 if the the normal form of the two unit
79  * structures are the same or > 0 if they are different. It returns
80  * #XLAL_FAILURE and ::xlalErrno is set to #XLAL_EFAULT if
81  * one of the input pointers is \c NULL.
82  *
83  * Example:
84  * \code
85  * if(XLALUnitCompare(&unit1, &unit2)) {
86  * units_are_not_equal();
87  * }
88  * \endcode
89  */
90 int XLALUnitCompare( const LALUnit *unit1, const LALUnit *unit2 )
91 {
92  LALUnit unitOne, unitTwo;
93 
94  if ( ! unit1 || ! unit2 )
96 
97  unitOne = *unit1;
98  unitTwo = *unit2;
99 
100  /* normalize the units */
101  if ( XLALUnitNormalize( &unitOne ) )
103  if ( XLALUnitNormalize( &unitTwo ) )
105 
106  /* factors of 10 disagree? */
107  if ( unitOne.powerOfTen != unitTwo.powerOfTen )
108  return 1;
109 
110  /* powers of dimensions disagree? use memcmp() to compare the arrays */
111  if ( memcmp( unitOne.unitNumerator, unitTwo.unitNumerator, LALNumUnits * sizeof( *unitOne.unitNumerator ) ) )
112  return 1;
113  if ( memcmp( unitOne.unitDenominatorMinusOne, unitTwo.unitDenominatorMinusOne, LALNumUnits * sizeof( *unitOne.unitDenominatorMinusOne ) ) )
114  return 1;
115 
116  /* agree in all possible ways */
117  return 0;
118 }
119 
120 /** @} */
double REAL8
Double precision real floating-point number (8 bytes).
@ LALNumUnits
The number of units.
Definition: LALDatatypes.h:480
int XLALUnitIsDimensionless(const LALUnit *unit)
Return 1 if a unit is dimensionless, 0 otherwise.
Definition: UnitCompare.c:36
REAL8 XLALUnitRatio(const LALUnit *unit1, const LALUnit *unit2)
Return the ratio unit1 / unit2.
Definition: UnitCompare.c:62
int XLALUnitCompare(const LALUnit *unit1, const LALUnit *unit2)
Returns 0 if the the normal form of the two unit structures are the same or > 0 if they are different...
Definition: UnitCompare.c:90
REAL8 XLALUnitPrefactor(const LALUnit *unit)
Return the unit's prefactor.
Definition: UnitCompare.c:51
LALUnit * XLALUnitDivide(LALUnit *output, const LALUnit *unit1, const LALUnit *unit2)
UNDOCUMENTED.
Definition: UnitMultiply.c:108
int XLALUnitNormalize(LALUnit *unit)
Returns 0 upon success or XLAL_FAILURE if the input pointer is NULL, in which case xlalErrno is set t...
Definition: UnitNormalize.c:72
#define XLAL_ERROR_REAL8(...)
Macro to invoke a failure from a XLAL routine returning a REAL8.
Definition: XLALError.h:752
#define XLAL_ERROR(...)
Macro to invoke a failure from a XLAL routine returning an integer.
Definition: XLALError.h:700
@ XLAL_EFAULT
Invalid pointer.
Definition: XLALError.h:408
@ XLAL_EFUNC
Internal function call failed bit: "or" this with existing error number.
Definition: XLALError.h:462
@ XLAL_EDIMS
Wrong dimensions.
Definition: XLALError.h:421
This structure stores units in the mksA system (plus Kelvin, Strain, and ADC Count).
Definition: LALDatatypes.h:498
INT2 powerOfTen
Overall power-of-ten scaling is 10^powerOfTen.
Definition: LALDatatypes.h:499
UINT2 unitDenominatorMinusOne[LALNumUnits]
Array of unit power denominators-minus-one.
Definition: LALDatatypes.h:501
INT2 unitNumerator[LALNumUnits]
Array of unit power numerators.
Definition: LALDatatypes.h:500