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

General FreeBASIC programming questions.
D.J.Peters
Posts: 7429
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: 1655
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: 7429
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: 8347
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: 7429
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: 8347
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: 2690
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: 7429
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: 2690
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: 8347
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?
coderJeff
Site Admin
Posts: 2690
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 22, 2018 14:48

from another thread:
dkl wrote:Looks like "refreshing the repository" (sf.net/p/fbc - Code - Admin - Code - Refresh Repository) fixed it.


fxm, I don't have access to sf.net. Is that the only way that sf.net repo gets updated?
fxm
Posts: 8347
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 22, 2018 15:33

St_W wrote:Still haven't switched the GIT repos from sourceforge to GitHub, but I'm strongly considering it as the repos are synced only very irregularly recently.
D.J.Peters
Posts: 7429
Joined: May 28, 2005 3:28

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

Postby D.J.Peters » Apr 22, 2018 20:46

A new found we can overload the functions left() and right() but not mid() !

D:\FBExamples\fbOop\fString.bas(115) error 4: Duplicated definition, found 'mid' in 'function mid(byref s as fString, byval nFirst as uinteger, byval nChars as uinteger) as fString'


Joshy

Code: Select all

#include once "crt.bi"


#define fALLOC(_x_,_y_) (_x_).c=callocate(sizeof(uinteger)+(_y_)) : *cptr(uinteger ptr,(_x_).c)=n : (_x_).c+=sizeof(uinteger)

type fString
  declare destructor
  declare constructor (byref n as const uinteger=0)
  declare constructor (byref r as fString)
  declare constructor (byref r as const zstring ptr)
  declare operator cast as string
  declare operator cast as any ptr
  declare operator [](byref index as const uinteger) byref as ubyte
  declare operator &=(byref r as fString)
  declare operator &=(byref r as const zstring ptr)
  declare function len as uinteger
  as ubyte ptr c
end type

destructor fString
  c-=sizeof(integer) : deallocate(c)
end destructor

constructor fString(byref n as const uinteger)
  fALLOC(this,n)
end constructor
constructor fString(byref r as fString)
'print "fString(fString) " & r
  var n = r.len() : fALLOC(this,n)
  if n then memcpy(c,r.c,n)
end constructor
constructor fString(byref r as const zstring ptr)
'print "fString(zstring ptr) " & *r
  var n = 0: while r[n] : n+=1 : wend
  fALLOC(this,n) : if n then memcpy(c,r,n)
end constructor

operator fString.cast as string
  var n = this.len()
  var s = space(n)
  if n then memcpy(strptr(s),c,n) 
  'print "op cast string " & s
  return s
end operator 

operator fString.cast as any ptr
  return c
end operator 

operator fString.[](byref index as const uinteger) byref as ubyte
  return c[index]
end operator

operator & (byref a as fString, ByRef b as fString) as fString
'print "op fString & fString"
  var n1=a.len(), n2=b.len()
  dim as fString r = fString(n1+n2)
  if n1 then memcpy(r.c   ,a,n1)
  if n2 then memcpy(r.c+n1,b,n2)
  return r
end operator

operator & (byref a as fString, ByRef s as string) as fString
'print "op fString & string"
  var b = fString(s)
  var n1=a.len(), n2=b.len()
  dim as fString r = fString(n1+n2)
  if n1 then memcpy(r.c   ,a,n1)
  if n2 then memcpy(r.c+n1,b,n2)
  return r
end operator

operator & (byref a as fString, ByRef i as const integer) as fString
'print "op fString & integer"
  var b = fString(str(i))
  var n1=a.len(), n2=b.len()
  dim as fString r = fString(n1+n2)
  if n1 then memcpy(r.c   ,a,n1)
  if n2 then memcpy(r.c+n1,b,n2)
  return r
end operator

operator & (byref a as fString, ByRef d as const double) as fString
'print "op fString & double"
  var b = fString(str(d))
  var n1=a.len(), n2=b.len()
  dim as fString r = fString(n1+n2)
  if n1 then memcpy(r.c   ,a,n1)
  if n2 then memcpy(r.c+n1,b,n2)
  return r
end operator

operator fString.&=(byref r as fString)
  this = this & r
end operator
operator fString.&=(byref r as const zstring ptr)
  this = this & r
end operator

function fString.len as uinteger
  return cptr(uinteger ptr,c)[-1]
end function 
function left(byref s as fString, byval nChars as uinteger) as fString
  var n = s.len() : if nChars>n then nChars=n
  var r=fString(nChars)
  if nChars then memcpy(r.c,s.c,nChars)
  return r
end function
function right(byref s as fString, byval nChars as uinteger) as fString
  var n = s.len() : if nChars>n then nChars=n
  var r =fString(nChars)
  if nChars then memcpy(r.c,s.c+n-nChars,nChars)
  return r
end function
function mid(byref s as fString, byref nFirst as uinteger, byval nChars as uinteger) as fString
  var n = s.len() : if nFirst>n then nChars=0
  var r =fString(nChars)
  if nChars then
    if nFirst+nChars>n then nChars=n-nFirst
    memcpy(r.c,s.c+nFirst,nChars)
  end if 
  return r
