本文整理汇总了PHP中is_finite函数的典型用法代码示例。如果您正苦于以下问题:PHP is_finite函数的具体用法?PHP is_finite怎么用?PHP is_finite使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_finite函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testInf
/**
* @covers Fisharebest\PhpPolyfill\Php::inf
* @covers Fisharebest\PhpPolyfill\Php::isLittleEndian
* @runInSeparateProcess
*/
public function testInf()
{
$this->assertTrue(is_float(Php::inf()));
$this->assertTrue(is_double(Php::inf()));
$this->assertTrue(is_infinite(Php::inf()));
$this->assertFalse(is_finite(Php::inf()));
}
示例2: toInteger
/**
* 与えられた値を整数型に変換して返します。
* @link http://www.hcn.zaq.ne.jp/___/WEB/WebIDL-ja.html#es-integers Web IDL (第2版 — 日本語訳)
* @param boolean|integer|float|string|resource|\GMP|\SplInt $value
* @param string $type byte、octet、short、unsigned short、long、unsigned long、long long、unsigned long long
* @param integer|float $min 浮動小数点型で正確に扱える整数の範囲よりも、整数型で扱える整数の範囲が狭ければ (整数型が32bitである環境なら) 浮動小数点数。
* @param integer|float $max 浮動小数点型で正確に扱える整数の範囲よりも、整数型で扱える整数の範囲が狭ければ (整数型が32bitである環境なら) 浮動小数点数。
* @param integer $bits
* @param booelan $signed
* @param string $extendedAttribute 拡張属性。[EnforceRange] か [Clamp] のいずれか。
* @return integer|float 整数型の範囲を超える場合は浮動小数点数。
* @throws \InvalidArgumentException 配列、NULL が与えられた場合。または、GMP、SplInt 以外のオブジェクトが与えられた場合。
* @throws \DomainException $extendedAttribute が [EnforceRange]、かつ与えられたの値が $min 〜 $max に収まらなかった場合。
*/
private static function toInteger($value, $type, $min, $max, $bits, $signed, $extendedAttribute = null)
{
/** @var string 要求される型。 */
$expectedType = sprintf('%s (an integer in the range of %s to %s)', $type, is_float($min) ? number_format($min, 0, '', '') : $min, is_float($max) ? number_format($max, 0, '', '') : $max);
if (!self::isIntegerCastable($value)) {
throw new \InvalidArgumentException(ErrorMessageCreator::create($value, $expectedType));
}
if ($value instanceof \GMP || is_resource($value) && get_resource_type($value) === 'GMP integer') {
// GMP数であれば、あらかじめ文字列に変換しておく
$value = gmp_strval($value);
}
/** @var integer|float 与えられた値の数値表現。整数型の範囲を超える場合は浮動小数点数。整数値となる場合、小数部があれば0方向へ丸められる。 */
$number = is_float($value) || (double) $value < self::$phpIntMin || (double) $value > PHP_INT_MAX ? (double) $value : (int) $value;
if ($extendedAttribute === '[EnforceRange]') {
/** @var integer|float 与えられた値の整数表現。整数型の範囲を超える場合は浮動小数点数。 */
$integer = self::roundTowardZero($number);
if (!is_finite($number) || $integer < $min || $integer > $max) {
throw new \DomainException(ErrorMessageCreator::create($value, $expectedType));
}
} elseif (!is_nan($number) && $extendedAttribute === '[Clamp]') {
$number = min(max($number, $min), $max);
$integer = is_float($number) ? round($number, 0, PHP_ROUND_HALF_EVEN) : $number;
} elseif (!is_finite($number)) {
$integer = 0;
} else {
$integer = self::modulo(self::roundTowardZero($number), pow(2, $bits));
if ($signed && $integer >= pow(2, $bits - 1)) {
$integer -= pow(2, $bits);
}
}
return is_float($integer) && $integer >= self::$phpIntMin && $integer <= PHP_INT_MAX ? (int) $integer : $integer;
}
示例3: matches
protected function matches($actual)
{
$asserter = new asserters\boolean();
try {
$asserter->setWith(is_finite($actual))->isTrue();
} catch (exception $exception) {
throw new exception($asserter, $this->analyzer->getTypeOf($actual) . ' is not finite');
}
}
示例4: toLine
/**
* Dumps information about a variable in readable format.
* @param mixed variable to dump
* @return string
*/
public static function toLine($var)
{
static $table;
if ($table === NULL) {
foreach (array_merge(range("", ""), range("", "ÿ")) as $ch) {
$table[$ch] = '\\x' . str_pad(dechex(ord($ch)), 2, '0', STR_PAD_LEFT);
}
$table['\\'] = '\\\\';
$table["\r"] = '\\r';
$table["\n"] = '\\n';
$table["\t"] = '\\t';
}
if (is_bool($var)) {
return $var ? 'TRUE' : 'FALSE';
} elseif ($var === NULL) {
return 'NULL';
} elseif (is_int($var)) {
return "{$var}";
} elseif (is_float($var)) {
if (!is_finite($var)) {
return str_replace('.0', '', var_export($var, TRUE));
// workaround for PHP 7.0.2
}
$var = str_replace(',', '.', "{$var}");
return strpos($var, '.') === FALSE ? $var . '.0' : $var;
// workaround for PHP < 7.0.2
} elseif (is_string($var)) {
if (preg_match('#^(.{' . self::$maxLength . '}).#su', $var, $m)) {
$var = "{$m['1']}...";
} elseif (strlen($var) > self::$maxLength) {
$var = substr($var, 0, self::$maxLength) . '...';
}
return preg_match('#[^\\x09\\x0A\\x0D\\x20-\\x7E\\xA0-\\x{10FFFF}]#u', $var) || preg_last_error() ? '"' . strtr($var, $table) . '"' : "'{$var}'";
} elseif (is_array($var)) {
$out = '';
$counter = 0;
foreach ($var as $k => &$v) {
$out .= $out === '' ? '' : ', ';
if (strlen($out) > self::$maxLength) {
$out .= '...';
break;
}
$out .= ($k === $counter ? '' : self::toLine($k) . ' => ') . (is_array($v) && $v ? 'array(...)' : self::toLine($v));
$counter = is_int($k) ? max($k + 1, $counter) : $counter;
}
return "array({$out})";
} elseif ($var instanceof \Exception || $var instanceof \Throwable) {
return 'Exception ' . get_class($var) . ': ' . ($var->getCode() ? '#' . $var->getCode() . ' ' : '') . $var->getMessage();
} elseif (is_object($var)) {
return self::objectToLine($var);
} elseif (is_resource($var)) {
return 'resource(' . get_resource_type($var) . ')';
} else {
return 'unknown type';
}
}
示例5: __toString
function __toString()
{
$sql = "";
foreach ($this as $column => $value) {
if (isset($this->{$column})) {
$sql = ($sql ? "{$sql}, " : "") . "{$column}=" . (is_double($value) && !is_finite($value) ? "NULL" : "'" . (self::$db ? self::$db->escape($value) : $value) . "'");
}
}
return $sql;
}
示例6: XNPV
private function XNPV($rate, $values, $dates)
{
if (!is_array($values) || !is_array($dates)) {
return null;
}
if (count($values) != count($dates)) {
return null;
}
$xnpv = 0.0;
for ($i = 0; $i < count($values); $i++) {
$xnpv += $values[$i] / pow(1 + $rate, $this->DATEDIFF('day', $dates[0], $dates[$i]) / 365);
}
return is_finite($xnpv) ? $xnpv : null;
}
示例7: toDouble
/**
* 与えられた値を、NAN、INFを含まない浮動小数点型に変換して返します。
* @link https://heycam.github.io/webidl/#idl-double Web IDL (Second Edition)
* @link http://www.w3.org/TR/WebIDL/#idl-double Web IDL
* @param boolean|integer|float|string|resource|\GMP|\SplFloat $value
* @throws \InvalidArgumentException 配列、NULL が与えられた場合。または、GMP、SplFloat 以外のオブジェクトが与えられた場合。
* @throws \DomainException 変換後の値が、NAN、INF、-INF のいずれかになった場合。
* @return float
*/
public static function toDouble($value)
{
$expectedType = 'double (a float not NAN or INF)';
try {
$float = self::toUnrestrictedDouble($value);
} catch (\InvalidArgumentException $exeception) {
throw new \InvalidArgumentException(ErrorMessageCreator::create($value, $expectedType));
}
if (is_finite($float)) {
return $float;
} else {
throw new \DomainException(ErrorMessageCreator::create($value, $expectedType));
}
}
示例8: execute
public function execute()
{
$src = FileBackendGroup::singleton()->get($this->getOption('src'));
$dst = FileBackendGroup::singleton()->get($this->getOption('dst'));
$posDir = $this->getOption('posdir');
$posFile = $posDir ? $posDir . '/' . wfWikiID() : false;
$start = $this->getOption('start', 0);
if (!$start && $posFile && is_dir($posDir)) {
$start = is_file($posFile) ? (int) trim(file_get_contents($posFile)) : 0;
++$start;
// we already did this ID, start with the next one
$startFromPosFile = true;
} else {
$startFromPosFile = false;
}
$end = $this->getOption('end', INF);
$this->output("Synchronizing backend '{$dst->getName()}' to '{$src->getName()}'...\n");
$this->output("Starting journal position is {$start}.\n");
if (is_finite($end)) {
$this->output("Ending journal position is {$end}.\n");
}
// Actually sync the dest backend with the reference backend
$lastOKPos = $this->syncBackends($src, $dst, $start, $end);
// Update the sync position file
if ($startFromPosFile && $lastOKPos >= $start) {
// successfully advanced
if (file_put_contents($posFile, $lastOKPos, LOCK_EX) !== false) {
$this->output("Updated journal position file.\n");
} else {
$this->output("Could not update journal position file.\n");
}
}
if ($lastOKPos === false) {
if (!$start) {
$this->output("No journal entries found.\n");
} else {
$this->output("No new journal entries found.\n");
}
} else {
$this->output("Stopped synchronization at journal position {$lastOKPos}.\n");
}
if ($this->isQuiet()) {
print $lastOKPos;
// give a single machine-readable number
}
}
示例9: convertBeforeEncode
static function convertBeforeEncode($val)
{
$arr = null;
if (is_object($val)) {
$_g = get_class($val);
switch ($_g) {
case "_hx_anonymous":
case "stdClass":
$arr = php_Lib::associativeArrayOfObject($val);
break;
case "_hx_array":
$arr = php_Lib::toPhpArray($val);
break;
case "Date":
return Std::string($val);
break;
case "HList":
$arr = php_Lib::toPhpArray(Lambda::harray($val));
break;
case "_hx_enum":
$e = $val;
return $e->index;
break;
case "StringMap":
case "IntMap":
$arr = php_Lib::associativeArrayOfHash($val);
break;
default:
$arr = php_Lib::associativeArrayOfObject($val);
break;
}
} else {
if (is_array($val)) {
$arr = $val;
} else {
if (is_float($val) && !is_finite($val)) {
$val = null;
}
return $val;
}
}
return array_map(isset(haxe_Json::$convertBeforeEncode) ? haxe_Json::$convertBeforeEncode : array("haxe_Json", "convertBeforeEncode"), $arr);
}
示例10: factorization
/**
* Returns the prime factors of a number.
* More info (http://bateru.com/news/2012/05/code-of-the-day-javascript-prime-factors-of-a-number/)
* Taken from Ratio.js
*
* @param number $number
* @return array an array of numbers
* @example prime.factorization(20).join(',') === "2,2,5"
**/
public static function factorization($number = 0)
{
$number = floor($number);
$continue = 1 < $number && is_finite($number);
$factors = array();
while ($continue) {
$sqrt = sqrt($number);
$x = 2;
if (fmod($number, $x) != 0) {
$x = 3;
while (fmod($number, $x) != 0 && ($x += 2) < $sqrt) {
}
}
$x = $sqrt < $x ? $number : $x;
$factors[] = $x;
$continue = $x != $number;
$number /= $x;
}
return $factors;
}
示例11: parseDMS
/**
* Parses string representing degrees/minutes/seconds into numeric degrees.
*
* This is very flexible on formats, allowing signed decimal degrees, or deg-min-sec optionally
* suffixed by compass direction (NSEW). A variety of separators are accepted (eg 3° 37′ 09″W).
* Seconds and minutes may be omitted.
*
* @param string|number $dmsStr - Degrees or deg/min/sec in variety of formats.
* @returns number Degrees as decimal number.
*/
public static function parseDMS($dmsStr)
{
// check for signed decimal degrees without NSEW, if so return it directly
if (is_numeric($dmsStr) && is_finite($dmsStr)) {
return (double) $dmsStr;
}
// strip off any sign or compass dir'n & split out separate d/m/s
$dms = preg_split('/[^0-9.,]+/', preg_replace(['/^-/', '/[NSEW]$/i'], '', trim((string) $dmsStr)));
if ($dms[count($dms) - 1] == '') {
array_splice($dms, count($dms) - 1);
}
// from trailing symbol
if ($dms == '') {
return NAN;
}
// and convert to decimal degrees...
switch (count($dms)) {
case 3:
// interpret 3-part result as d/m/s
$deg = $dms[0] / 1 + $dms[1] / 60 + $dms[2] / 3600;
break;
case 2:
// interpret 2-part result as d/m
$deg = $dms[0] / 1 + $dms[1] / 60;
break;
case 1:
// just d (possibly decimal) or non-separated dddmmss
$deg = $dms[0];
// check for fixed-width unseparated format eg 0033709W
//if (/[NS]/i.test(dmsStr)) deg = '0' + deg; // - normalise N/S to 3-digit degrees
//if (/[0-9]{7}/.test(deg)) deg = deg.slice(0,3)/1 + deg.slice(3,5)/60 + deg.slice(5)/3600;
break;
default:
return NAN;
}
if (preg_match('/^-|[WS]$/i', trim($dmsStr))) {
$deg = -$deg;
}
// take '-', west and south as -ve
return (double) $deg;
}
示例12: whatIs
public static function whatIs($what)
{
$type = gettype($what);
switch ($type) {
case "double":
case "integer":
if (is_finite($what)) {
return $what % 1 === 0 ? "integer" : "number";
}
if (is_nan($what)) {
return "not-a-number";
}
return "unknown-number";
case "NULL":
return "null";
case "boolean":
case "object":
case "array":
case "string":
return $type;
default:
return "unknown-type";
}
}
示例13: doCast
/**
* Cast an value to a specific type.
*
* Public calls should be made to "cast" which then delegates to this method.
*
* @param mixed $value Value to be casted and returned by reference.
* @param mixed $defaultValue Value to be returned if not set.
*
* @return mixed New value (or default value if unmatched).
* @throws \TypeError If defaultValue is of invalid type.
*/
public function doCast($value, $defaultValue = null)
{
if (false === is_float($defaultValue) && null !== $defaultValue) {
throw new \TypeError('DefaultValue must be a float for AsFloat casts');
}
if (false === is_string($value) && false === is_numeric($value)) {
$this->setError(self::ONLY_STRINGS_NUMERICS);
return $defaultValue;
}
if (false === is_float($value)) {
$options = ['options' => ['default' => null, 'decimal' => $this->decimalSeparator]];
// remove thousands separator
$value = str_replace($this->digitsSeparator, '', strval($value));
$value = filter_var($value, FILTER_VALIDATE_FLOAT, $options);
}
if (false === is_float($value) || true === is_nan($value) || false === is_finite($value)) {
$this->setError(self::FLOAT_MUST_BE_ACCEPTED_FORMAT, ['decimalSeparator' => $this->decimalSeparator, 'digitsSeparator' => $this->digitsSeparator]);
return $defaultValue;
}
if (null !== $this->precision) {
$value = round($value, $this->precision);
}
return $value;
}
示例14: toJson
/**
* @return mixed
*/
private static function toJson(&$var, $options, $level = 0)
{
if (is_bool($var) || is_null($var) || is_int($var)) {
return $var;
} elseif (is_float($var)) {
return is_finite($var) ? strpos($tmp = json_encode($var), '.') ? $var : array('number' => "{$tmp}.0") : array('type' => (string) $var);
} elseif (is_string($var)) {
return self::encodeString($var, $options[self::TRUNCATE]);
} elseif (is_array($var)) {
static $marker;
if ($marker === NULL) {
$marker = uniqid("", TRUE);
}
if (isset($var[$marker]) || $level >= $options[self::DEPTH]) {
return array(NULL);
}
$res = array();
$var[$marker] = TRUE;
foreach ($var as $k => &$v) {
if ($k !== $marker) {
$k = preg_match('#^\\w{1,50}\\z#', $k) ? $k : '"' . self::encodeString($k, $options[self::TRUNCATE]) . '"';
$res[] = array($k, self::toJson($v, $options, $level + 1));
}
}
unset($var[$marker]);
return $res;
} elseif (is_object($var)) {
$obj =& self::$liveStorage[spl_object_hash($var)];
if ($obj && $obj['level'] <= $level) {
return array('object' => $obj['id']);
}
if ($options[self::LOCATION] & self::LOCATION_CLASS) {
$rc = $var instanceof \Closure ? new \ReflectionFunction($var) : new \ReflectionClass($var);
$editor = Helpers::editorUri($rc->getFileName(), $rc->getStartLine());
}
static $counter = 1;
$obj = $obj ?: array('id' => self::$livePrefix . '0' . $counter++, 'name' => Helpers::getClass($var), 'editor' => empty($editor) ? NULL : array('file' => $rc->getFileName(), 'line' => $rc->getStartLine(), 'url' => $editor), 'level' => $level, 'object' => $var);
if ($level < $options[self::DEPTH] || !$options[self::DEPTH]) {
$obj['level'] = $level;
$obj['items'] = array();
foreach (self::exportObject($var, $options[self::OBJECT_EXPORTERS]) as $k => $v) {
$vis = 0;
if (isset($k[0]) && $k[0] === "") {
$vis = $k[1] === '*' ? 1 : 2;
$k = substr($k, strrpos($k, "") + 1);
}
$k = preg_match('#^\\w{1,50}\\z#', $k) ? $k : '"' . self::encodeString($k, $options[self::TRUNCATE]) . '"';
$obj['items'][] = array($k, self::toJson($v, $options, $level + 1), $vis);
}
}
return array('object' => $obj['id']);
} elseif (is_resource($var)) {
$obj =& self::$liveStorage[(string) $var];
if (!$obj) {
$type = get_resource_type($var);
$obj = array('id' => self::$livePrefix . (int) $var, 'name' => $type . ' resource');
if (isset(self::$resources[$type])) {
foreach (call_user_func(self::$resources[$type], $var) as $k => $v) {
$obj['items'][] = array($k, self::toJson($v, $options, $level + 1));
}
}
}
return array('resource' => $obj['id']);
} else {
return array('type' => 'unknown type');
}
}
示例15: newPagerForSavedQuery
public function newPagerForSavedQuery(PhabricatorSavedQuery $saved)
{
if ($this->shouldUseOffsetPaging()) {
$pager = new PHUIPagerView();
} else {
$pager = new AphrontCursorPagerView();
}
$page_size = $this->getPageSize($saved);
if (is_finite($page_size)) {
$pager->setPageSize($page_size);
} else {
// Consider an INF pagesize to mean a large finite pagesize.
// TODO: It would be nice to handle this more gracefully, but math
// with INF seems to vary across PHP versions, systems, and runtimes.
$pager->setPageSize(0xffff);
}
return $pager;
}