2つのdouble型変数の大小関係を調べる。
Compare two double-type variables.
◆形式(Format)
#include <doublemath.h>
inline int doublecmp(const double a,const double b)
◆引数(Arguments)
a
比較したい一方の実数。
One of the real numbers to be compared.
b
比較したいもう一つの実数。
The other one of the real numbers to be compared.
◆戻り値(Return value)
のとき1
1 if
のとき0
0 if
のとき if
但しとが非常に近い値の場合は厳密に等しくなくてもと見なす。
詳しくは下記「判定方法」参照。
When and are very close to each other,
it is regarded that , even if they are not exactly equal.
For detail, see “Judgements” section below.
浮動小数点数では内部のビット表現の問題により、
本当は等しい2つの実数が僅かに異なる値を持つ場合がある。
例えば論理式
(1.0/3.0)∗3.0==1.0
はfalseとなる。
そこでこの関数では完全にでなくとも
との差がある閾値以下であればであると見なす。
その閾値のもとでと見なされた場合は、
と見なされた場合は0、
と見なされた場合は1を返す。
Due to internal expressions of floating-point variables,
two real numbers which are equal
may have slightly different values to each other.
For example, a logical formula
(1.0/3.0)∗3.0==1.0
gives false.
In this function, and are regarded to be equal
if the difference between them is below a threshold level,
even if they are not exactly equal.
The return values are , 0, and 1
if it is regarded that , , and , respectively,
based on comparisons of the differences to the threshold level.
であると見なす基準としては
を用いている
(ZERO_THRESHOLDはmacro.hで定義されたマクロ)。
但し、0による割り算を防ぐため、
の場合は別扱いで0を返すようにしている。
これが成り立つのは厳密にの場合だけであるので0を返して問題無い。
For a threshold to regard as , eq. () is used,
where ZERO_THRESHOLD is a macro defined in macro.h.
To avoid a division by 0,
the return value of this function is set to 0
in case of .
This operation is validated by noticing that
the condition holds
only when strictly .
()式の左辺の分母は
,の値そのものの大きさのスケールの目安と思えば良い。
例えばとした場合、
が,の大きさのスケールと比較しておおよそ6桁程度小さければ
事実上と見なすというのが()の意味するところである。
このような基準を用いているので
結果はやの値そのもののスケールには依存しない。
すなわち任意の正定数について、
doublecmp(a,b)とdoublecmp(c*a,c*b)は同じ結果を返す。
The denominator of the left hand side of eq. (),
i.e., ,
can be regarded to be a measure of the scales of and .
For example, when ,
eq. () means that
and are regarded to be equal
when the difference is smaller than the scales of and
by a factor of .
Using this criterion, the result is independent of
the scales of and ;
doublecmp(a,b) and doublecmp(c*a,c*b) returns exactly the same values
for arbitrary positive constants .
例外として、とのどちらか片方が厳密に0.0の場合には
もう片方の絶対値がZERO_THRESHOLDよりも小さければと見なす。
例えば厳密にの場合には
ならばと見なす。
そうしないとif(doublecmp(a,0.0)==0){}のような判定文は
aにほんの僅かでも数値誤差があれば成り立たなくなるからである。
An exception is that
when one of or is exactly 0.0
and the absolute value of the other is less than ZERO_THRESHOLD,
and are regarded to be equal.
For example, when is exactly 0.0
and ,
is regarded to be equal to .
This is because otherwise
a very slight numerical error in results in
a failure of judgement for if(doublecmp(a,0.0)==0){}.