Page 1 of 1

max() min() abs()

Posted: Wed Oct 29, 2003 2:29 pm
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...

Posted: Wed Oct 29, 2003 6:14 pm
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.

Posted: Wed Oct 29, 2003 10:39 pm
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?

Posted: Wed Oct 29, 2003 11:23 pm
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...

Posted: Wed Oct 29, 2003 11:49 pm
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 ... :)

Posted: Thu Oct 30, 2003 12:31 am
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) 

Posted: Thu Oct 30, 2003 1:08 am
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

Posted: Thu Oct 30, 2003 8:31 am
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

Posted: Thu Oct 30, 2003 9:21 am
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...

Posted: Thu Oct 30, 2003 9:58 am
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.

Posted: Thu Oct 30, 2003 10:03 am
by einander
"Psychophanta"
einander, i'll try to put today in Tricks&Tips a Limit() and Wrap() functions. :)
Thx! :D

Posted: Thu Oct 30, 2003 10:21 am
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:

Posted: Thu Oct 30, 2003 10:49 am
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)

Posted: Thu Oct 30, 2003 11:01 am
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...)

Posted: Mon Nov 10, 2003 6:16 pm
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. :-)