Skip to content

Instantly share code, notes, and snippets.

@toby55kij
Created October 10, 2010 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toby55kij/619061 to your computer and use it in GitHub Desktop.
Save toby55kij/619061 to your computer and use it in GitHub Desktop.
// g100pon #56 数値型(Javaとの相違点など)
// 実数は指定が無い限りBigDecimal
assert (0.5).class == java.math.BigDecimal
// Float同士の加減乗算の結果はDouble(JavaではFloat)
assert (1f + 2f).class == java.lang.Double
assert (1f - 2f).class == java.lang.Double
assert (1f * 2f).class == java.lang.Double
// Float同士の除算の結果はDouble
assert (1f / 2f).class == java.lang.Double
// 整数同士の除算の結果は実数(Javaでは整数)
assert 1 / 2 == 0.5
// 除算で割り切れない場合四捨五入(HALF_UP)で丸められる
assert 2 / 3 == 0.6666666667
// 丸めの精度は10か引数の精度の大きい方
assert 1000000 / 3 == 333333.3333333333
// 引数の精度が10以上の場合
assert 0.00000000001 / 3 == 0.0
assert 0.00000000002 / 3 == 1E-11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment