Jump to content
  • Advertisement
Sign in to follow this  
Nitage

memset for doubles?

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

Is there a way to set an array of doubles using similar functionality to memset or is a loop the only way to do it (I'm using C for this project and the array needs to be initialized to a specific number that's known at compiler time and isn't 0).

Share this post


Link to post
Share on other sites
Advertisement
memset goes and sets a byte at a time to the value you send in. So there really is no good way to set any meaningful value for a double - unless you know of a double that has the same byte repeating over and over. It certainly wouldn't work for any arbitrary value.

On the otherhand you could write your own (templated?) custom memset operation. You would just have to take care of memory alignment stuff


[edit]
Oh, just saw this was a C project, so ignore the templated aspect of the custom function

Share this post


Link to post
Share on other sites
If you know how big your doubles are in actual bytes (8 bytes, probably), then yes, you can, you just need to know the IEEE floating point type.

For a 32 bit floating point type it's (something like) this:
1-sign-bit 9-bit-exponent 22-bit-mantissa

Then you can find how to represent your number byte-by-byte, and memset it like that. In other words...

Just use a loop.

Share this post


Link to post
Share on other sites
You know, when it comes down to it, memset() doesn't do anything magical: it too is just a loop? In C++, the std::fill() and std::fill_n() function templates play that role. So don't feel bad about writing your own memset-like function for doubles.

Share this post


Link to post
Share on other sites
I don't know but this page: http://www.daniweb.com/tutorials/tutorial1767.html
states that a double can be >= the size of a float. So this may work if sizeof(int) == sizeof(double) I think. (Not sure though)

Pseudo code:


double myvalue = 0xf00d;//change to whatever value you want
memset(doublearray,reinterpret_cast<int>(myvalue),sizeof(double)*doublearraylen);





where doublearray is your array of doubles,
and doublearraylen is the number of doubles contained in that array ;)

EDIT: Fixed broken source lang="cpp"-tag and a typo

Share this post


Link to post
Share on other sites
The 2nd int parameter to memset converts its argument to unsigned char AFAIK and copies char sized into is destination so the above probably won't work.

As previously stated, memset is only a loop so there isn't much to be gained over writing your own initialisation loop.

HTH. Paul

Share this post


Link to post
Share on other sites
Quote:
I thought IEEE 754 stated that a double is 32 bits and float is 16 bits?


You thought wrong.

If you want to fill doubles (or floats) with the "0" value, you can use memset(), because 0.0f is all zero bytes, as is 0.0.

If you want to fill with another value, use std::fill().

Share this post


Link to post
Share on other sites
A double is 64 bits and a float is 32 bits. A long double is probably 80 bits on most platforms, although your compilers sizeof(long double) may be 12, not 10, due to padding.

Share this post


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

  • 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!