Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function "_ARCSEC()" is wrong?
#11
You can find it here too:

https://mathworld.wolfram.com/InverseSecant.html

pls. see formula (8).
Reply
#12
The same problem with QB64 _ARCCSC-Funktion: domain of x for real result: |x| >1  !
Pls. check this sources:
https://en.wikipedia.org/wiki/Inverse_tr..._functions
https://mathworld.wolfram.com/InverseCosecant.html
https://www.wolframalpha.com/input/?i=arccosec%28x%29

But in the QB64 help and WIKI:

Code: (Select All)

FUNCTION ARCCSC (x) ' Inverse CoSecant
    IF x < 1 THEN ARCCSC = ATN(1 / SQR(1 - x * x)) + (SGN(x) - 1) * (2 * ATN(1)) ELSE BEEP
END FUNCTION


Example:

Code: (Select All)
PRINT _ARCCSC(0.5) 
  
gives: 0.52358...

But arccosec(0.5) is not defined!

The alternative formula for  _ARCCSC(x):

arccosec(x) = arcsin(1/x)

Code: (Select All)
PRINT _ARCCSC(0.5)
PRINT _ASIN(1 / 0.5)

This delivers:

0.52358...
nan

"nan" is correct here. 

conclusion:
I think both functions _ARCSEC and _ARCCSC are not correct in QB64!
Reply
#13
It goes to show how much people use these extended functions in QB64 -- they're completely backwards!

Code: (Select All)
 double func_arcsec(double num) {
    int sign = (num > 0) - (num < 0);
    if (num < -1 || num > 1) {
        error(5);
        return 0;
    }
    return std::atan(num / std::sqrt(1 - num * num)) + (sign - 1) * (2 * std::atan(1));
}

double func_arccsc(double num) {
    int sign = (num > 0) - (num < 0);
    if (num < -1 || num > 1) {
        error(5);
        return 0;
    }
    return std::atan(num / std::sqrt(1 - num * num)) + (sign - 1) * (2 * std::atan(1));
}

If you look in libqb.cpp, you'll find the code above. Take a moment to look at it and you'll see that the logic here is completely reversed! We're tossing errors in the range where we should give valid results, and we're giving valid results in the range where we should be tossing errors!

Fix is rather simple though -- just go into libqb.cpp and down around line 33925, change the above to the following:

Code: (Select All)
double func_arcsec(double num) {
    int sign = (num > 0) - (num < 0);
    if (std::abs(num) < 1) {
        error(5);
        return 0;
    }
    return std::atan(num / std::sqrt(1 - num * num)) + (sign - 1) * (2 * std::atan(1));
}

double func_arccsc(double num) {
    int sign = (num > 0) - (num < 0);
    if (std::abs(num) < 1) {
        error(5);
        return 0;
    }
    return std::atan(num / std::sqrt(1 - num * num)) + (sign - 1) * (2 * std::atan(1));
}

Save and you should be good to go. (If not, then in the QB64PE menu, click DEBUG and then PURGE C++ LIBRARIES. That should rebuild the c-side of things and correct the issue after you've saved the changes above.)
Reply
#14
Quote:Save and you should be good to go. (If not, then in the QB64PE menu, click DEBUG and then PURGE C++ LIBRARIES. That should rebuild the c-side of things and correct the issue after you've saved the changes above.)
I did that, but I still get an error message.
[Image: libqp-c-2023-10-30-142654.jpg]

[Image: Illegal2023-10-30-142841.jpg]
Reply
#15
(10-30-2023, 01:33 PM)Kernelpanic Wrote:
Quote:Save and you should be good to go. (If not, then in the QB64PE menu, click DEBUG and then PURGE C++ LIBRARIES. That should rebuild the c-side of things and correct the issue after you've saved the changes above.)
I did that, but I still get an error message.
[Image: libqp-c-2023-10-30-142654.jpg]

[Image: Illegal2023-10-30-142841.jpg]

You've fixed it!

Before, it was running and giving answers for you, when it shouldn't have.  Now, it errors out in the range of -1 to 1, which is where the second method is printing "NaN" -- Not a Number as a result.  Smile

That ERROR message *is* the fix in this case.
Reply
#16
Quote:That ERROR message *is* the fix in this case.
I thought NAN was the correct answer. The error message may be correct, but it's somehow . . . not so well. I think that NAN would be better.
Reply
#17
Both functions patched and changes are pushed into the repo.  This issue should be corrected in the next release.
Reply




Users browsing this thread: 7 Guest(s)