Unidata / UDUNITS-2

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

Symbol for degree Fahrenheit not resolved #115

Closed Enchufa2 closed 1 year ago

Enchufa2 commented 1 year ago

In the following example, only the symbol for degree Celsius is resolved:

#include <stdio.h>
#include <udunits2/udunits2.h>

int main() {
    ut_set_error_message_handler((ut_error_message_handler) ut_ignore);
    ut_system *sys = ut_read_xml(NULL);
    ut_encoding enc = UT_UTF8;
    ut_set_error_message_handler((ut_error_message_handler) vprintf);

    ut_unit *deg_C = ut_parse(sys, "deg_C", enc);
    ut_unit *deg_F = ut_parse(sys, "deg_F", enc);
    printf("deg_C symbol: %s\n", ut_get_symbol(deg_C, enc));
    printf("deg_F symbol: %s\n", ut_get_symbol(deg_F, enc));

    ut_free(deg_C);
    ut_free(deg_F);
    ut_free_system(sys);
    return 0;
}

Output:

$ gcc test.c -o test -ludunits2 && ./test
deg_C symbol: °C
deg_F symbol: (null)

Not sure whether this could be an issue in the XML definition or in the library. But I would expect both to be resolved.

More info:

$ cat /etc/redhat-release 
Fedora release 37 (Thirty Seven)
$ rpm -q udunits2
udunits2-2.2.28-6.fc37.x86_64
$ gcc --version
gcc (GCC) 12.2.1 20221121 (Red Hat 12.2.1-4)
semmerson commented 1 year ago

You're correct and I understand why this must seem annoying. Unfortunately, it's unavoidable because the degree Fahrenheit is defined as a degree Rankin offset at 459.67 and the degree Rankin is defined as a degree Kelvin divided by 1.8. I'm afraid the double precision value associated with the degree Fahrenheit isn't able to be completely recovered when going from Fahrenheit to Kelvin and back to Fahrenheit.

For what it's worth, the use-case for the UDUNITS-2 package was to convert any incoming unit into something that could be manipulated under the assumption that the user would specify the final output unit.