Raised This Month: $51 Target: $400
 12% 

Function with Low/High Constraints...


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Brad
AMX Mod X Team Member
Join Date: Jun 2004
Old 09-07-2005 , 22:05   Function with Low/High Constraints...
Reply With Quote #1

I'm not sure if I recall this from Pawn or from another language I've programmed in.

There was a function that you passed at least 3 parameters, probably was 4. Essentially you gave it a low number, a high number, a seed, and then a +/- value to add to the seed. It would +/- and then rollover if it hit the low or high number.

Ex:

Months of a year. 1 2 3 4 5 6 7 8 9 10 11 12

One would pass 1 as the low value, 12 as the high, 9 as the seed, and say +5 as the other value. The result would be 2.

Is this in Pawn or am I confused?
Brad is offline
XxAvalanchexX
Veteran Member
Join Date: Oct 2004
Location: abort73.com
Old 09-07-2005 , 23:06  
Reply With Quote #2

Not sure of any function like that, but I'm sure you could easily make one if you weren't me.
__________________
No longer around. Thanks your support, everyone! As always:
THIS ONES FOR YOU
3000 PTS
XxAvalanchexX is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 09-08-2005 , 08:28  
Reply With Quote #3

I've never seen that but it would be something like this:

Code:
function(low, high, seed, tehadd) {    seed += tehadd;    while (seed > high)       seed = low + (seed - high) - 1;    while (seed < low)       seed = high - (low - seed) + 1;    return seed; }
__________________
hello, i am pm
PM is offline
Brad
AMX Mod X Team Member
Join Date: Jun 2004
Old 09-08-2005 , 10:32  
Reply With Quote #4

Close, but without any leetness . The version I was thinking of would be implemented something like:
Code:
function(low, high, seed, offset) {    new result = seed + offset;    if (result > high)       result = low + (result - high) - 1;    else if (result < low)       result = high - (low - result) + 1;    return result; }

It probably wasn't in Pawn if you haven't seen it. Not a big deal to roll my own but I could swear I've seen it somewhere. Now it's stuck in my head to find out where.

EDIT: Renamed "variance" to "offset" in the code sample.
Brad is offline
Brad
AMX Mod X Team Member
Join Date: Jun 2004
Old 09-08-2005 , 10:40  
Reply With Quote #5

The only reason I even thought of this function is because so the example I had given earlier. A small thing I'm implementing in one of my plugins needs to get the month which is an offset from the current month.

"offset". <-- That's the word I couldn't think of when I was typing out the code in my post above. It should be "offset", not "variance". I'll go edit the code now...

Is this something that conceivably might be useful in other applications other than my month example? In other words, would it make sense for this to become part of AMXX? If not, I'll just pack it away in my slowly building stock library.
Brad is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 09-08-2005 , 10:56  
Reply With Quote #6

You are right, this version might work (untested), handles even offsets higher than the high value:
Code:
function(low, high, seed, offset) {    new numElements = high - low + 1;    offset += seed - low;        if (offset >= 0)       return low + (offset % numElements);    else       return high - (abs(offset) % numElements) + 1;    return 0;    // Makes the compiler happy -_- }

Hmm, maybe I should include it somewhere, what would the name for the function be?

EDIT: Bleh, my brain was blocked
__________________
hello, i am pm
PM is offline
BAILOPAN
Join Date: Jan 2004
Old 09-08-2005 , 11:05  
Reply With Quote #7

gaben
__________________
egg
BAILOPAN is offline
Brad
AMX Mod X Team Member
Join Date: Jun 2004
Old 09-08-2005 , 11:13  
Reply With Quote #8

You also have to account for an offset significantly lower than the low constraint.

The name of the function should be something like BradsGreatNewContstraintFunction(). Er, I mean I dunno. Let me do a search and see if I can find where I saw this already implemented to see what others have named it.
Brad is offline
Brad
AMX Mod X Team Member
Join Date: Jun 2004
Old 09-08-2005 , 11:20  
Reply With Quote #9

Granted it was a quick search but it was fruitless.

Perhaps any of the following:
  • taiya()
  • brad()
  • todd()
  • ed()
  • get_contraint_offset()
  • get_offset()
  • get_offset_num()
  • get_absofsteel()
  • offset_num()
  • num_from_offset()
Brad is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 09-08-2005 , 11:25  
Reply With Quote #10

I've updated the code because it was broken.

I've added it to amxmisc.inc in the cvs; I thought BAILOPAN's idea was most creative so I've chosen that name.

EDIT: It's called constraint_offset now
__________________
hello, i am pm
PM is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 05:11.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode