Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / How can I wrap a number within a range?

Author
Message
Starshyne Emir
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location: Porto Alegre, southern Brazil
Posted: 24th Jun 2020 05:48
Hi!
Since I started using Matrix1Utils I noticed that one of the only functions in there that I used was WRAP(n,min,max).
The problem is: when I make programs using M1U they require dlls that people failt entirely in obtaining, so my programs doesn't work for almost no one.
I tried to rewrite the function in DBPro in such way I could reinstal DBPro pure with no plugins (or at least only with plugins that doesn't require aditional DLs) but even the one in C that I converted doesn't work.
Most of the atempts result in strange behavior.
For example:
Some versions of the code ignore the 0. If I wrap -3 betwen -3 and 3 it gives me -3,-2,-1,1,2,3.
Can anybody help me with a WRAP function that work?
God is real - except if declared as integer.
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 24th Jun 2020 14:14 Edited at: 28th Jun 2020 15:54
Try this function, I have tried hard to break it. Let me know if you discover any flaws.

Starshyne Emir
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location: Porto Alegre, southern Brazil
Posted: 24th Jun 2020 16:04
It works, but only if the number is smaller than the minimum.
Now I must figure out how to do when the number is greater than the maximum.
But don't worry, I'll do this myself as an exercise. I need to learn how to write this. You gave me a good starting point, leave the rest to me.
Thanks.
God is real - except if declared as integer.
Starshyne Emir
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location: Porto Alegre, southern Brazil
Posted: 24th Jun 2020 16:21
Well, I tried and I can't figure out how to apply the wrapping when xVal > xMax.
And no matter which value I assign to xVal, it always start as 0.

Exam7ple of output:

a = 7
do
a = wrap(a,-3,3)
print a
wait key
inc a,1
loop

function wrap():endfunction - your function

The output is:
0
1
2
3
-3
-2
-1
0
1
2
3
And so on...

Why? Dunno. I'll leave this alone by now.
God is real - except if declared as integer.
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 24th Jun 2020 16:25
Seem to work for me. Could you please give example where it fails.
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 24th Jun 2020 16:32 Edited at: 24th Jun 2020 16:32
Your results are what they should be. The result after 3 wraps to -3. Am I missing something here.
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 24th Jun 2020 18:44 Edited at: 24th Jun 2020 18:46
Ok, I just removed the wrap function from my DBPro source to test the Matrix1Utils wrap function. The Matrix1Utils only works with positive integers. Passing the function with a = Wrap(a,-3,3) give a result from (0) - (5), not (-3) - (3) as it should.
Starshyne Emir
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location: Porto Alegre, southern Brazil
Posted: 24th Jun 2020 20:52
And I was here all this time trying to wrap both positive and negative.
With positives it is easier.
But... I used your wrap from M1U with negatives and it worked - or at least it pretended to work very well. % easier.
God is real - except if declared as integer.
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 24th Jun 2020 21:05
Whatever.
Starshyne Emir
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location: Porto Alegre, southern Brazil
Posted: 24th Jun 2020 21:32
I give up. I am not smart enough to replace your wonderful and deliciously useful little piece of code. I can't.
I tried and this is the result: a warm and smoking piece of excrement.

All I gonna do is keep on programming with M1U and attaching a ZIP with the DLLs of it and a link to download DX9 - and if they can't make my stuff run, it is none of my business.

God is real - except if declared as integer.

Login to post a reply

Server time is: 2024-04-19 22:30:06
Your offset time is: 2024-04-19 22:30:06