English 中文(简体)
计算时的比额表损失
原标题:loss of scale when performing calculation

我进行计算,我没有获得我期望的答案。 我失去了计算方法的一些比额表。

Calc is: 651/1000 * - 413.72063274 = - 269.33213191 (to 8 d.p)

联合国 我这样做:

declare @var numeric(28,8)
declare @a numeric(28,8)
declare @b numeric(28,8)

set @var = -413.72063274
set @a   = 651.00000000
set @b   = 1000.00000000

select CAST((@a/@b) * @var as numeric(28,8)) as result_1
     , CAST(CAST(@a as numeric(28,8)) 
      /CAST(@b as numeric(28,8)) as numeric(28,8)) 
      *CAST(@var as numeric (28,8))   as result_2

结果是

result_1: -269.33213200 (correct to 6dp)
result_2 : -269.332132 (correct to 6dp)

我如何回来:-269.33213191(更正为8dp)?

最佳回答

For this specific case, you could cheat by first multiplying all values and dividing the final result back with this multiplier.

declare @var numeric(28,8)
declare @a numeric(28,8)
declare @b numeric(28,8)
declare @m numeric(28,8)

set @var = -413.72063274
set @a   = 651.00000000
set @b   = 1000.00000000
set @m   = 10000000


select CAST(((@a*@m) * (@var*@m) / (@b*@m)) AS NUMERIC(28, 8)) / @m 
-- Result: -269.3321319137

<><>Edit>/strong>

另一方面,在保持其准确性而不使用乘数的情况下,

select CAST(@a * @var AS NUMERIC(28, 8)) / @b
-- Result: -269.3321319140
问题回答

*The result precision and scale have an absolute maximum of 38. When a result precision is greater than 38, the corresponding scale is reduced to prevent the integral part of a result from being truncated.

但没有确切说明这种拖网是如何进行的。 页: 1 然而,有时使用审判和错误更容易!

以下中间派系给你预期的结果。 你们能否与他们一起生活?

DECLARE @var NUMERIC(19,8)
DECLARE @a NUMERIC(19,8)
DECLARE @b NUMERIC(19,8)

SET @var = -413.72063274
SET @a   = 651.00000000
SET @b   = 1000.00000000

DECLARE @v SQL_VARIANT 
SET @v =  CAST(@a/@b AS NUMERIC(24,10))* CAST(@var AS NUMERIC(23,8))

SELECT    CAST(SQL_VARIANT_PROPERTY(@v,  BaseType ) AS VARCHAR(30)) AS BaseType,    
          CAST(SQL_VARIANT_PROPERTY(@v,  Precision ) AS INT) AS PRECISION,    
          CAST(SQL_VARIANT_PROPERTY(@v,  Scale ) AS INT) AS Scale

你们先是分裂,然后又越多。 精确度的损失在该司。 这四舍五入到八分之八,结果乘以3位数(413)价值(最后两位数失踪)

解决办法是提高中间成果的精确度,或首先实现你的多重复。

(@a * @var) / @b)在数学上等同于您的计算,但可以得出更准确的结果。





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签