1

Does Anyone know the c++ version of java's Double.longBitsToDouble ... i have seen this answer which answers float.intBitsToFloat; C equivalent of Java's Float.intBitsToFloat

i tried to apply the same method to convert Java_java_lang_Double_longBitsToDouble to pure c++ version, but the function has a line i don't know how to convert, the line being jlong_to_jdouble_bits(&v);

you can find the class here https://github.com/openjdk/shenandoah/blob/master/src/java.base/share/native/libjava/Double.c

3rdhope
  • 19
  • 2
  • I tried but i'm not getting the same result as in java... – 3rdhope Feb 09 '20 at 17:24
  • Read a good book about [C++ programming](http://stroustrup.com/programming.html) but be aware that C++ and Java are different language and might not have equivalents. Floating point is tricky, read http://floating-point-gui.de/ – Basile Starynkevitch Jun 04 '20 at 05:59
  • 1
    [searching for the definition](https://github.com/openjdk/shenandoah/search?q=jdouble_to_jlong_bits) indicates that its a macro that does nothing – kmdreko Jun 04 '20 at 06:01

1 Answers1

1

Just copy the content from long variable to double variable, here is the example:

double ori_d_value = 3.14d;
long l_value = 1234;
memcpy(&l_value, &ori_d_value, 8);  // convert double value to row long value
double d_value;
memcpy(&d_value, &l_value, 8);  // convert row long value to double value
printf("long value: %ld, double value: %lf\n", l_value, d_value);
glishijie
  • 49
  • 4
  • I am not sure it is not undefined behavior. Of course, on most systems, it will practically work but is implementation specific (endianess issues, implementations representing floating point outside of IEEE-754, such as IBM mainframes) – Basile Starynkevitch Jun 04 '20 at 06:01
  • You should also check that both types have the same size, 8 bytes (likely, but not guaranteed). Also, if the the intent is to "play" with the bits of the internal representation, an unsigned type should be better. – Bob__ Jun 04 '20 at 08:01