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 / Converting number ranges

Author
Message
Redmotion
22
Years of Service
User Offline
Joined: 16th Jan 2003
Location: Mmm mmm.. Marmite
Posted: 22nd Sep 2007 13:02
I want to convert a number that exists within the range:

0 to 65535

to exist within:

-100 to +100 (so the 32767 would become 0)

Is there a standard maths technique to do this and if so, what's it called?!

Something like:

out = convertrange(in,oldlow,oldhigh,newlow,newhigh)

Cheers in advance.

revenant chaos
DBPro Master
18
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 22nd Sep 2007 13:27 Edited at: 22nd Sep 2007 13:40
take your number, subtract 32767 from it, and multiply it by 0.0000305

example:


press the up and down arrows to change the value.

to figure out the value (0.0000305) is really quite simple. so your range is 0 to 65535 and you want the lower half of it to be a negative. divide the maximum value by 2 (=32767). now you want this number to represent the number 1. divide the number 1 by 32767. using the windows calculator, you will get:
3.0518509475997192297128208258309e-5

the E-5 means you have you have to move the decimal point 5 places to the left, and this leaves you with:
0.000030518509475997192297128208258309

you only need the first 8 digits (excluding the decimal point) of the number, and that is the number that you use to multiply the value-(half of max value) with.
revenant chaos
DBPro Master
18
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 22nd Sep 2007 23:10 Edited at: 22nd Sep 2007 23:10
This wouldnt happen to be for a PC Elements recoil game pad is it?

I just thought of an easier way that requires no math to be done



the same demo from above, but using the new method:


the "converted" variable needs to be a float, and the number that it is divided by (the numerator) must be expressed as a float, otherwise it will return an integer value. -1 at 0, 0 at 32767, and 1 at 65535.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 22nd Sep 2007 23:12
Ideally, you should take the first 8 significant digits rather than 8 decimal places to avoid too much loss of precision. i.e. in this example, I'd use 0.000030518509, rounding the last digit as appropriate according to the next digit.

It won't make too much difference on the values being used here (+-32767 to +-100), but for scaling upwards especially for larger ratios, the difference may be quite noticeable.

Utility plugins collection and
http://www.matrix1.demon.co.uk for older plug-ins and example code
Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Sep 2007 23:17
Of course, neither of those solutions work correctly (and the original question wasn't quite right either - what's half of 65535?).
kbyoyoa
18
Years of Service
User Offline
Joined: 30th Apr 2007
Location: Minnesota
Posted: 22nd Sep 2007 23:39 Edited at: 22nd Sep 2007 23:40
ok I work on this for a few minutes and came up with this function...



this will convert the number "in" in the range "oldlow" to "oldhigh" to a number in the same position in the range "newlow" to "newhigh"

This will work for any range not just the one you stated.

hope this is what you where looking for...
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 23rd Sep 2007 00:12
Quote: "Of course, neither of those solutions work correctly (and the original question wasn't quite right either - what's half of 65535?)."

You're right about the solution being wrong, as it concentrates on start/end points, and misses the fact that we're talking about inclusive ranges. -100 to 100 has 201 integers in it, and 0 to 65535 has 65536 integers in it - (endpoint - startpoint + 1).

Here's working code, that's not quite right yet:


The problem is that the rounding isn't quite right just yet - it needs to round to the nearest integer rather than truncating as it does now

Utility plugins collection and
http://www.matrix1.demon.co.uk for older plug-ins and example code

Login to post a reply

Server time is: 2025-05-28 06:06:44
Your offset time is: 2025-05-28 06:06:44