32 bit range

Started by
4 comments, last by MaulingMonkey 18 years, 10 months ago
Hi I have a 32 bit fixed point datatype. I was thinking if I want to create a huge world using fixed point 32 bit variables, I would soon run out of range. I thought of a possibility to make the 32 bit datatype to act as a 64 bit datatype and write a routine to emulate it. As I'm using only fixed point I do not have the luxury to use the infinite floating range. My minimal value is 1. Can I get suggessions on how to tacke this issue ? Has someone faced such a problem?
Advertisement
It depends on what programming language you are using...

For example, without having to emulate anything, the long integer type can give you 64 bits in most languages...

If you meant floating numbers, then you can use the double data type...

Anyway, if you really need to emulate, then I have this simple idea:

You can use two variables: one containing the higher 32 bits (call it v_high) and the other containing the lower 32 bits (call it v_low)..

As long as the number is less than 2 ^ 32, the v_low is equal to the number and v_high = 0.

If the value gets larger such as 2 ^ 32, then v_low = 0 and v_high = 1..

if value gets 2 ^ 33 - 1, the v_low = 2 ^ 32 - 1, v_high = 1..
if value gets 2 ^ 33, then v_low = 0, v_high = 2...

and so on...

I'm not sure if this answers your question.. If it doesn't, please explain more..
Quote:Original post by Ali_B
It depends on what programming language you are using...

For example, without having to emulate anything, the long integer type can give you 64 bits in most languages...


Ehm, where the heck did you hear that? For C/C++, long long is fairly commonly 64 bit (with emulation done by your compiler if on a 32 bit platform), but usually long (again, with C/C++) is simply 32 bits - even on 64 bit targets, long is quite often 32 bits (such as with Microsoft Compilers).

Also, "most languages" don't have the long type. C/C++/C#/D/Java/OtherCRipoff are just about it. And VB if you count Long (different capitalization). See Open Directory - Computers: Programming Languages for what's still definately a not completely comprehensive list.

Quote:If you meant floating numbers, then you can use the double data type...

Anyway, if you really need to emulate, then I have this simple idea:


That works allright for simple adds/subtracts:
class unsigned64 {    unsigned int low, high;public:    unsigned64 & operator+=( const unsigned64 & addee ) {        if ( low + addee.low < low ) ++high;        low += addee.low;        high += addee.high;        return *this;    }    unsigned64 & operator-=( const unsigned64 & subtractee ) {        if ( low - subtractee.low > low ) --high;        low -= subtractee.low;        high -= subtractee.high;        return *this;    }    ...};


Multiplication gets more difficult, since the overflow can be more than one.. e.g. whereas (in hex) FFFF FFFF + FFFF FFFF = 1 FFFF FFFE, which is a mere overflow of 1 in the high bits, FFFF FFFF x FFFF FFFF = FFFF FFFE 0000 0001, a nearly (2^32) overflow in the high bits. To deal with this, you deal with 16 bit chunks, since FFFF x FFFF = FFFE 0001, which means the maximum result of multiplying two 16 bit ints still fits within 32 bits. Remember long multiplication? You do that, in effect.

Division is worse still. It's long division, and not in base 10.

Oh dear me, it's getting light out. I'd better go to sleep.

[Edited by - MaulingMonkey on June 17, 2005 10:53:29 PM]
The API that I'm using supports only fixed point data. So i do not have a choice for using floating point datatypes. So using long and double is out of the question. I've to emulate 64 bit datatype to get my range.
Quote:Original post by MaulingMonkey
Ehm, where the heck did you hear that? long long is fairly commonly 64 bit (with emulation done by your compiler if on a 32 bit platform), but usually long is simply 32 bits, if you're compiling for a 32 bit computer. Even on 64 bit, long is quite often 32 bits (such as with Microsoft Compilers).


long is 64 bits in Java and C#. Always.
Yratelev
Quote:Original post by Yratelev
long is 64 bits in Java and C#. Always.


I'll take your word for it :-). Post updated to clarify my intent...

Quote:Original post by sriniatig
The API that I'm using supports only fixed point data. So i do not have a choice for using floating point datatypes. So using long and double is out of the question. I've to emulate 64 bit datatype to get my range.


Can you expand on how your API accepts the data? Does it come with a special fixed-point type? You won't be able to directly use 64-bit types with that API if it's input fixed-point type is 32-bit...

Note that the solution here is usually truncation/discardment. For example, the OpenGL API would surely explode if we tried to directly pass random 512-bit coordinates within 1 of each other by directly casting to float, due to rounding error at extreme distances. The solution there is to adjust those coordinates to "local" coordinates... e.g. place the player at local 0,0,0 - which means to find the local coordinate of random a nearby object , you'd calculate it to be: object.x-player.x,object.y-player.y,object.z-player.z

Then, if the object is outside of, say, a 32-bit range, we can ignore it since it's far enough away we needn't bother rendering it.

This topic is closed to new replies.

Advertisement