Re: [dart-misc] How convert double or num into binary presentation? To int64 or to int32 or into bytes?
668 views
Skip to first unread message
Message has been deleted
Lasse R.H. Nielsen
unread,
Sep 24, 2012, 5:06:33 AM9/24/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mi...@dartlang.org
There is currently no functionality to do that in the libraries.
It is under consideration to add a double.asBits (or some other name).
That won't work in the dart2js compiler because it doesn't support
real integers yet (only doubles, which can't contain a 64-bit value).
If you desperately need it now, you can use the following code to
convert a double to its bits arithmetically:
int doubleToBits(double value) {
const pow52 = 4503599627370496.0; // 2^52
const pow1022 = 4.49423283715579e+307; // 2^1022
if (value.isNaN()) {
return 0x7FF8000000000000;
}
int signbit = 0;
if (value.isNegative()) {
signbit = 0x8000000000000000;
value = -value;
}
if (value.isInfinite()) {
return signbit | 0x7FF0000000000000;
} else if (value < 2.2250738585072014e-308) {
// Denormal or zero.
// Multiply by 2^(1022+52) to get the bits into the correct position.
int bits = (value * pow1022 * pow52).toInt();
return signbit | bits;
} else {
// Slow linear search to move bits into correct position for mantissa.
// Use binary search or something even smarter for speed.
int exponent = 52;
while (value < pow52) {
value *= 2;
exponent -= 1;
}
while (value >= pow52 * 2) {
value /= 2;
exponent += 1;
}
int mantissaBits = (value - pow52).toInt();
int exponentBits = (exponent + 1023);
return signbit | (exponentBits << 52) | mantissaBits;
}
}
(Again: This does NOT work in dart2js, you need 64-bit unsigned
integers for it to work.)
Message has been deleted
Ladislav Thon
unread,
Sep 24, 2012, 8:11:31 AM9/24/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mi...@dartlang.org
There is currently no functionality to do that in the libraries.
Except that there is :-)
I needed this too in my MsgPack implementation and I realized that ByteArray and companions are perfectly suitable for this. Something like (new Float64Array(1)..[0] = doubleValue).asByteArray() (writing this from memory) gives you 8-element list of bytes. Converting it to Int64Array for getting a 64bit integer is left for the reader as an excersise (I didn't need this).
LT
Mads Ager
unread,
Sep 24, 2012, 8:32:04 AM9/24/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mi...@dartlang.org
As Ladislav points out there is a VM-only library that will let you
perform operations like these.