double d = 0; // random decimal value with it's integral part within the range of Int32 and always positive.
int floored = (int) Math.Floor(d); // Less than or equal to.
int ceiled = (int) Math.Ceiling(d); // Greater than or equal to.
int lessThan = ? // Less than.
int moreThan = ? // Greater than.
下限関数と天井関数には、それぞれ以下/以上の最大/最小整数が含まれ d
ます。それぞれより小さい/大きいが等しくない d
最大/最小の整数を見つけたいです。
もちろん、これはいくつかの方法で実現できますがif's and but's
、この操作はアルゴリズムで何十億回も実行されるため、分岐を含まないか、少なくとも非常に高速な方法を探しています。
バイナリ操作は可能ですか? そうでない場合、最良の代替手段は何ですか?
明らかな解決策は次のようになります。
int lessThan = (d - floored) > double.Epsilon ? floored : (floored-1);
int moreThan = (ceiled - d) > double.Epsilon ? ceiled : (ceiled+1);
注: 目的は、 がまたはd
に近いかどうかを調べることです。lessThan
moreThan