如何在 Android 应用程序中禁用科学浮点表示法

发布于 2025-01-01 13:11:13 字数 569 浏览 3 评论 0原文

我正在执行基本计算器示例。

在除以两个数字时,我得到科学浮动符号的结果。

当我除 3/2 时,我得到结果 1.4E24,但我需要正常值 1.5。 我正在使用NDK。 函数使用如下

 float res2 = nativeLib.divide(v1, v2);
 s= ""+res2;
 System.out.println(res2);
 result.setText(s); >

函数调用位于 NativeLib 类文件中
公共本机浮点除法(int x,int y);

Function 的实现是 C 语言

JNIEXPORT jfloat JNICALL Java_com_example_Ndkdemo_NativeLib_divide
(JNIEnv * env, jobject obj, jint value1, jint value2)
{    float d= (value1 / value2) ;
return (d);} 

I am executing the basic calculator example.

While dividing two numbers I am getting the result in scientific floating notation.

When I divide 3/2 I am getting result 1.4E24, but I need normal value as 1.5.
I am using NDK.
The Function use is below

 float res2 = nativeLib.divide(v1, v2);
 s= ""+res2;
 System.out.println(res2);
 result.setText(s); >

The Function call is in NativeLib Class File
public native Float divide(int x, int y);

And the Function implementation is is C lang

JNIEXPORT jfloat JNICALL Java_com_example_Ndkdemo_NativeLib_divide
(JNIEnv * env, jobject obj, jint value1, jint value2)
{    float d= (value1 / value2) ;
return (d);} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿童谣 2025-01-08 13:11:13

您可能希望将 value1/value2 转换为浮点数,而不仅仅是将其分配给浮点数。

You may want to cast the value1/value2 to a float, not just assign it to a float.

夜声 2025-01-08 13:11:13

您的 C 代码有轻微损坏。您正在使用两个 int 并执行 float = int / int。这里的问题是转换规则是这样的:C 首先将一个 int 除以另一个,然后转换为 float。因此传递 3, 2 将导致 3/2 == 1,而不是 1.5。

你想要的东西更像是

float d = (float) value1 / value2;

你要回来的Re 1.4e24。这就是返回值被破坏的情况。

这是因为您将除法函数声明为 Float 而不是 float。这些不是同一件事。 Float 是一个装箱值,并且绝对与 C 端的 jfloat 类型不同。

如果您将本机声明更改为

public native float divide(int x, int y);

它,那么一切都应该开始正常工作。 (注意“float”上缺少大写字母!)

Your C code is mildly broken. You're taking two ints and doing float = int / int. The problem here is that the casting rules are such that C will first divide one int by the other and then cast to float. So passing 3, 2 will result in 3/2 == 1, not 1.5.

You want something more like

float d = (float) value1 / value2;

Re the 1.4e24 you're getting back. That's the return value getting corrupted.

This is because you're declaring the divide function as Float instead of float. These are NOT the same thing. The Float is a boxed value, and is most definitely not the same type as jfloat on the C side.

If you change the native declaration to

public native float divide(int x, int y);

it should all start working correctly. (note the lack of capital letter on 'float'!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文