Does anyone know how to make a c equivalent to Java's Float.intBitsToFloat method?
https://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/api/java.lang.Float.html#intBitsToFloat(int)
I can't find any examples of it being ported.
Does anyone know how to make a c equivalent to Java's Float.intBitsToFloat method?
https://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/api/java.lang.Float.html#intBitsToFloat(int)
I can't find any examples of it being ported.
This source has given a way to do this
JNIEXPORT jfloat JNICALL
Java_java_lang_Float_intBitsToFloat(JNIEnv *env, jclass unused, jint v)
{
union {
int i;
float f;
} u;
u.i = (long)v;
return (jfloat)u.f;
}
So working around the above you can do something like this .
union int_to_float_bits {
int32_t integer_bits;
float converted_float_bits;
};
float intBitsToFloat(int32_t int_value)
{
union int_to_float_bits bits;
bits.integer_bits = int_value;
return bits.converted_float_bits;
}
I like the idea Ankur has for this. Here is another way (a bit hackish mind you):
#include <stdio.h>
int main() {
int v = 1090099610;
float *f = (float*)(&v);
printf("%E\n", *f);
return 0;
}
Output: http://cpp.sh/6v2px
In jshell:
jshell> Float.intBitsToFloat(1090099610)
$1 ==> 7.8