end function

dim as fString a = "Hello "
dim as fString b = "World !"
dim as fString c = a & b & " " & 123 & " " & 1.23
c &= c
print c,c.len
print left(c,5)
print mid(c,6,8)
print right(c,8)

sleep

D.J.Peters
Posts: 7429
Joined: May 28, 2005 3:28

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

Postby D.J.Peters » Apr 23, 2018 0:09

As a workaround I declared left,mid and right as static member function but than I get next errors.

Implemnting the static member function without "STATIC" in front I get:
error 147: Default types or suffixes are only valid in -lang deprecated or fblite or qb, found '.' in 'function tString.right(byref s as fString, byval nChars as uinteger) as fString'
with "STATIC" in front I get:
error 258: STATIC used on non-member procedure, before 'tString' in 'static function tString.right(byref s as fString, byval nChars as uinteger) as fString'

Why is it ?

Thank you

Joshy

Code: Select all

#include once "crt.bi"

#define fALLOC(_x_,_y_) (_x_).c=callocate(sizeof(uinteger)+(_y_)) : *cptr(uinteger ptr,(_x_).c)=(_y_) : (_x_).c+=sizeof(uinteger)

type fString
  declare destructor
  declare constructor
  declare constructor (byref nChars as const uinteger)
  declare constructor (byref aString as fString)
  declare constructor (byref aZString as const zstring ptr)
  declare operator cast as string
  declare operator cast as any ptr
  declare operator [](byref aIndex as const uinteger) byref as ubyte
  declare operator &=(byref aString as fString)
  declare operator &=(byref aZString as const zstring ptr)
  declare function len as uinteger
 
  as ubyte ptr c
 
  declare static function left_(byref s as fString, byval nChars as uinteger) as fString
  declare static function right_(byref s as fString, byval nChars as uinteger) as fString
  declare static function mid_  (byref s as fString, byref nFirst as uinteger, byval nChars as uinteger) as fString
end type

static function fString.left_(byref s as fString, byval nChars as uinteger) as fString
  var n = s.len() : if nChars>n then nChars=n
  var r=fString(nChars)
  if nChars then memcpy(r.c,s.c,nChars)
  return r
end function

static function tString.right(byref s as fString, byval nChars as uinteger) as fString
  var n = s.len() : if nChars>n then nChars=n
  var r =fString(nChars)
  if nChars then memcpy(r.c,s.c+n-nChars,nChars)
  return r
end function
static function tString.mid(byref s as fString, byref nFirst as uinteger, byval nChars as uinteger) as fString
  var n = s.len()
  if nFirst>n thena
    nChars=0
  elseif nFirst<1 then
    nChars=0
  end if 
  if nChars then
    nFirst-=1
    if nFirst+nChars>n then nChars=n-nFirst
    var r=fString(nChars)
    memcpy(r.c,s.c+nFirst,nChars)
    return r
  end if 
  return fString()
end function
fxm
Posts: 8347
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 23, 2018 5:04

Typos:
- tString instead of fString
- underscores missing in function member definitions.

Code without underscore:

Code: Select all

#include once "crt.bi"

#define fALLOC(_x_,_y_) (_x_).c=callocate(sizeof(uinteger)+(_y_)) : *cptr(uinteger ptr,(_x_).c)=(_y_) : (_x_).c+=sizeof(uinteger)

type fString
  declare destructor
  declare constructor
  declare constructor (byref nChars as const uinteger)
  declare constructor (byref aString as fString)
  declare constructor (byref aZString as const zstring ptr)
  declare operator cast as string
  declare operator cast as any ptr
  declare operator [](byref aIndex as const uinteger) byref as ubyte
  declare operator &=(byref aString as fString)
  declare operator &=(byref aZString as const zstring ptr)
  declare function len as uinteger
 
  as ubyte ptr c
 
  declare static function left(byref s as fString, byval nChars as uinteger) as fString
  declare static function right(byref s as fString, byval nChars as uinteger) as fString
  declare static function mid  (byref s as fString, byref nFirst as uinteger, byval nChars as uinteger) as fString
end type

static function fString.left(byref s as fString, byval nChars as uinteger) as fString
  var n = s.len() : if nChars>n then nChars=n
  var r=fString(nChars)
  if nChars then memcpy(r.c,s.c,nChars)
  return r
end function

static function fString.right(byref s as fString, byval nChars as uinteger) as fString
  var n = s.len() : if nChars>n then nChars=n
  var r =fString(nChars)
  if nChars then memcpy(r.c,s.c+n-nChars,nChars)
  return r
end function
static function fString.mid(byref s as fString, byref nFirst as uinteger, byval nChars as uinteger) as fString
  var n = s.len()
  if nFirst>n then
    nChars=0
  elseif nFirst<1 then
    nChars=0
  end if
  if nChars then
    nFirst-=1
    if nFirst+nChars>n then nChars=n-nFirst
    var r=fString(nChars)
    memcpy(r.c,s.c+nFirst,nChars)
    return r
  end if
  return fString()
end function
to contiue

Return to “General”

Who is online

Users browsing this forum: Provoni and 4 guests