[solved]I can overlod sin,cos,tan,exp ... but not sqr ?

General FreeBASIC programming questions.
D.J.Peters
Posts: 7347
Joined: May 28, 2005 3:28

[solved]I can overlod sin,cos,tan,exp ... but not sqr ?

Postby D.J.Peters » Apr 16, 2018 1:19

I overload some operators for a complex number class but isn't allowed to overwrite SQR why is it ?

operator sqr(byref z as complex) as complex

Joshy

this works

Code: Select all

operator tan(byref z as complex) as complex
  return sin(z) / cos(z) ' <- sin/cos are overloaded also
end operator
Last edited by D.J.Peters on Apr 17, 2018 3:27, edited 1 time in total.
srvaldez
Posts: 1514
Joined: Sep 25, 2005 21:54

Re: I can overlod sin,cos,tan,exp ... but not sqr ?

Postby srvaldez » Apr 16, 2018 2:19

it's been like that for a long time, the same problem exist with the atan2 function
the hack work around is something like the following

Code: Select all

function sqr_(byval x as double) as double
   function = sqr(x)
end function

#undef sqr

function sqr overload(byval x as double) as double
   function = sqr_(x)
end function

function sqr(byref x as complex) as complex
   function = you_sqr(x)
end function
D.J.Peters
Posts: 7347
Joined: May 28, 2005 3:28

Re: I can overlod sin,cos,tan,exp ... but not sqr ?

Postby D.J.Peters » Apr 16, 2018 2:28

Hello srvaldez to late but thank you I solved it with #undef also :-)

But I don't mark it as [solved] may it needs a bug report.

Joshy
fxm
Posts: 7737
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: I can overlod sin,cos,tan,exp ... but not sqr ?

Postby fxm » Apr 16, 2018 16:02

For now, I have updated the documentation:
- KeyPgExp → fxm [Overloadable]
- KeyPgAtan2 → fxm [Not overloadable]
- KeyPgSqr → fxm [Not overloadable]
- KeyPgAtn → fxm [Overloadable]
- KeyPgTan → fxm [Overloadable]
- KeyPgAcos → fxm [Overloadable]
- KeyPgCos → fxm [Overloadable]
- KeyPgAsin → fxm [Overloadable]
- KeyPgSin → fxm [Overloadable]
- KeyPgLog → fxm [Overloadable]
- KeyPgOpConcatConvert → fxm [Overloadable]
D.J.Peters
Posts: 7347
Joined: May 28, 2005 3:28

Re: I can overlod sin,cos,tan,exp ... but not sqr ?

Postby D.J.Peters » Apr 16, 2018 19:25

@fxm thank you for your work.

Do you know why the SQR overload limitation exists ?

Joshy
fxm
Posts: 7737
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: I can overlod sin,cos,tan,exp ... but not sqr ?

Postby fxm » Apr 16, 2018 19:50

No I do not know.
coderJeff
Site Admin
Posts: 2497
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: I can overlod sin,cos,tan,exp ... but not sqr ?

Postby coderJeff » Apr 16, 2018 23:48

I don't know either. Maybe it was missed, or more likely there was a reason years ago (or still is).
There are no tests in fbc's test-suite for overloaded builtins; could be sleeping bugs waiting.

Otherwise, pretty easy to add; probably atan2 also. in 2 files: 1 line changed, 3 lines added.

Code: Select all

diff --git a/src/compiler/parser-assignment.bas b/src/compiler/parser-assignment.bas
index 328c133b6..c94cb8aa6 100644
--- a/src/compiler/parser-assignment.bas
+++ b/src/compiler/parser-assignment.bas
@@ -68 +68 @@ function cOperator( byval is_overload as integer ) as integer
-            FB_TK_ATN, FB_TK_LEN, _
+            FB_TK_ATN, FB_TK_SQR, FB_TK_LEN, _
@@ -101,0 +102 @@ function cOperator( byval is_overload as integer ) as integer
+               case FB_TK_SQR  : op = AST_OP_SQRT
diff --git a/src/compiler/symb-mangling.bas b/src/compiler/symb-mangling.bas
index 2863e6fbb..b558d96fd 100644
--- a/src/compiler/symb-mangling.bas
+++ b/src/compiler/symb-mangling.bas
@@ -1069,0 +1070,3 @@ private function hGetOperatorName( byval proc as FBSYMBOL ptr ) as const zstring
+       case AST_OP_SQRT
+               function = @"v13sqr"
D.J.Peters
Posts: 7347
Joined: May 28, 2005 3:28

Re: I can overlod sin,cos,tan,exp ... but not sqr ?

Postby D.J.Peters » Apr 17, 2018 3:26

Yes a sleeping bug with an easy fix !

Good job.

Joshy
coderJeff
Site Admin
Posts: 2497
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: [solved]I can overlod sin,cos,tan,exp ... but not sqr ?

Postby coderJeff » Apr 21, 2018 3:04

Added "Allow overload operator SQR()" to fb 1.06.0
fxm
Posts: 7737
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: [solved]I can overlod sin,cos,tan,exp ... but not sqr ?

Postby fxm » Apr 21, 2018 14:08

coderJeff wrote:Added "Allow overload operator SQR()" to fb 1.06.0

Can this be activated for a St_W's next build?

Return to “General”

Who is online

Users browsing this forum: No registered users and 5 guests