QB64 Phoenix Edition
function "_ARCSEC()" is wrong? - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11)
+--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2)
+--- Thread: function "_ARCSEC()" is wrong? (/showthread.php?tid=2125)

Pages: 1 2


function "_ARCSEC()" is wrong? - BSpinoza - 10-28-2023

One example:
Code: (Select All)
PRINT _ARCSEC(-2.5)

results in "illegal function call".

But " .... the domain of sec inverse x is (-∞, -1] U [1, ∞) (being the range of secant function) and the range of arcsec is [0, π/2) U (π/2, π] (principal branch of sec x). ..."

From: https://www.cuemath.com/trigonometry/sec-inverse-x/

The result should be 1.982313172862385.

An alternative formula gives the correct result:

arcsec(x) = acos(1/x)

Code: (Select All)
PRINT _ACOS(1/-2.5)

This works fine and delivers: 1.982313172862385. 

The Help says:
Quote:  FUNCTION ARCSEC (x) ' Inverse Secant
    IF x < 1 THEN ARCSEC = ATN(x / SQR(1 - x * x)) + (SGN(x) - 1) * (2 * ATN(1)) ELSE BEEP
END FUNCTION
 I think this is not correct!   

 Correct for the domain is:  |x| > 1 

What do our mathematical specialists think?


RE: function "_ARCSEC()" is wrong? - Kernelpanic - 10-28-2023

Quote:What do our mathematical specialists think?  Rolleyes
If x < 1, the result cannot be correct because there is an error message. If x > 1 the result is zero.
I should first go through the formula in peace .  . .

[Image: ARCSEC.jpg]


RE: function "_ARCSEC()" is wrong? - bplus - 10-28-2023

When x < 1 do what you did @BSpinoza because even the help says you can't do it with a straight function call. Your solution looks simpler, so if it works, GO FOR IT! Smile


RE: function "_ARCSEC()" is wrong? - SMcNeill - 10-28-2023

Looks like a glitch to me.

ARCSEC should work in a range from: x ≤ -1 or x ≥ 1


RE: function "_ARCSEC()" is wrong? - bplus - 10-28-2023

Why is there a forumla for ArcSec given below for Wiki on _ArcSec Keyword?

Something is wrong? a typo a wrong word? something...

Update: Oh that was from old QB stuff, who cares? water under the bridge


RE: function "_ARCSEC()" is wrong? - bplus - 10-28-2023

(10-28-2023, 02:42 PM)bplus Wrote: Why is there a forumla for ArcSec given below for Wiki on _ArcSec Keyword?

Something is wrong? a typo a wrong word? something...

Update: Oh that was from old QB stuff, who cares? water under the bridge

PRINT _ARCSEC(-2.5) OK it's broken!


RE: function "_ARCSEC()" is wrong? - Kernelpanic - 10-28-2023

The formula in the wiki seems to be wrong. Is the result of the formula from the QBasic-Manual correct?

Code: (Select All)

Option _Explicit

Dim As Integer x

x = 0
Const PI = 1.141593

'Print _Arcsec(-2.5
Print
Print _Acos(1 / -2.5)
Print
Print ARCSECF(-2.5)

End

Function ARCSECF (x) ' Inverse Secant

  Dim As Double Arcsec

  If x <= 1 Or x >= 1 Then
    Arcsec = Atn(Sqr(x * x - 1)) + Sgn(Sgn(x) - 1) * PI
  Else Beep
  End If
  ARCSECF = Arcsec
End Function



RE: function "_ARCSEC()" is wrong? - bplus - 10-28-2023

How does BSpinoza's solution look to you, seems like good idea to me.


RE: function "_ARCSEC()" is wrong? - Kernelpanic - 10-28-2023

Something else comes out of it too. Wait for @Spinoza.

Code: (Select All)

Option _Explicit

Declare Function ARCSECF(x As Integer) As Double

Dim As Integer arcsecw, x

x = -2.5

Const PI = 1.141593

'Print _Arcsec(-2.5
Print
Print _Acos(1 / -2.5)
Print
Print ARCSECF(-2.5)

Print
arcsecw = _Acos(1 / x)
Print arcsecw

End

Function ARCSECF (x As Integer) 'Inverse Secant

  Dim As Double Arcsec

  If x <= 1 Or x >= 1 Then
    Arcsec = Atn(Sqr(x * x - 1)) + Sgn(Sgn(x) - 1) * PI
  Else Beep
  End If
  ARCSECF = Arcsec
End Function



RE: function "_ARCSEC()" is wrong? - Kernelpanic - 10-28-2023

Correction: @BSpinoza's formula is correct.

Code: (Select All)

Option _Explicit

Declare Function ARCSECF(x As Double) As Double

Dim As Double arcsecw, x

x = -2.5

Const PI = 1.141593

'Print _Arcsec(-2.5
Print
Print _Acos(1 / -2.5)
Print
Print ARCSECF(-2.5)

Print 'Spinoza's Formel
arcsecw = _Acos(1 / x)
Print Using "#.#########"; arcsecw

End