Skip to content
This repository
tag: v1200
Fetching contributors…

Cannot retrieve contributors at this time

file 85 lines (69 sloc) 2.552 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* {1 About this module}
*
* {1 Where should I start?}
*
* {1 What if I need more?}
*/

/**
* {1 Interface}
*/

Math = {{

  /**
* {2 Constants}
**/

  E = 2.718281828459045235360
  PI = 3.141592653589793238462
  SQRT2 = 1.414213562373095048802

  /**
* {2 Methods}
**/

  acos = %% BslNumber.Math.acos %% : float -> float
  asin = %% BslNumber.Math.asin %% : float -> float
  atan = %% BslNumber.Math.atan %% : float -> float
  ceil = %% BslNumber.Math.ceil %% : float -> float
  cos = %% BslNumber.Math.cos %% : float -> float
  exp = %% BslNumber.Math.exp %% : float -> float
  floor = %% BslNumber.Math.floor %% : float -> float
  is_NaN = %% BslNumber.Math.isNaN %% : float -> bool
  is_infinite = %% BslNumber.Math.is_infinite %% : float -> bool
  is_normal = %% BslNumber.Math.is_normal %%: float -> bool
  ln = %% BslNumber.Math.log %% : float -> float
  sin = %% BslNumber.Math.sin %% : float -> float
  sqrt_f = %% BslNumber.Math.sqrt_f %% : float -> float
  sqrt_i = %% BslNumber.Math.sqrt_i %% : int -> int
  tan = %% BslNumber.Math.tan %% : float -> float

  /**
* An interesting variant of Float.round, which returns a float (instead of an int),
* and which rounds 0.5 to 1. (instead of 0 for Float.round)
* (well, on server-side; of course it's different on client-side)
**/
  round(n:float) = floor(n + 0.5)

  square_f(n:float) = n * n
  square_i(n:int) = n * n

  /**
* You can use pow_f ONLY when a is greater than 0
**/
  pow_f(a, b:float) = Math.exp(b * Math.ln(a))

  pow_i(a, b) = match b
    0 -> 1
    1 -> a
    _ ->
    if b < 0 then error("Math.pow_i with invalid number")
    else
      square_i( pow_i(a, b / 2)) * (if mod(b, 2) == 0 then 1 else a)

  div_sup(n, d) = (n + d - 1) / d
}}
Something went wrong with that request. Please try again.