Page 1 of 1

Arsinh Function Not Working

Posted: 22 Mar 2020, 12:33
by 1100++
I'm trying to write a function that calculates the arsinh of a number. I'm using a formula found on this Wikipedia page to calculate it.

Here's my code:

Code: Select all

arsinh(x) {
	static terms := [0, 0, 0, 0, 0, 0, 0, 0]
	If x is not number
		return
	power := 1, s := abs(x)
	While s > .0625
		s := sqrt(sqrt(s * s + 1) - 1), power *= 2
	terms[1] := x := (x < 0 ? -1 : 1) * s, term := 2, term_pwr := 3, xc := .5, xt := x, xf := x * x
	While term <= 8
		terms[term] := (term & 1 ? 1 : -1) * xc * (xt *= xf) / term_pwr, ++term, xc *= term_pwr, xc /= term_pwr + 1, term_pwr += 2
	sum := 0, term := 8
	While term > 0
		sum += terms[term--]
	return sum * power
}
However, this function gives erroneous results. For example, while arsinh(5) is about 2.312438, this function returns 57.776128 when given 5, which is off by more than an order of magnitude.

Can someone tell me what's wrong with this function?

Re: Arsinh Function Not Working

Posted: 22 Mar 2020, 13:45
by Helgef
I don't know what is wrong. From your link I get:

Code: Select all

arsinh(x) {
	return ln(x+sqrt(x**2+1))
}
Cheers.

Re: Arsinh Function Not Working

Posted: 23 Mar 2020, 12:31
by Rohwedder
Hallo 1100++ ,
where on this Wikipedia page is your formula?

Re: Arsinh Function Not Working

Posted: 23 Mar 2020, 12:51
by boiler
It looks like 1100++ is doing the series expansion.

Re: Arsinh Function Not Working

Posted: 24 Mar 2020, 03:08
by Rohwedder
But there's no arsinh(x) series for |x| > 1.

Re: Arsinh Function Not Working

Posted: 24 Mar 2020, 04:46
by boiler
Good point. Looks like you identified why it’s not working for x = 5.

Re: Arsinh Function Not Working  Topic is solved

Posted: 24 Mar 2020, 23:51
by 1100++
Never mind, I know what's wrong. I got the argument scaling formula wrong. Here's my corrected function:

Code: Select all

arsinh(x) {
	static terms := [0, 0, 0, 0, 0, 0, 0, 0]
	If x is not number
		return
	power := 1, s := abs(x)
	While s > .0625
		s := sqrt((sqrt(s * s + 1) - 1) / 2), power *= 2
	terms[1] := x := (x < 0 ? -1 : 1) * s, term := 2, term_pwr := 3, xc := .5, xt := x, xf := x * x
	While term <= 8
		terms[term] := (term & 1 ? 1 : -1) * xc * (xt *= xf) / term_pwr, ++term, xc *= term_pwr, xc /= term_pwr + 1, term_pwr += 2
	sum := 0, term := 8
	While term > 0
		sum += terms[term--]
	return sum * power
}
(I started composing this post a few minutes after I posted the original topic, but didn't get around to posting it until now.)