3

私はすでにこのトピックに関するさまざまな質問を検索していましたが、明確なアイデアは得られませんでした。このコードを確認してください:

class Test{
    public static void main(String[] s){
        int a=5;
        float b=(float)a/0;
        System.out.print(b);
    }
}

出力はInfinityです。しかし、私が得ていないのは、例外をスローする必要aがあるということです。では、どのように出力を表示できますか?inta/0Infinity

4

8 に答える 8

8

その理由は

(float)a/0;

として解釈されます

((float)a)/0;

ではなく

(float)(a/0);

したがって、実際には、整数除算を実行してから結果を変換するのではなく、除算を実行aする前にに変換しています。float

お役に立てれば!

于 2011-09-08T01:47:09.813 に答える
4

整数をゼロで割っていません。float式は次と同等であるため、aをゼロで除算しています。

float b=((float)a)/0;

次の例のように、代わりに整数のみで除算を強制すると、期待値ArithmeticExceptionがスローされます。

float b=(float)(a/0);
于 2011-09-08T01:47:19.603 に答える
2

すべての浮動小数点計算は、IEEE754仕様に準拠しています。特に、オーバーフローとエラーを示す3つの特別な浮動小数点値があります。

•正の無限大•負の無限大•NaN(数値ではない)

たとえば、正の数を0で割った結果は、正の無限大になります。0/0または負の数の平方根を計算すると、NaNが得られます。

も参照してください

注意:浮動小数点数は、丸め誤差が許容できない財務計算には適していません。たとえば、コマンドSystem.out.println(2.0-1.1)は、予想どおり0.9ではなく、0.8999999999999999を出力します。このような丸め誤差は、浮動小数点数が2進数システムで表されるという事実によって引き起こされます。10進法で分数1/3の正確な表現がないのと同じように、分数1/10の正確な2進表現はありません。丸め誤差のない正確な数値計算が必要な場合は、この章の後半で紹介するBigDecimalクラスを使用してください。

コアJavaボリューム1の第3章から

于 2011-09-08T01:50:44.773 に答える
1

aはintですが、除算が発生したときにfloatにキャストする場合を除きます。キャストは除算よりも優先順位が高いことに注意してください。わかりやすくするために角かっこを使用すると、次のようになります。

float b = ((float) a)/0;

したがって、aがintである間、浮動小数点除算を実行しています。

于 2011-09-08T01:48:42.767 に答える
1

これは、Javaではintsを使用したゼロによる除算が許可されておらず、浮動小数点値を使用できるためです。

于 2011-09-08T01:48:44.067 に答える
1

Infinity浮動小数点演算によって、通常は表現できないほど大きな浮動小数点数が作成された場合に生成されます。

のフロートへのキャストは、フロートへaの自動キャストも生成します0

于 2011-09-08T01:49:58.300 に答える
1

フロートにキャストaしているので、ゼロで除算します。フロートには+/-無限大があります。

http://www.velocityreviews.com/forums/t137207-division-by-zero-float-vs-int.html

于 2011-09-08T01:52:10.767 に答える
0

二項 / 演算子は除算を実行し、そのオペランドの商を生成します。左側のオペランドは被除数で、右側のオペランドは除数です。

整数除算は 0 に向かって丸めます。つまり、2 進数値昇格 (§5.6.2) 後の整数であるオペランド n と d に対して生成される商は、|d·q|| を満たしながら、大きさが可能な限り大きい整数値 q です。 n|; さらに、|n||d| の場合、q は正です。n と d は同じ符号ですが、|n||d| の場合、q は負です。n と d の符号は反対です。この規則を満たさない特殊なケースが 1 つあります。被除数がその型で可能な最大の大きさの負の整数で、除数が -1 の場合、整数オーバーフローが発生し、結果は被除数と等しくなります。この場合、オーバーフローがあっても例外はスローされません。一方、整数除算の除数の値が 0 の場合、ArithmeticException がスローされます。

浮動小数点除算の結果は、IEEE 演算の仕様によって決まります。

If either operand is NaN, the result is NaN.
If the result is not NaN, the sign of the result is positive if both operands have the same sign, negative if the operands have different signs.
Division of an infinity by an infinity results in NaN.
Division of an infinity by a finite value results in a signed infinity. The sign is determined by the rule stated above.
Division of a finite value by an infinity results in a signed zero. The sign is determined by the rule stated above.
Division of a zero by a zero results in NaN; division of zero by any other finite value results in a signed zero. The sign is determined by the rule stated above.
Division of a nonzero finite value by a zero results in a signed infinity. The sign is determined by the rule stated above.
In the remaining cases, where neither an infinity nor NaN is involved, the exact mathematical quotient is computed. A floating-point value set is then chosen:
    If the division expression is FP-strict (§15.4):
        If the type of the division expression is float, then the float value set must be chosen.
        If the type of the division expression is double, then the double value set must be chosen. 
    If the division expression is not FP-strict:
        If the type of the division expression is float, then either the float value set or the float-extended-exponent value set may be chosen, at the whim of the implementation.
        If the type of the division expression is double, then either the double value set or the double-extended-exponent value set may be chosen, at the whim of the implementation. 

Next, a value must be chosen from the chosen value set to represent the quotient. If the magnitude of the quotient is too large to represent, we say the operation overflows; the result is then an infinity of appropriate sign. Otherwise, the quotient is rounded to the nearest value in the chosen value set using IEEE 754 round-to-nearest mode. The Java programming language requires support of gradual underflow as defined by IEEE 754 (§4.2.4). 

オーバーフロー、アンダーフロー、ゼロによる除算、または情報の損失が発生する可能性があるという事実にもかかわらず、浮動小数点除算演算子 / の評価で実行時例外がスローされることはありません。

これはhttp://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.17.2で確認できます。

于 2011-09-08T15:43:04.683 に答える