select ID,(0.0*a/b)*log(0.0*a/b)/log(10) as Entropy from table1

解决方案 »

  1.   

    因为参与计算的全部是decimal类型,所以结果也应该是decimal型
      

  2.   

    对于0.0012345..之类的比较小的数在放入表中就变成了.12345-E2的形式,当我更新或写入数据时,就会出错:数据类型varchar与numeric不符,请问各位有什么好办法
      

  3.   

    --不知道为什么说计算不出来,我的电脑上就很正常
    declare @t table(id int,a decimal(20,2),b decimal(20,2))
    insert into @t
    select 1,1,1
    union all select 2,1,2select ID,(a/b)*log(a/b)/log(10) as Entropy from @t/*--结果:ID          Entropy                                               
    ----------- ----------------------------------------------------- 
    1           0.0
    2           -0.15051499783199057
    (所影响的行数为 2 行)
    --*/
      

  4.   

    --如果楼主说的decimal指int/bigint,就要改用:
    declare @t table(id int,a int,b int)
    insert into @t
    select 1,1,1
    union all select 2,1,2
    --应该执行此句:
    select ID,(cast(a as decimal(20))/b)*log(cast(a as decimal(20))/b)/log(10) as Entropy from @t
    /*--结果:ID          Entropy                                               
    ----------- ----------------------------------------------------- 
    1           0.0
    2           -0.15051499783199057
    (所影响的行数为 2 行)
    --*/
      

  5.   

    --如果楼主因为存储格式的问题,可以通过数据类型转换,只保留指定位数的小数.
    declare @t table(id int,a decimal(20,2),b decimal(20,2))
    insert into @t
    select 1,1,1
    union all select 2,1,2select ID,cast((a/b)*log(a/b)/log(10) as decimal(20,2)) as Entropy from @t