Arsinh Function Not Working Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
1100++
Posts: 78
Joined: 10 Feb 2018, 19:05

Arsinh Function Not Working

22 Mar 2020, 12:33

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?
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Arsinh Function Not Working

22 Mar 2020, 13:45

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.
Rohwedder
Posts: 7627
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Arsinh Function Not Working

23 Mar 2020, 12:31

Hallo 1100++ ,
where on this Wikipedia page is your formula?
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Arsinh Function Not Working

23 Mar 2020, 12:51

It looks like 1100++ is doing the series expansion.
Rohwedder
Posts: 7627
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Arsinh Function Not Working

24 Mar 2020, 03:08

But there's no arsinh(x) series for |x| > 1.
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Arsinh Function Not Working

24 Mar 2020, 04:46

Good point. Looks like you identified why it’s not working for x = 5.
1100++
Posts: 78
Joined: 10 Feb 2018, 19:05

Re: Arsinh Function Not Working  Topic is solved

24 Mar 2020, 23:51

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.)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], jameswrightesq, wpulford and 409 guests