Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

JOL

unsigned double in C++

This topic is 5317 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

Of course there is a historical and also technical reason for the unsigned int type, (due to how 2complement numbers are stored), but the unsigned keyword would be applicable to other datatypes aswell. sometimes there is need for a unsigned double type, ex. in a function like this...
void setPositiveSpeedChangeInPercentage(unsigned double spspdchngnprcntg); 
 
is there any good ways of doing this, suggestions ? I understand why this isn''t incorporated in the language since it is very strong typed, how would YOU design this ?

Share this post


Link to post
Share on other sites
Advertisement
No. There is no such thing as an unsigned double, and it is unlikely there ever will be, given that floating-point numbers follow the IEEE 754 specifications

float - 1 sign bit, 8 exponent bits, 23 mantissa bits.
double - 1 sign bit, 11 exponent bits, 52 mantissa bits. (IIRC)

You could write your own, but you would get absolutely none of the benefits of the hardware floating point operations - which work on IEEE 754 numbers.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)


[edited by - Fruny on February 28, 2004 5:15:44 PM]

Share this post


Link to post
Share on other sites
Well, you couldn''t really get any hardware accelerated math operations on an unsigned double. There is specifically a single bit in the IEEE floating point formats that designates the sign of the number. Well, you might be able to somehow convert a 32-bit or 64-bit floating point number into the 80-bit floating point format that processors use internally, and interpret the sign bit as an extra bit in the mantissa, but I don''t know enough about processor level programming to know if it is possible to do that.

Another option is to create your own number class, and make it optionally a fixed point or floating point number, and simply not have a sign value. You could have an unsigned long, and just interpret it as having a range from 0.0 to 42,949.67295, instead of 0 to 4,294,967,295. That way, you''d still be able to do hardware accelerated math for almost everything. If you made your own floating point class, though, it''d probably be much harder to make it perform as quickly as a standard floating point format. But functionally, it would do the job.

Share this post


Link to post
Share on other sites
One thing you could try is to write a class wrapper around the double primitive and put in asserts all over the place to make sure the sign bit never gets set. Or zero out negative results, etc.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
well, here''s a simple class wrapper, sort of does the job but...

class uDouble{
public:
double dobb;
uDouble(double d){dobb=d;}
operator double() const{
assert(dobb >= 0);
return dobb;
}
};

void setTempo(uDouble dobb){
std::cout << dobb;
}

int main(int argc, char *argv[]){
setTempo(3.14159);
system("PAUSE";
return 0;
}


My problem is that I am writing a class dealing with arrays, the arrays are indexed in regard to a double value, offsets calculated at runtime, guaranteed problems.
I want to make sure that this value is positive by making it obvoius and illegal in the resulting API, there isn''t a ''natural'' way of dealing with negative values.

maybe throwing a exception, or just assert ?

Share this post


Link to post
Share on other sites
IF you want to protect the array, you don''t wrap the indexer so that it can never be negative (cause it would crash just as hard with an invalid positive number as a negative number) ... you wrap the ARRAY so that invalid indexes are caught ...

Share this post


Link to post
Share on other sites
yes, that is of course what I am doing alredy but the elegance of using the unsigned int would be great ...

setEulerInterpolationTimeStep(unsigned double);


And also, passing a negative value doesn''t make sense in this case.

The way I am calculating the offset into a array makes no sense if the passed value is negative but it makes sense for ANY psoitive value...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!