简体   繁体   English

要求失败,并带有浮点数

[英]Require fails with floating point numbers

private void waveformProgress(double currentProgressPercentage) {
   if (currentProgressPercentage < 0.0f) currentProgressPercentage = 0.0f;
   if (currentProgressPercentage > 1.0f) currentProgressPercentage = 1.0f;
   waveView.setProgress((float) (100.0 * currentProgressPercentage));
   ..
 }

I have rare crash reports on the setProgress line, in the call to a kotlin setter: 在对kotlin setter的调用中,我在setProgress行上有罕见的崩溃报告:

var progress: Float = 0F
        set(value) {
            require(value in 0..100) { "Progress must be in 0..100" }
...
        }

I know about floating point equality and all, but how is it possible that the require fails here? 我知道浮点数等的所有知识,但是require怎么可能在这里失败? How would I correct it? 我该如何纠正? I suppose it's possible Crashlytics is reporting new version number when the crash happened in old version that didn't ensure the number was between 1..100. 我想当崩溃发生在旧版本中时,Crashlytics可能会报告新版本号,而该版本不能确保版本号在1..100之间。

There is a currentProgressPercentage for which you should get this exception: NaN (it isn't actually a single value, but this doesn't matter here). 有一个currentProgressPercentage ,您应为其获取以下异常: NaN (实际上不是单个值,但这在这里无关紧要)。 Both comparisons will be false, and (float) (100.0 * currentProgressPercentage) will return a NaN which isn't in range 0..100 . 两次比较都将为false,并且(float) (100.0 * currentProgressPercentage)将返回不在0..100范围内的NaN A NaN can be produced, eg, by calculating 0.0/0.0 . 可以例如通过计算0.0/0.0来生成NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM