Sign in to follow this  

integers with floating point behaviour

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have two integer variables: x and size. They have to stay integer. I want the relative position of x to size to be always the same For example imagine x=50 and size=100. If I now make size=200, x should become 100. Imagind again x=50 and size=100. In a loop I always increment size with 1, and apply the following formula to x: x = (x * newsize) / size, where newsize is the size incremented with 1. If these all would have been floating points, this would have worked perfectly as I intended. However, because they're integers, x will actually forever remain 50, because the new value is too small for integers to detect. Too keep my code simple, I was wondering if there was a solution to this problem without having to use floats or separate tracking variables. I thought up something: maybe, if x remains the same (indicating that the increment of x is too small for integers), I can sometimes add 1 to it and sometimes not. To detect when to add 1 to it, well that depends on the current value of size and/or newsize. However I'm not sure if this will be really reliable (ie have the same effect as when it would be floats) and how exactly to implement it. So, is the problem I described a known problem that can be solved in a way similar to the one I mentioned, or do you totally don't know what I'm talking about or is it a new problem never encountered by anyone before?

Share this post


Link to post
Share on other sites
this seems almost identical to the line-drawing problem solved by Bresenham's algorithm. Maybe you could apply the same technique?

Share this post


Link to post
Share on other sites
Quote:
Original post by lonesock
this seems almost identical to the line-drawing problem solved by Bresenham's algorithm. Maybe you could apply the same technique?


Hmm maybe, unless it would require me to keep extra variables that need to be remembered for the next size change. I'll study it.

(I don't want to remember extra variables to keep my code as simple as possible, because I have a whole list of these things that I'd have to keep then and stuff, it's doable but I want to see if it can be done without first.)

Share this post


Link to post
Share on other sites
You could calculate one of the variables and store the other. That way they can't get out of sync.

Share this post


Link to post
Share on other sites
it would seem fixed point math is what you need.

also, it would seem easier to just store the ratio between the two numbers if you want to keep it really constant: otherwise numerical error will get a hold of it no matter what.


struct fixedpoint{
union{
int32 data;
struct{
int16 high, low;
}
}
}

fixedpoint x.high = 50, size.high = 100;
int32 ratio = ((size.data << 32) / x.data) >> 32;

size.high += 1;
x = (size.data * ratio) >> 32;
printf(x.high);


or something along those lines. probably full of bugs but it illustrates the idea.

Share this post


Link to post
Share on other sites
Sign in to follow this