# integers with floating point behaviour

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

## 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 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 on other sites
Quote:
 Original post by lonesockthis 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 on other sites
You could calculate one of the variables and store the other. That way they can't get out of sync.

##### Share on other sites
do you have access to a FPU? you could always just keep your ratio a float, and do the math, then use static_cast.

##### 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.

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632997
• Total Posts
3009799
• ### Who's Online (See full list)

There are no registered users currently online

×