.
std::min(-0.0,0.0) = -0.0
std::max(-0.0,0.0) = -0.0
는
fmin(-0.0, 0.0) = -0.0
fmax(-0.0, 0.0) = 0.0
반면 그래서
std::min
는
fmin
의 1-1 대체하지 않습니다.
std::min
및
std::max
함수는 교환 가능하지 않습니다. 인수
fmin(-0.0, 0.0) = std::min(-0.0, 0.0)
fmax(-0.0, 0.0) = std::max(0.0, -0.0)
그러나 지금까지의 내가
all these functions are implementation defined anyway in this case을 말할 수 그래서 당신은 그들이 어떻게 구현되는지 테스트해야 100 % 확신 할 수를 교환해야
fmin
및
fmax
하나와 두 배와 같은 결과를 얻을 수 있습니다.
또 다른 중요한 차이점이 있습니다. x ! = NaN
들어 :
std::max(Nan,x) = NaN
std::max(x,NaN) = x
std::min(Nan,x) = NaN
std::min(x,NaN) = x
fmax
fmax(Nan,x) = x
fmax(x,NaN) = x
fmin(Nan,x) = x
fmin(x,NaN) = x
하는 반면, 다음 코드
double myfmax(double x, double y)
{
// z > nan for z != nan is required by C the standard
int xnan = isnan(x), ynan = isnan(y);
if(xnan || ynan) {
if(xnan && !ynan) return y;
if(!xnan && ynan) return x;
return x;
}
// +0 > -0 is preferred by C the standard
if(x==0 && y==0) {
int xs = signbit(x), ys = signbit(y);
if(xs && !ys) return y;
if(!xs && ys) return x;
return x;
}
return std::max(x,y);
}
이 std::max
가 fmax
의 일부임을 나타낸다 에뮬레이트 될 수있다.
어셈블리를 보면 Clang이 fmax
및 fmin
에 대해 내장 코드를 사용하는 반면 GCC는 수학 라이브러리에서 코드를 호출합니다.-O3
와 fmax
대한 연타의 조립체 std::max(double, double)
것이 -Ofast
fmax
를 사용 GCC와 연타 들어
maxsd xmm0, xmm1
그러나 단순히 반면
movapd xmm2, xmm0
cmpunordsd xmm2, xmm2
movapd xmm3, xmm2
andpd xmm3, xmm1
maxsd xmm1, xmm0
andnpd xmm2, xmm1
orpd xmm2, xmm3
movapd xmm0, xmm2
단순히 그래서 방송
maxsd xmm0, xmm1
진다 다시 std::max
은 fmax
의 하위 집합입니다. 그리고 nan
또는 부호가없는 0이 아닌 더 가벼운 부동 소수점 모델을 사용하면 fmax
과 std::max
은 동일합니다. 동일한 논거는 분명히 fmin
과 std::min
에 적용됩니다.
경고 : min과 max은 똑같은 유형의 두 변수 만 비교할 수 있습니다. 그래서 int와 double을 비교할 수 없습니다. ( –
True - 최대 (1, 2.0)이 작동하지 않습니다. 최대 (1, 2.0) 또는 최대 (double (1), 2.0)와 같아야합니다. –
어떤 것이 Good Thing ™ IMO입니까? – Cogwheel