I am trying to calculate dewpoint in a 9S12X processor, and need a way to approximate the natural log function. I have used a simple polynomial approximation, -x - x^2/2 - x^3/3..., but it breaks down below a certain relative humidity.

Does anyone know of any methods or routines, preferrable in assembly?

Thanks,

Chris

Added p/n to subject.

Hello Chris, and welcome to the forum.

I assume that you require the natural log of the fractional relative humidity value. The problem with using the Taylor series for ln(1-x) is that the series converges very slowly, and extremely slowly for low values of RH.

An alternative method may be to consider deriving the the base 2 log, and to then to use the identity:

ln(h) = ln(2) * log2(h) = 0.6931 * log2(h)

Since 0 < h < 1, the log value will always be negative, so the sign may be ignored within the derivation (and the result then subtracted, rather than added). The log result will have an integer part and a fractional part.

By initially using a base of 2, the integer part may be very easily derived by counting the number of left shifts of the binary h value, until there are no leading zeros remaining. After shifting has occurred, the value will be within the range 0.5 <= h < 1, and the fractional part of the result will be derived from this value. With this limited range, it would seem that a table lookup and interpolate method may be appropriate. The use of linear interpolation between the the table values should give good accuracy for quite a small table. My understanding is that the HCS12 has table lookup and interpolation instructions available, to simplify this process.

The result obtained would then be multiplied by the binary fractional value equivalent to 0.6931 ($B172 for a 16-bit fraction).

Regards,

Mac