lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2013/1/18 martinwguy <martinwguy@gmail.com>:
> On 18 January 2013 14:38, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>> There's no math.asinh, math.acosh, math.atanh.
>>
>> The reason presumably being that ANSI C does not provide
>> them.
>>
>> But if your C compiler provides it, you may find the included
>> file useful.
>
> Or
> math.asinh = function (x) return math.log(x + math.sqrt(x * x + 1)); end
> math.acosh = function (x) return math.log(x + math.sqrt(x * x - 1)); end
> math.atanh = function(x) return (math.log(1 + x) - math.log(1 - x)) / 2; end
>

That's not good enough.

The asinh and atanh formulas are known to be inaccurate near the
origin because of smearing.  When x*x+1 is formed, digits get thrown
away. When x is added to the sqrt of that, more digits get thrown away.
When log of something close to 1 is calculated, 1 is subtracted in the
process.  This cancellation does not by itself introduce a new error,
but exposes the fact that the digits are already irretrievably gone.

E.g. using the above, and compare with asin which is supplied,

> x=1e-7
> =x-math.asinh(math.sinh(x))
5.66321331625e-17
> return x-math.asin(math.sin(x))
0

The error is always at or about 1e-16, which is crude when
the correct value is << 1.

Using the invhyp module,
> =x-math.asinh(math.sinh(x))
0