2010-06-23 5 views
1

Microsoft SQL Server의 모듈러스 함수는 특정 데이터 형식에서만 작동합니다.TSQL에서 float의 모듈러스를 계산하는 방법은 무엇입니까?

은 MSDN 기사에 따르면 [1] 계수 연산자, 당신은 일반적으로 배당이 부동 소수점 데이터 유형 인 경우 작동하지 않습니다 ... 그러나이 같은

dividend % divisor 

dividend 
Is the numeric expression to divide. dividend must be a valid 
expression of any one of the data types in the integer and 
monetary data type categories, or the numeric data type. 

divisor 
Is the numeric expression by which to divide the dividend. 
divisor must be any valid expression of any one of the data 
types in the integer and monetary data type categories, or 
the numeric data type. 

를 계수를 사용합니다. 우리가 생각해 낸 대답은 나중에 참조 할 수 있도록 아래에 나열되어 있습니다.

[1] http://msdn.microsoft.com/en-us/library/ms190279.aspx

+2

. 정확하지 않은 데이터 유형이므로 반올림 문제가 발생할 수 있습니다. – HLGEM

+0

좋은 지적. 나는 오류 메시지가 지난 문제에 대해 생각하지 않았다. 그 지혜의 작은 조각을 가져 주셔서 감사합니다. –

답변

4

십진수/숫자, 모듈로 및 캐스트 백으로 캐스트 할 수 있습니까? 당신이 수학 계산을하고 있다면 당신은 부동 소수점 데이터 형식에 데이터를 저장하지 않아야하기 때문이다

CAST(CAST(TheInaccurateFloatValue AS decimal(38,19)) % ModuloValue AS float) 
+0

또 다른 좋은 해결책입니다. –

1

이 내가 생각 해낸 대답이다. 배당금이 부동 소수점이 아니라 부동 소수점 인 경우에만 적용됩니다.

(cast(dividend as integer) % divisor) + (dividend - cast(dividend as integer)) 
1
declare @A float = 2.5 
declare @B float = 1.1 

-- Expected: A % B = 2.5 % 1.1 = 0.3 

select @A - floor(@A/@B) * @B 
관련 문제