need simple algorithm

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

hey guys..I'm looking for a simple algorithm to do the following: on in : ofs can be any int, block can be any int on out : ofs must be in the range (0..31) if ofs is -32..-1 block will be decremented by one and ofs will have 32 added to it (to get it in the range of 0..31) if ofs is -64..-33 block will be decremented by 2 and ofs will have 64 added to it..etc.. if ofs is >= 32 block will have ofs / 32 added to it and ofs will be ofs % 32.. I'm looking for a simple couple line solution.. I have it working now but it does alot of operations and I'd like to condense it if at all possible..thanks in advance -- Bradd [Edited by - bradbobak on September 12, 2005 12:47:13 PM]

Share on other sites
possibly something like this:
int delta = ofs / 32;ofs += delta * 32;block += delta;

I'm pretty sure this is what you are asking for. Let me know if it isn't what you were looking for.

Share on other sites
nah, i need to be able to process negative numbers too..
for e.g.

ofs block new_ofs new_block
-1 1 31 0
-35 7 29 5

all offsets between (-1..-32) add -1 to block and add 32 to itself
between (-33..-64) add -2 to block and 64 to itself
all offsets > 0 add ofs/32 to block and set itself to ofs%32
in effect, ofs will end up being between 0..31 at finish time

Share on other sites
Dwiel wasn't far off:
int delta = ofs / 32;if (ofs < 0){	--delta;}ofs -= delta * 32;block += delta;

You really should use better variable names though. Even I knew what context it was used in the name ofs almost certainly tells me nothing about the value it represents.

Enigma

Share on other sites
Your description is somewhat inconsistent (-32 + 64 = +32 is not in 0..31).

Ignoring that, the algorithm you describe implemented in Haskell:
f::(Integer,Integer)->(Integer,Integer)f (ofs,block)=(mod ofs 32,block+div ofs 32)

Note: The definition of div and mod are very important here. Other languages might only offer the equivalents of the similar functions quot and rem. Here is a good summary of the differences that also explains how to implement them.

Share on other sites
wonderful..thank you.. yeah I know I should have used better names..
the names I used were the same as the ones in my code.. map [block] pixel [ofs]

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 19
• 12
• 14
• 9
• Forum Statistics

• Total Topics
633363
• Total Posts
3011509
• Who's Online (See full list)

There are no registered users currently online

×