[Done] Wrong results in trigonometric and inverse hyperbolic functions

Post bugreports for the Windows version here
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

[Done] Wrong results in trigonometric and inverse hyperbolic functions

Post by STARGÅTE »

I observed a lot of bug in the math library regarding the trigonometric and inverse hyperbolic functions:
(PB 5.73 LTS, PB 6.00 Alpha 3)
_________________________

For functions like Sin() and Cos() the documentation writes:
Return value
Returns the sine of the angle. The result will be between -1.0 and 1.0.
However, large numbers generate strange results, which look like that the argument is just passed through:
(I know the numbers can not be correct for such high numbers, but the result should between -1.0 and 1.0)

Code: Select all

Debug Sin(1.0e18)    ; -0.99281610405300347
Debug Sin(1.0e19)    ; 10000000000000000000.0     Why? Should be between -1 and 1

Debug Cos(1.0e18)    ; 0.11965025504785125
Debug Cos(1.0e19)    ; 10000000000000000000.0     Why? Should be between -1 and 1

Debug Tan(1.0e18)    ; -8.2976513811520967
Debug Tan(1.0e19)    ; NaN                        Why? 
_________________________

For functions like ACosH, ASinH, and ATanH, the result is Infinity or 0.0 even if the result can be represented by a double:

Code: Select all

Debug ACosH(1.0e154)   ; 355.29125150164299
Debug ACosH(1.0e155)   ; +Infinity                Why? Should be 357.59383659463703 = Log(2)+Log(1.0e155)

Debug ASinH(1.0e154)  ; 355.29125150164299
Debug ASinH(1.0e155)  ; +Infinity                 Why? Should be 357.59383659463703 = Log(2)+Log(1.0e155)

Debug ASinH(1.0e8)    ; 19.113827924512311
Debug ASinH(-1.0e8)   ; -Infinity                 Why? Should be -19.113827924512311

Debug StrD(ASinH(1.0e-15), 30)  ; 0.000000000000001110223024625156
Debug StrD(ASinH(1.0e-16), 30)  ; 0.000000000000000000000000000000     Why exact 0.0? Should be 1.0e-16 = 0.0000000000000001...

Debug StrD(ATanH(1.0e-16), 30)  ; 0.000000000000000111022302462516
Debug StrD(ATanH(1.0e-17), 30)  ; 0.000000000000000000000000000000     Why exact 0.0? Should be 1.0e-17 = 0.00000000000000001...
_________________________

Both categories can be solved by using a private procedure, but it would be good to avoid such bypass.

Code: Select all

Procedure.d MySin(Value.d)
	ProcedureReturn Sin(Mod(Value, 2*#PI))
EndProcedure

Define Ten.i, Value.d
For Ten = 0 To 30
	Value = Pow(10, Ten)
	Debug RSet(StrD(Sin(Value)), 35) + RSet(StrD(MySin(Value)), 35)
Next

Code: Select all

Procedure.d MyASinH(Value.d)
	If Abs(Value) > 1.0e154
		ProcedureReturn 0.6931471805599453094 + Log(Abs(Value))
	ElseIf Abs(Value) < 1.0e-4
		ProcedureReturn Value - Value*Value*Value/6.0
	ElseIf Abs(Value) < 1.0e-3
		ProcedureReturn Value - Value*Value*Value/6.0 + Value*Value*Value*Value*Value*3.0/40.0
	EndIf
	ProcedureReturn Sign(Value) * ASinH(Abs(Value))
EndProcedure

Define Ten.i, Value.d

For Ten = 0 To -20 Step -1
	Value = Pow(10, Ten)
	Debug RSet(StrD(ASinH(Value),30), 35) + RSet(StrD(MyASinH(Value),30), 35)
Next
For Ten = 0 To 12
	Value = - Pow(10, Ten)
	Debug RSet(StrD(ASinH(Value)), 35) + RSet(StrD(MyASinH(Value)), 35)
Next
For Ten = 150 To 160
	Value = Pow(10, Ten)
	Debug RSet(StrD(ASinH(Value)), 35) + RSet(StrD(MyASinH(Value)), 35)
Next
Btw: The first bug (trigonometric functions) is solved in the C-backend.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Fred
Administrator
Administrator
Posts: 16618
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Wrong results in trigonometric and inverse hyperbolic functions

Post by Fred »

Fixed the Sin(), Cos(), Tan() issue with AMS backend. For the remaining bugs, it's Windows only as we use substitutions functions
instead of libc one, due to old MSVCRT which doesn't support them. May be if we migrate to a more recent MSVCRT or something we could have better functions.
User avatar
IceSoft
Addict
Addict
Posts: 1616
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Wrong results in trigonometric and inverse hyperbolic functions

Post by IceSoft »

Fred wrote: Sat Apr 08, 2023 11:59 am May be if we migrate to a more recent MSVCRT or something we could have better functions.
Thats will be a great step too
Belive!
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Fred
Administrator
Administrator
Posts: 16618
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Wrong results in trigonometric and inverse hyperbolic functions

Post by Fred »

Fixed.
Post Reply