我有以下简单的代码:
int speed1 = (int)(6.2f * 10);
float tmp = 6.2f * 10;
int speed2 = (int)tmp;
Run Code Online (Sandbox Code Playgroud)
speed1和speed2应该具有相同的值,但事实上,我有:
speed1 = 61
speed2 = 62
Run Code Online (Sandbox Code Playgroud)
我知道我应该使用Math.Round而不是cast,但我想了解为什么值不同.
我查看了生成的字节码,但除了存储和加载外,操作码是相同的.
我也在java中尝试了相同的代码,我正确地获得了62和62.
有人可以解释一下吗?
编辑: 在实际代码中,它不是直接6.2f*10而是函数调用*常量.我有以下字节码:
速度1:
IL_01b3: ldloc.s V_8
IL_01b5: callvirt instance float32 myPackage.MyClass::getSpeed()
IL_01ba: ldc.r4 10.
IL_01bf: mul
IL_01c0: conv.i4
IL_01c1: stloc.s V_9
Run Code Online (Sandbox Code Playgroud)
速度2:
IL_01c3: ldloc.s V_8
IL_01c5: callvirt instance float32 myPackage.MyClass::getSpeed()
IL_01ca: ldc.r4 10.
IL_01cf: mul
IL_01d0: stloc.s V_10
IL_01d2: ldloc.s V_10
IL_01d4: conv.i4
IL_01d5: stloc.s V_11
Run Code Online (Sandbox Code Playgroud)
我们可以看到操作数是浮点数,唯一的区别是stloc/ldloc
至于虚拟机,我尝试使用Mono/Win7,Mono/MacOS和.NET/Windows,结果相同
我将项目升级到Gradle插件3.0.1,但在生成数据绑定类期间,我收到以下错误(并且未生成绑定类):
error: cannot generate view binders java.lang.NullPointerException
at android.databinding.tool.store.SetterStore.getMatchingMultiAttributeSetters(SetterStore.java:642)
at android.databinding.tool.store.SetterStore.getMultiAttributeSetterCalls(SetterStore.java:529)
at android.databinding.tool.BindingTarget.resolveMultiSetters(BindingTarget.java:221)
at android.databinding.tool.LayoutBinder.<init>(LayoutBinder.java:249)
at android.databinding.tool.DataBinder.<init>(DataBinder.java:52)
at android.databinding.tool.CompilerChef.ensureDataBinder(CompilerChef.java:90)
at android.databinding.tool.CompilerChef.sealModels(CompilerChef.java:203)
at android.databinding.annotationprocessor.ProcessExpressions.writeResourceBundle(ProcessExpressions.java:193)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:794)
...
Run Code Online (Sandbox Code Playgroud)
经过一些调查后,如果我在布局xml中的给定元素上使用数据绑定最多有1个属性,那么似乎正确生成了绑定类.如果我添加第二个,我得到前一个异常.
在我的项目环境中,以下视图会导致错误,但不幸的是,在一个全新的项目中,它会正常编译.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View"/>
<variable
name="title"
type="String"/>
<variable
name="visible"
type="boolean"/>
</data>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- with either text or visiblility, the project builds, but not with both -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{title}"
android:visibility="@{visible ? View.VISIBLE : View.GONE}"
/>
</RelativeLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
有没有人遇到过这个问题?