SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[fn_LensThickCalcAC] (@sph as DECIMAL(9,2),
@cyl as DECIMAL(9,2),
@ri as DECIMAL(9,2),
@bc as DECIMAL(9,2),
@dia as SMALLINT,
@ct as DECIMAL(9,2),
@axs as SMALLINT)
RETURNS FLOAT
AS
BEGIN
DECLARE @dR2S DECIMAL(9,2)
DECLARE @dR2SC DECIMAL(9,2)
DECLARE @dSizeA DECIMAL(9,2)
DECLARE @dSizeB DECIMAL(9,2)
DECLARE @dSizeC DECIMAL(9,2)
DECLARE @dSizeB_S FLOAT
DECLARE @dSizeB_SC FLOAT
DECLARE @dThickness DECIMAL(9,2)
DECLARE @dAxs DECIMAL(9,2)
DECLARE @dESizeB_S FLOAT
DECLARE @dESizeB_SC FLOAT
DECLARE @dDiffSC FLOAT
DECLARE @dR1 DECIMAL(9,2)
DECLARE @dESizeD FLOAT
DECLARE @dSizeD DECIMAL(9,2)
DECLARE @adjThick DECIMAL(9,2)
DECLARE @finalThick DECIMAL(9,2)
IF(@AXS > 0)
BEGIN
IF (@AXS >= 180)
BEGIN
SET @dAxs = @AXS - 90
END
ELSE
BEGIN
SET @dAxs = @AXS
END
END
ELSE
BEGIN
SET @dAxs = @AXS
END
SET @dR2S = ((@RI - 1.0) * 1000) / (@BC - @Sph)
SET @dDiffSC = @BC - (@Sph + @Cyl)
IF @dDiffSC = 0
BEGIN
SET @dDiffSC = @BC
END
SET @dR2SC = ((@RI - 1.0) * 1000) / (@dDiffSC)
SET @dESizeB_S = POWER(@dR2S,2) - POWER(@DIA /2,2)
SET @dESizeB_SC = POWER(@dR2SC,2) - POWER(@DIA /2,2)
SET @dSizeB_S = @dR2S - Sqrt(@dESizeB_S)
SET @dSizeB_SC = @dR2SC - Sqrt(@dESizeB_SC)
SET @dSizeA = @CT
IF @dSizeB_S > @dSizeB_SC
BEGIN
SET @dSizeB = @dSizeB_S
END
ELSE
BEGIN
SET @dSizeB = @dSizeB_SC
END
SET @dSizeC = @dSizeA + @dSizeB
SET @dR1 = (@RI - 1.0) * 1000 / (@BC)
SET @dESizeD = POWER(@dR1,2) - POWER(@DIA /2,2)
IF @dR1 > @dR2S
BEGIN
SET @dSizeD = @dR1 - sqrt(@dESizeD)
END
ELSE
BEGIN
SET @dSizeD = @dSizeB
END
SET @adjThick = @dSizeC - @dSizeD
IF @dSizeD > 3.50
BEGIN
SET @dSizeD = @dSizeD - 3.50
SET @finalThick = @adjThick + @dSizeD + 1.50
END
ELSE
BEGIN
SET @finalThick = @adjThick + 1.50
END
RETURN @finalThick
END
从上面存储的SQL函数中,尝试从中派生一个简单的C#应用程序。
产品:
public float calc2(decimal sph, decimal cyl, decimal bc, decimal dia, decimal ct, decimal Axs)
{
decimal ri = 1.74M, dAxis, dR2S, dR2SC, dR1;
decimal dDiffSC, dSizeB_S, dSizeB_SC, dESizeD, dESizeB_S, dESizeB_SC;
decimal dSizeA, dSizeB, dSizeC, dSizeD;
decimal adjthick;
float finaloutput;
dAxis = Axs > 0 ? (Axs >= 180 ? Math.Round(Axs - 90,2) : Math.Round(Axs,2)) : Math.Round(Axs,2);
dR2S = Math.Round((((Math.Round(ri, 2) - 1.0M) * 1000)/(bc - sph)),2);
dDiffSC = bc - (sph + cyl);
dDiffSC = dDiffSC == 0 ? bc : dDiffSC;
dR2SC = Math.Round((((Math.Round(ri, 2) - 1.0M) * 1000) / (dDiffSC)),2);
dESizeB_S = dPow(dR2S, 2M) - dPow((dia/2), 2M);
dESizeB_SC = dPow(dR2SC, 2M) - dPow((dia/2), 2M);
dSizeB_S = dR2S - (decimal)Math.Sqrt((double)dESizeB_S);
dSizeB_SC = dR2SC - (decimal)Math.Sqrt((double)dESizeB_SC);
dSizeA = Math.Round(ct, 2);
dSizeB = dSizeB_S > dSizeB_SC ? Math.Round(dSizeB_S,2) : Math.Round(dSizeB_SC,2);
dSizeC = Math.Round((dSizeA + dSizeB),2);
dR1 = Math.Round((((Math.Round(ri, 2) - 1.0M) * 1000) / (bc)),2);
dESizeD = dPow(dR1, 2M) - dPow((dia/2), 2M);
dSizeD = dR1 > dR2S ? Math.Round((dR1 - (decimal)Math.Sqrt((double)dESizeD)),2): Math.Round(dSizeB,2);
adjthick = Math.Round((dSizeC - dSizeD),2);
finaloutput = dSizeD > 3.50M ? (float)Math.Round((adjthick + (dSizeD - 3.50M) + 1.50M),2) : (float)Math.Round((adjthick + 1.50M),2);
return finaloutput;
}
public static decimal dPow(decimal x, decimal y)
{
Double X = (double)x;
Double Y = (double)y;
return (decimal)System.Math.Pow(X, Y);
}
但是,经过数小时的试验和更改数据类型的错误,(double,decimal,float等)。我仍然有不一致的地方。
使用这些值:
在SQL中执行存储的函数后
我得到了 4.46 的结果
在C#中运行我的代码后。
我得到了 4.52
它的差异很小,但随着sph
和cyl
的变化,这两种方法产生的差异也会增加。
问题:
我使用的数据类型中是否存在问题?
SQL Server的行为与C#不同吗?
这些差异还有其他可能的因素吗?
答案 0 :(得分:0)
更新:明白了。我需要将dia
和axs
的数据类型更改为SMALLINT
/ Int16
感谢您的帮助