Next: , Previous: map, Up: Top


41 math

     %--------------------------------------------------%
     % vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
     %--------------------------------------------------%
     % Copyright (C) 1995-2007, 2011-2012 The University of Melbourne.
     % This file may only be copied under the terms of the GNU Library General
     % Public License - see the file COPYING.LIB in the Mercury distribution.
     %--------------------------------------------------%
     %
     % File: math.m.
     % Main author: bromage.
     % Stability: high.
     %
     % Higher mathematical operations.  (The basics are in float.m.)
     %
     % By default, domain errors are currently handled by throwing an exception.
     % For better performance, each operation in this module that can throw a domain
     % exception also has an unchecked version that omits the domain check.
     %
     % The unchecked operations are semantically safe, since the target math
     % library and/or floating point hardware perform these checks for you.
     % The benefit of having the Mercury library perform the checks instead is
     % that Mercury will tell you in which function or predicate the error
     % occurred, as well as giving you a stack trace if that is enabled; with
     % the unchecked operations  you only have the information that the
     % floating-point exception signal handler gives you.
     %
     %--------------------------------------------------%
     %--------------------------------------------------%
     
     :- module math.
     :- interface.
     
         % A domain error exception, indicates that the inputs to a function
         % were outside the domain of the function.  The string indicates
         % where the error occurred.
         %
     :- type domain_error ---> domain_error(string).
     
     %--------------------------------------------------%
     %
     % Mathematical constants
     %
     
         % Pythagoras' number.
         %
     :- func math.pi = float.
     
         % Base of natural logarithms.
         %
     :- func math.e = float.
     
     %--------------------------------------------------%
     %
     % "Next integer" operations
     %
     
         % math.ceiling(X) = Ceil is true if Ceil is the smallest integer
         % not less than X.
         %
     :- func math.ceiling(float) = float.
     
         % math.floor(X) = Floor is true if Floor is the largest integer
         % not greater than X.
         %
     :- func math.floor(float) = float.
     
         % math.round(X) = Round is true if Round is the integer closest to X.
         % If X has a fractional value of 0.5, it is rounded up.
         %
     :- func math.round(float) = float.
     
         % math.truncate(X) = Trunc is true if Trunc is the integer closest to X
         % such that |Trunc| =< |X|.
         %
     :- func math.truncate(float) = float.
     
     %--------------------------------------------------%
     %
     % Polynomial roots
     %
     
         % math.sqrt(X) = Sqrt is true if Sqrt is the positive square root of X.
         %
         % Domain restriction: X >= 0
         %
     :- func math.sqrt(float) = float.
     :- func math.unchecked_sqrt(float) = float.
     
     :- type math.quadratic_roots
         --->    no_roots
         ;       one_root(float)
         ;       two_roots(float, float).
     
         % math.solve_quadratic(A, B, C) = Roots is true if Roots are
         % the solutions to the equation Ax^2 + Bx + C.
         %
         % Domain restriction: A \= 0
         %
     :- func math.solve_quadratic(float, float, float) = quadratic_roots.
     
     %--------------------------------------------------%
     %
     % Power/logarithm operations
     %
     
         % math.pow(X, Y) = Res is true if Res is X raised to the power of Y.
         %
         % Domain restriction: X >= 0 and (X = 0 implies Y > 0)
         %
     :- func math.pow(float, float) = float.
     :- func math.unchecked_pow(float, float) = float.
     
         % math.exp(X) = Exp is true if Exp is e raised to the power of X.
         %
     :- func math.exp(float) = float.
     
         % math.ln(X) = Log is true if Log is the natural logarithm of X.
         %
         % Domain restriction: X > 0
         %
     :- func math.ln(float) = float.
     :- func math.unchecked_ln(float) = float.
     
         % math.log10(X) = Log is true if Log is the logarithm to base 10 of X.
         %
         % Domain restriction: X > 0
         %
     :- func math.log10(float) = float.
     :- func math.unchecked_log10(float) = float.
     
         % math.log2(X) = Log is true if Log is the logarithm to base 2 of X.
         %
         % Domain restriction: X > 0
         %
     :- func math.log2(float) = float.
     :- func math.unchecked_log2(float) = float.
     
         % math.log(B, X) = Log is true if Log is the logarithm to base B of X.
         %
         % Domain restriction: X > 0 and B > 0 and B \= 1
         %
     :- func math.log(float, float) = float.
     :- func math.unchecked_log(float, float) = float.
     
     %--------------------------------------------------%
     %
     % Trigonometric operations
     %
     
         % math.sin(X) = Sin is true if Sin is the sine of X.
         %
     :- func math.sin(float) = float.
     
         % math.cos(X) = Cos is true if Cos is the cosine of X.
         %
     :- func math.cos(float) = float.
     
         % math.tan(X) = Tan is true if Tan is the tangent of X.
         %
     :- func math.tan(float) = float.
     
         % math.asin(X) = ASin is true if ASin is the inverse sine of X,
         % where ASin is in the range [-pi/2,pi/2].
         %
         % Domain restriction: X must be in the range [-1,1]
         %
     :- func math.asin(float) = float.
     :- func math.unchecked_asin(float) = float.
     
         % math.acos(X) = ACos is true if ACos is the inverse cosine of X,
         % where ACos is in the range [0, pi].
         %
         % Domain restriction: X must be in the range [-1,1]
         %
     :- func math.acos(float) = float.
     :- func math.unchecked_acos(float) = float.
     
         % math.atan(X) = ATan is true if ATan is the inverse tangent of X,
         % where ATan is in the range [-pi/2,pi/2].
         %
     :- func math.atan(float) = float.
     
         % math.atan2(Y, X) = ATan is true if ATan is the inverse tangent of Y/X,
         % where ATan is in the range [-pi,pi].
         %
     :- func math.atan2(float, float) = float.
     
     %--------------------------------------------------%
     %
     % Hyperbolic functions
     %
     
         % math.sinh(X) = Sinh is true if Sinh is the hyperbolic sine of X.
         %
     :- func math.sinh(float) = float.
     
         % math.cosh(X) = Cosh is true if Cosh is the hyperbolic cosine of X.
         %
     :- func math.cosh(float) = float.
     
         % math.tanh(X) = Tanh is true if Tanh is the hyperbolic tangent of X.
         %
     :- func math.tanh(float) = float.
     
     %--------------------------------------------------%
     %--------------------------------------------------%