# pike.git / src / modules / Math / module.pmod.in

 version » Context lines: 10204080filenone3

pike.git/src/modules/Math/module.pmod.in:226:    float cos() {    return predef::cos(rad());    }       //! Returns the tangen for the angle.    float tan() {    return predef::tan(rad());    }       //! An angle can be casted to float, int and string. -  float|int|string cast(string to) { +  protected float|int|string cast(string to) {    switch(to) {    case "float": return (float)angle;    case "int": return (int)angle;    case "string": return (string)angle;    } -  error( "Could not cast to %s.\n", to ); +  return UNDEFINED;    }       //! Returns the sum of this angle and what it is added with. If    //! added with an angle, a new angle object is returnes.    float|int|Angle `+ (float|int|Angle _angle) {    if(objectp(_angle)) return Angle( angle + _angle->get(type), type );    return angle + _angle;    }       //! @decl Angle add(float|int angle)
pike.git/src/modules/Math/module.pmod.in:336:    case "gon": return "Angle(" + angle + " gons)";    case "str": return "Angle(" + angle + " streck)";    }    case 't':    return "Angle";    }    return UNDEFINED;    }   }    - //! Calculate binomial koefficient @[n] choose @[k]. + //! Calculate the binomial coefficient @[n] choose @[k].   //! - //! This is equvivalent to @tt{@[n]!/(@[k]!*(@[n]-@[k])!)@}. + //! This is equal to @tt{@[n]!/(@[k]!*(@[n]-@[k])!)@}.   int choose(int n, int k)   {    if (k*2 > n) k = n-k;    if (!k) return 1;    if (k < 0) return 0;    int val = 1;    for (int i=1; i <= k; i++,n--) {    val *= n;    val /= i;    }    return val;   }      //! The 10-logarithm of @[x]. - float log10(float x) { return log(x)/log(10.0); } + float log10(int|float x) { return log(x)/log(10.0); }      //! The 2-logarithm of @[x]. - float log2(float x) { return log(x)/log(2.0); } + float log2(int|float x) { return log(x)/log(2.0); }    - //! The @[n]-logatirhm of @[x]. - float logn(float n, float x) { return log(x)/log(n); } + //! The @[n]-logarithm of @[x]. + float logn(int|float n, int|float x) { return log(x)/log(n); }    -  - // Only available when there is Gmp.mpz - #if constant(Gmp.mpz) -  +    //! Factorize the integer @[x]. The returned list of factors will   //! be sorted biggest to smallest factor. -  + //!   //! @note - //! This function is only available when Pike has been compiled with - //! bignums. + //! In Pike versions prior to v8.0, only primes @expr{<= 8161@} were + //! considered.   array(int) factor(int x) { -  int f = Gmp.mpz(x)->small_factor(); -  if(f==0) +  if (!x) return ({ 0 }); +  if (x==1) return ({ 1 }); +  int f = x->small_factor(); +  if(!f) { +  /* No small factors left. */ +  if (x->probably_prime_p()) { +  /* The remainder seems to be a prime. */    return ({ x }); -  else +  } +  array(int) ret = ({}); +  int p = 8161; /* The largest prime known by small_factor(). */ +  while (x > p) { +  p = p->next_prime(); +  if (!(x % p)) { +  while (!(x % p)) { +  ret = ({ p }) + ret; +  x /= p; +  } +  if (x->probably_prime_p()) break; +  } +  } +  if (x != 1) ret = ({ x }) + ret; +  return ret; +  } else    return factor(x/f) + ({ f });   } - #endif +