よく使う述語: 数値演算編
Mercury の標準ライブラリから,数値演算に関するよく使いそうな述語をピックアップしました.
float モジュール (浮動小数点数)
基本演算
:- func + (float::in) = (float::uo) is det. :- func - (float::in) = (float::uo) is det.
前置演算子
:- func (float::in) + (float::in) = (float::uo) is det. :- func (float::in) - (float::in) = (float::uo) is det. :- func (float::in) * (float::in) = (float::uo) is det. :- func (float::in) / (float::in) = (float::uo) is det.
中置演算子
:- pred (float::in) < (float::in) is semidet. :- pred (float::in) =< (float::in) is semidet. :- pred (float::in) >= (float::in) is semidet. :- pred (float::in) > (float::in) is semidet.
比較演算子
整数を浮動小数点数に変換
:- func float(int) = float.
浮動小数点数を整数に変換
:- func ceiling_to_int(float) = int. :- func floor_to_int(float) = int. :- func round_to_int(float) = int. :- func truncate_to_int(float) = int.
- ceiling: 切り上げ
- floor: 切り捨て
- round: 四捨五入
- trancate: 0に向かって丸める
float.abs
:- func abs(float) = float.
絶対値
float.max
:- func max(float, float) = float.
値が大きい方
float.min
:- func min(float, float) = float.
値が小さい方
特殊な値の判定
:- pred is_nan(float::in) is semidet. :- pred is_inf(float::in) is semidet. :- pred is_nan_or_inf(float::in) is semidet.
math モジュール
有名な定数
:- func math.pi = float. :- func math.e = float.
math.sqrt
:- func math.sqrt(float) = float.
対数
:- func math.pow(float, float) = float. :- func math.exp(float) = float. :- func math.ln(float) = float. :- func math.log10(float) = float. :- func math.log2(float) = float. :- func math.log(float, float) = float.
三角関数
:- func math.sin(float) = float. :- func math.cos(float) = float. :- func math.tan(float) = float. :- func math.asin(float) = float. :- func math.acos(float) = float. :- func math.atan(float) = float. :- func math.atan2(float, float) = float. :- func math.sinh(float) = float. :- func math.cosh(float) = float. :- func math.tanh(float) = float.
integer モジュール (多倍長整数)
定数
:- func integer.zero = integer. :- func integer.one = integer.
変換
:- func integer.integer(int) = integer. :- func integer.to_string(integer) = string. :- func integer.from_string(string::in) = (integer::out) is semidet. :- func integer.float(integer) = float. :- func integer.int(integer) = int.
基本演算
:- func integer + integer = integer. :- func integer - integer = integer. :- func integer * integer = integer. :- func integer div integer = integer. :- func integer mod integer = integer. :- func integer.abs(integer) = integer.