LoginSignup
4
5

More than 5 years have passed since last update.

FLOAT型とDECIMAL型の使い分け

Last updated at Posted at 2013-11-01

いくつか定義した確率(0, 0.1, 0.05, 0.01, 0.001)に対するカラムを検索したいなとおもったときに

sample.sql
select * from sometable where probability = 0.1

とやったら、マッチしない!
なんでだ?と思って調べていたら下記に行き着いた。

If you are comparing FLOAT or DOUBLE columns with numbers that have decimals, you can't use equality (=) comparisons. This problem is common in most computer languages because not all floating-point values can be stored with exact precision. In some cases, changing the FLOAT to a DOUBLE fixes this. See 「Problems with Floating-Point Comparisons」.

要は、浮動小数点は完全に(=)にならないよってことなので、

sample.sql
select * from sometable where probability >= 0.1 and probability <= 0.11

みたいなトリッキーなことしないとダメっぽい。

なので、四則演算に使わないような少数値は、FLOAT型ではなく、DECIMAL型を使いましょう。

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5