[pgsql-jp: 27714] Re: float4を使った計算について

sugita @ sra.co.jp sugita @ sra.co.jp
2002年 10月 22日 (火) 13:48:52 JST


  杉田です。

From: sugita @ sra.co.jp
Subject: [pgsql-jp: 27711] Re: float4を使った計算について
Date: Tue, 22 Oct 2002 12:04:51 +0900 (JST)

;;; ;;; ロス率を含めた合計数を求めたいと思っていますが、フィールドにfloat4を
;;; ;;; 使った計算結果が期待した値にならないのはなぜかを教えていただけますで
;;; ;;; しょうか。
;;; ;;; f1が数量、f2がロス率としています。
;;; ;;; 
;;; ;;; postgres=# create table test(f1 float4,f2 float4);
;;; ;;; CREATE
;;; ;;; postgres=# insert into test values(1.2,5);
;;; ;;; INSERT 38183619 1
;;; ;;; postgres=# insert into test values(0.7,0);
;;; ;;; INSERT 38183623 1
;;; ;;; postgres=# insert into test values(1.5,0);
;;; ;;; INSERT 38183624 1
;;; ;;; postgres=# insert into test values(1.2,1.1);
;;; ;;; INSERT 38183625 1
;;; ;;; postgres=# create view test_view as select f1,f2,f1*f2/100 as f3 from test;
;;; ;;; CREATE
;;; ;;; postgres=# select f1,f2,f3,f1+f3 as total from test_view;
;;; ;;;  f1  | f2  |         f3         |       total
;;; ;;; -----+-----+--------------------+-------------------
;;; ;;;  1.2 |   5 |               0.06 |  1.26000004768372
;;; ;;;  0.7 |   0 |                  0 | 0.699999988079071
;;; ;;;  1.5 |   0 |                  0 |               1.5
;;; ;;;  1.2 | 1.1 | 0.0132000005245209 |  1.21320004820824
;;; ;;; 
;;; ;;; 
;;; ;;; そもそもfloat4はこのように使うべきでないのでしょうか。
;;; 
;;;   f1*f2/100 の 100 を float4 にするのはどうでしょう。

  float4 にすると次のようになります。 

    create view test_view as select f1,f2,f1*f2/100::float4 as f3 from test;
    CREATE
    select f1,f2,f3,f1+f3 as total from test_view;
     f1  | f2  |   f3   | total  
    -----+-----+--------+--------
     1.2 |   5 |   0.06 |   1.26
     0.7 |   0 |      0 |    0.7
     1.5 |   0 |      0 |    1.5
     1.2 | 1.1 | 0.0132 | 1.2132
    (4 rows)


Kenji Sugita                                      




pgsql-jp メーリングリストの案内