Chapter 18 The Mathematical Library

The math library comprises a standard set of mathematical functions, such as
trigonometric functions (sin, cos, tan, asin, acos, etc.), exponentiation and logarithms
(exp, log, log10), rounding functions (floor, ceil), max, min, functions
for generating pseudo-random numbers (random, randomseed), plus variables pi
and huge, which is the largest representable number. (huge may be the special

value inf in some platforms.)


All trigonometric functions work in radians. You can use the functions deg
and rad to convert between degrees and radians. If you want to work in degrees,
you can redefine the trigonometric functions:
do
local sin, asin, ... = math.sin, math.asin, ...
local deg, rad = math.deg, math.rad
math.sin = function (x) return sin(rad(x)) end
math.asin = function (x) return deg(asin(x)) end
...
end  这样子多麻烦,除非整体上需要这么转换,,,不过我们可以写成module  ,这样我们就不需要重复写,,,

math.random() no argument, return   x[0 1]

math.random(N) N is integer ,return  x[1 N]

math.random(N1,N2) N1,N2 is integer and  N1<N2,  ,return  x[N1 N2]

You can set a seed for the pseudo-random generator with the randomseed
function; its numeric sole argument is the seed. Usually, when a program
starts, it initializes the generator with a fixed seed. This means that, every
time you run your program, it generates the same sequence of pseudo-random
numbers. For debugging, this is a nice property; but in a game, you will have
the same scenario over and over. A common trick to solve this problem is to
use the current time as a seed, with the call math.randomseed(os.time()). The
os.time function returns a number that represents the current time, usually as
the number of seconds since some epoch.


Exercise 18.1: Write a function to test whether a given number is a power of
two

local function isPOT(n)
  sk=math.sqrt(n);
  if sk- math.floor(sk)==0 then
  return true;
  else
  return false;
  end
end









你可能感兴趣的:(Chapter 18 The Mathematical Library)