在我的 JNI 中,我从 OpenCL 类 (java) 调用一个函数。我传递了一个 float 作为参数,但是当我打印它时,它添加了 2 个数字。
JNI发送代码:
float ndrangeDuration =
(end.tv_sec + end.tv_usec * 1e-6) - (start.tv_sec + start.tv_usec * 1e-6);
LOGD("NDRangeKernel time: %f", ndrangeDuration);
jclass MyJavaClass = (*env).FindClass("com/denayer/ovsr/OpenCL");
if (!MyJavaClass){
LOGD("Aj :(");
return;} /* method not found */
jmethodID setTimeFromJNI = (*env).GetMethodID(MyJavaClass, "setTimeFromJNI", "(F)V");
(*env).CallVoidMethod(thisObject, setTimeFromJNI, ndrangeDuration);
LOGD 是:
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
java代码:
public void setTimeFromJNI(float time)
{
Log.i("setTimeFromJNI","Time set on " + String.valueOf(time));
}
输出:
NDRangeKernel 时间:0.104581 这是来自 JNI
Time set on 0.10458103 This is from the Java
谁能告诉我我做错了什么或没看到什么?
提前致谢!
最佳答案
值是一样的,只是打印方式不同而已。
来自 printf(3)
手册页:
f, F The double argument is rounded and converted to decimal notation
in the style [-]ddd.ddd, where the number of digits after the
decimal-point character is equal to the precision specification.
If the precision is missing, it is taken as 6; if the precision
is explicitly zero, no decimal-point character appears. If a
decimal point appears, at least one digit appears before it.
您会注意到,从 JNI 打印时,该值的小数点后有 6 位数字。如果您使用“%.8f”而不是“%f”,您会在两边看到相同的数字串。
Android中可以看到Java语言的浮点格式代码here .请注意,输出长度没有固定上限。
如果您的目标是获得匹配的输出,您可以使用 String.format()从用 Java 编程语言编写的代码中打印值。它接受的内容的完整描述在 Formatter 中。 ;如果你深入挖掘它,你会发现:
The number of digits in the result for the fractional part of m or a is equal to the precision. If the precision is not specified then the default value is 6.
这与 native printf()
行为相匹配。
关于java - 从 JNI 发送 float 到 Java 代码添加 2 个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22436574/