开发者_C百科How do I perform a twos complement on a double and return a double?
If you are trying to do the two's complement of the internal bit representation of the double, you can use the BitConverter
class.
Something like:
double x = 12345.6;
Int64 bits = BitConverter.DoubleToInt64Bits(x);
bits = ~bits + 1;
x = BitConverter.Int64BitsToDouble(bits);
I'm not sure why you would want to do this, though...
You might need to cast to a long and then do the twos complement and cast back:
double x = 1245.1;
long l = (long)x;
l=~l; l++; /* complement followed by + 1 */
x = (double)l;
I didn't test this, but hopefully it gets you on the right track.
Edit: Since you cannot cast from double to long with bit representation then you might need to do something like:
double x = 1234.5;
ulong l;
unsigned char * d = (unsigned char *) &x;
l = (ulong)(*d);
l=~l; l++;
d = (unsigned char *) &l;
x = (double)(*d);
Again untested...
精彩评论