Tag: 浮点

为什么使用整数变量抛出exception?

我遇到了以下两个代码。 为什么它不会为浮点引发exception,而在其他情况下它会引发运行时exception。 class FloatingPoint { public static void main(String [] args) { float a=1000f; float b=a/0; System.out.println(“b=” +b); } } 输出:b =无穷大 。 如果我尝试使用int值,那么它将抛出运行时exception。 为什么会这样?

性能:浮动到int转换和剪切结果到范围

我正在使用float进行一些音频处理。 结果需要转换回PCM样本,我注意到从float到int的转换是非常昂贵的。 更令人沮丧的是,我需要将结果剪辑到一个短的范围(-32768到32767)。 虽然我通常会明确地假设这可以通过简单地将float转换为short来确保,但这在Java中失败,因为在字节码级别它导致F2I后跟I2S。 而不是一个简单的: int sample = (short) flotVal; 我需要诉诸这个丑陋的序列: int sample = (int) floatVal; if (sample > 32767) { sample = 32767; } else if (sample < -32768) { sample = -32768; } 有更快的方法吗? (约占总运行时间的6%似乎用于铸造,而6%似乎乍看起来并不那么多,当我认为处理部分涉及大量矩阵乘法和IDCT时,它令人震惊) 编辑上面的强制转换/剪辑代码(不出意料地)在循环体中,它从float []中读取浮点值并将它们放入byte []中。 我有一个测试套件,可以测量几个测试用例的总运行时间(处理大约200MB的原始音频数据)。 当通过将循环索引分配给样本来替换强制转换“int sample =(int)floatVal”时,6%是从运行时差异中得出的。 编辑@leopoldkot:我知道Java中的截断,如原始问题(F2I,I2S字节码序列)中所述。 我只是简单地尝试了强制转换,因为我认为Java有一个F2S字节码,但遗憾的是它没有(最初来自68K汇编背景,其中一个简单的“fmove.w FP0,D0”就完全按照我想要的方式完成) 。

任何符合IEEE754(r)标准的Java实现?

是否有任何完全符合IEEE的IEEE754r实现可用于Java,它们支持Java选择省略的所有function(或者更确切地说是高级语言,如省略): 陷阱 粘滞的旗帜 定向舍入模式 延长/长双 四精度 DPD(密集小数) 在任何人弄错之前澄清:我不是在寻找JVM来为上面提供任何支持,只是在软件中实现类型和操作的一些类,基本上是已经存在的原始包装类的样式Float /双。

Java中Float的最大值?

以下问题表明Double的最小值为-Double.MAX_VALUE 。 Float也是如此(即-Float.MAX_VALUE )?

Java:双机器epsilon不是最小的x,这样1 + x!= 1?

我试图在Java中确定double 机器epsilon ,使用它的定义是最小的可表示的double值x ,使得1.0 + x != 1.0 ,就像在C / C ++中一样。 根据维基百科,这台机器epsilon等于2^-52 (52是double尾数位的数量 – 1)。 我的实现使用Math.ulp()函数: double eps = Math.ulp(1.0); System.out.println(“eps = ” + eps); System.out.println(“eps == 2^-52? ” + (eps == Math.pow(2, -52))); 结果是我的预期: eps = 2.220446049250313E-16 eps == 2^-52? true 到现在为止还挺好。 但是,如果我检查给定的eps确实是最小的 x ,使得1.0 + x != 1.0 ,那么似乎有一个较小的,即根据Math.nextAfter()的前一个 double值: double epsPred […]

为具有浮点成员的类实现“容忍”`equals`和`hashCode`

我有一个带float字段的类。 例如: public class MultipleFields { final int count; final float floatValue; public MultipleFields(int count, float floatValue) { this.count = count; this.floatValue = floatValue; } } 我需要能够按值比较实例。 现在我如何正确实现equals & hashCode ? 实现equals和hashCode的常用方法是仅考虑所有字段。 例如,Eclipse将生成以下equals : public boolean equals(Object obj) { // irrelevant type checks removed …. MultipleFields other = (MultipleFields) obj; if (count != other.count) return […]

Java是不是它的工作原理?

我正在查看openjdk-1.7.0_25源代码,我看到了这个方法: /** * Returns {@code true} if the specified number is a * Not-a-Number (NaN) value, {@code false} otherwise. * * @param v the value to be tested. * @return {@code true} if the argument is NaN; * {@code false} otherwise. */ static public boolean isNaN(float v) { return (v != v); } 当这个方法可以返回true时,我无法理解它是如何工作的?

如何强制小数点后的两位数

我有以下TextView,它始终显示$ 0.00,除非在单击按钮并保存到此TextView后进行任何计算。 当我运行应用程序时,它显示$ 0.0(这将起作用,除了我正在使用美元和美分,我希望它显示小数后的两位数,除非两个数字都是00) 我正在使用SharedPreferences ,这是我在应用程序中的代码: onCreate() float totalTollAmount = 0; tollAmount = (EditText) mFrame2.findViewById(R.id.etToll); //where the amount is entered tollAmount.setFilters(new InputFilter[] {new DecimalFilter(6, 2)}); tvTotalAmountLabel = (TextView) mFrame2.findViewById(R.id.tvTotalAmount); //holding total amount if (Float.toString(prefs.getFloat(“dblTollAmount”, 0)) != “0.0”) { tollAmount.setText(Float.toString(prefs.getFloat(“dblTollAmount”, 0))); } tvTotalAmountLabel.setText(“$” + Float.toString(prefs.getFloat(“totalTollAmount”, 0))); //retrieves the total toll amount and displays btnCalc.setOnClickListener(new View.OnClickListener() { […]

Java中的浮点精度和相等性

知道浮点数,即使十进制格式小数点后的固定位数,也无法准确表示。 所以我有以下程序来测试: public class Main { public static void main(String[] args) { System.out.printf(“0.1 in single precision is %.50f\n”, 0.1f); System.out.printf(“0.2 in single precision is %.50f\n”, 0.2f); System.out.printf(“0.3 in single precision is %.50f\n”, 0.3f); System.out.printf(“0.1 + 0.2 in single precision is %.50f\n”, 0.1f + 0.2f); System.out.printf(“0.1 + 0.2 == 0.3 is %b in single precision\n”, 0.1123f […]

Java VS C#中的intBitsToFloat方法?

在C#中将位转换为float时,我输入的数字错误。 我们使用这个位number= 1065324597 在Java中 ,如果我想从bit转换为float,我将使用intBitsToFloat方法 int intbits= 1065324597; System.out.println(Float.intBitsToFloat(intbits)); 输出: 0.9982942 ,这是我想要在C#中获得的正确输出 但是,在C#中我使用过 int intbits= 1065324597; Console.WriteLine((float)intbits); 输出: 1.065325E+09错! 我的问题是你如何在C#中转换inbitsToFloat? 我的尝试:我查看了这里的文档http://msdn.microsoft.com/en-us/library/aa987800(v=vs.80).aspx但我仍然有同样的麻烦