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

Rounding errors #35

Closed mark-r-cullen closed 9 years ago

mark-r-cullen commented 9 years ago

Hi,

I've successfully got UDUnits working within C# in Visual Studio and am trying to do a simple temperature conversion from Celsius to Fahrenheit and vice versa. I am running in to some rounding errors.

e.g 32.0 degF = 0.000000000000035527136788005009 degC

I tried debugging the udunits dll, but the only thing I could see is that the Galilean parameters for the conversion seemed to have a rounding error to start with. When doing 0.0 degC to degF, the fromProduct->Galilean->scale is 1.7999999999999998 and the fromProduct->Galilean->intercept is -459.67000000000002 (line 3635 in unitcore.c). I am unable to figure out exactly how these values are set so far.

Is this something that can be investigated and fixed?

Regards, Mark

semmerson commented 9 years ago

Nothing can be done. Floating-point values are inherently fuzzy (the value 0.1, for example, can't be represented exactly). See http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html.

mark-r-cullen commented 9 years ago

Aware of that, but not entirely convinced nothing can be done with such small numbers and a relatively simple conversion. I've got another (admittedly custom) library that can do these examples just fine...

mark-r-cullen commented 9 years ago

Okay, I've looked in to this more this morning and found it disappears off in to some YACC parsing stuff, and eventually found an atof(). The result of parsing the Kelvin is 273.1444444...

The other library is pure C#, so I guess the two compilers must just behave differently somehow as it doesn't seem to suffer from this problem. double.Parse("273.15") seems to return 273.15 exactly. A bit of a shame...

semmerson commented 9 years ago

Sounds like your atof() is bad.

I've replaced the use of atof() with strtod(). Maybe that will help. A new release should be out today.