Unidata / UDUNITS-2

API and utility for arithmetic manipulation of units of physical quantities
http://www.unidata.ucar.edu/software/udunits
Other
62 stars 36 forks source link

Less persnickety version of ut_compare()? #39

Open semmerson opened 9 years ago

semmerson commented 9 years ago

Hi,

I found the ut_compare() function, as currently existing the the udunits2 library, to be too strict for my needs.

In particular, for some units, the comparison between the original unit and a back-and-forth transformation to text expression fails. In other words : for some values of unit, ut_compare(unit, (ut_parse(ut_format(unit))) != 0.

I know that a decimal textual representation of a real number cannot be exact, but I believe that we can safely assume that two units whose relative difference is less than 10^-8 or 10^-10 are identical. So, I wrote my own, more tolerant, ut_compare function (see attached source code).

My questions are :

Regards, Bruno.

Bruno Piguet Météo-France Équipe GMEI/TRAMM CNRM/GAME, UMR3589 CNRS/Météo-France Fixe : +33 561079659 Fax : +33 561079627 Postal : 42 Av. G. Coriolis 31057 Toulouse CEDEX 1

piguet-b commented 9 years ago

Here is a link to a gist where I put my example code :

https://gist.github.com/piguet-b/271f856cdd8d45bd6105