max() min() abs()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

max() min() abs()

Post by blueznl »

sometimes i run into simple things missing...

abs() only for floats?
no min() and max() function

(yeah, i wrote procedures for them, it's just funny that they are missing in such an advanced :wink: basic)

and why different functions for floats and ints? the compiler should be able to see what kind of variable (type) follows the keywords, and thus use the proper code

come to think of it, this goes for str() as well...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Yes, Abs() should return an int if the input parameter is int, and float if it is float.

min(), max(), and i remember a useful function in BlitzBasic2, and perhaps in gfa: it is Wrap(number, left_end, right_end) in which you obtain a wrapped value inside that range, this is, if range is 3-78 and you put a number of 80, then the result is 4; if range is 0-360 and you put 380, then the result is 20.
And there could be another function Limit(number, left_end, right_end) in which the result is a clipped value, this is, if you put 380 in a range of 0-360, the result is 360.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

fred (i know you are reading this :-)) is there a chance that in a future version of purebasic the compiler will detect the type of var, so some of the current functions could be 'enhanced' to apply to ints as well as floats?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
LCD
Enthusiast
Enthusiast
Posts: 206
Joined: Sun Jun 01, 2003 10:55 pm
Location: Austria, Vienna
Contact:

Post by LCD »

Until this is supported, you can make procedures for Min and Max. I did use a lot of them in my program, and there is also a lot of Sgn() functions. For those who did not know Sinclair Basic: SGN returns a 0 if the input is 0, in any other case it returns 1, so Sgn(255) returns 1
procedure sgn(value)
if value=0:ret=0:else:ret=1:endif
procedurereturn ret
endprocedure
Simple but effective and fast...
My PC
Ryzen 9 5950, 64 GB RAM, nVidia RTX A4000, Win 10
Ryzen 7 1700, 32 GB RAM, nVidia RTX A2000, Win 10
Blade
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Aug 06, 2003 2:49 pm
Location: Venice - Italy, Japan when possible.
Contact:

Post by Blade »

LCD wrote:For those who did not know Sinclair Basic: SGN returns a 0 if the input is 0, in any other case it returns 1, so Sgn(255) returns 1
procedure sgn(value)
if value=0:ret=0:else:ret=1:endif
procedurereturn ret
endprocedure
Simple but effective and fast...
Sinclair Basic was my first programming language! :cry:

Actually if the input was <0 , the return value was -1 ... :)
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post by einander »

(Psychophanta:)
And there could be another function Limit(number, left_end, right_end) in which the result is a clipped value, this is, if you put 380 in a range of 0-360, the result is 360.
I'm using often this procedure, but I'd like to find a better one...

Code: Select all

Procedure LIM(A,B,C)  ;returns a value for A between B and C
       If B>C:D=B:B=C:C=D: EndIf   ; With this line,  B could be  > C ; Switch(B,C) would be nice...
       If A<B : ProcedureReturn B : EndIf
       If A>C : ProcedureReturn C : EndIf
  ProcedureReturn a
EndProcedure

Debug Lim(180,20,178)
Debug Lim(10,20,178)
Debug Lim(10,170,20) 
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

lcd, i know, but it's a bit funny to write all those functions twice, for floats as well as ints, while they are rather standard components of most advanced basics...

hey, pure is advanced, isn't it? :roll: :P
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

einander, i'll try to put today in Tricks&Tips a Limit() and Wrap() functions. :)

But i'd like someone made a userlibrary with my funcs that PB will not include in next version; who dares? :P
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

well, perhaps i will :-) somewhat later :oops: the list of x_.... procedures is increasing steadily...

but normally, i don't put everything in a library, but in one file that i cut / paste in my code, old habits don't die easily...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Detecting the type by the compiler isn't always bullet proof, it would include casts, like in C which I don't want to. So it will be probably a set of commands for float and another for integers.
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post by einander »

"Psychophanta"
einander, i'll try to put today in Tricks&Tips a Limit() and Wrap() functions. :)
Thx! :D
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Fred wrote
Detecting the type by the compiler isn't always bullet proof, it would include casts, like in C which I don't want to. So it will be probably a set of commands for float and another for integers.
I know it, but i said:
Yes, Abs() should return an int if the input parameter is int, and float if it is float.
just to know it there was a possible really easy solution. Thanx :wink:
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

oh well, i can live with

abs()
absf()
max(a,b)
maxf(a,b)
int()
intf()

fred, you're the expert, so you should know :-)

i'm just a bit worried when additional datatypes will show up, will that mean we will end up with additional variations of the commands? (shudder)

i mean, the following would be horrible...

abs() for ints
absf() for floats
absd() for double ints
absb() for bytes
absdf() for doublefloats

(shudder)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

wow, last minute thought:

if new functions would be added, seperately for floats and ints, then the abs function stands out a little...

may i suggest to change the current abs() function to absf() ? some other keywords might need a change or alias as well... although this might affect older code, rewriting of that coude would simply be a find / replace with the new keyword

int: max() min() abs() sgn()
float: maxf() minf() absf() sgnf() intf()

int() and intf() could be aliases

just a thought, but it is something that will come up further down the line :-)

(oh, on a note, i'm beginning to get more impressed with purebasic lately, some things are veeeeeeeeeery nice, it's just some very minor things that sometimes annoy me (some very basic :-) keywords are just plain weird) but still it's probably the language i am gonna stick with for the moment...)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
LCD
Enthusiast
Enthusiast
Posts: 206
Joined: Sun Jun 01, 2003 10:55 pm
Location: Austria, Vienna
Contact:

Post by LCD »

Blade wrote:
LCD wrote:For those who did not know Sinclair Basic: SGN returns a 0 if the input is 0, in any other case it returns 1, so Sgn(255) returns 1
procedure sgn(value)
if value=0:ret=0:else:ret=1:endif
procedurereturn ret
endprocedure
Simple but effective and fast...
Sinclair Basic was my first programming language! :cry:

Actually if the input was <0 , the return value was -1 ... :)
Ooops, you're right, I never used negative values... Thanks for the reminder. :-)
My PC
Ryzen 9 5950, 64 GB RAM, nVidia RTX A4000, Win 10
Ryzen 7 1700, 32 GB RAM, nVidia RTX A2000, Win 10
Post Reply