本文整理汇总了PHP中float::asRational方法的典型用法代码示例。如果您正苦于以下问题:PHP float::asRational方法的具体用法?PHP float::asRational怎么用?PHP float::asRational使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类float
的用法示例。
在下文中一共展示了float::asRational方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: convertNumberToRational
/**
* Convert if possible a supplied argument to a rational
*
* @param int|float|string|NumericTypeInterface $value
*
* @return \Chippyash\Math\Matrix\RationalNumber
*
* @throws \Chippyash\Matrix\Exceptions\MatrixException
* @throws \Exception
*/
protected function convertNumberToRational($value)
{
if ($value instanceof NumericTypeInterface) {
return $value->asRational();
}
switch (gettype($value)) {
case 'integer':
return RationalTypeFactory::create($value, 1);
case 'double':
return RationalTypeFactory::fromFloat($value);
case 'string':
try {
return RationalTypeFactory::fromString($value);
} catch (\Exception $e) {
try {
return ComplexTypeFactory::fromString($value)->asRational();
} catch (\Exception $ex) {
throw new MatrixException('The string representation of the number is invalid for a rational');
}
}
case 'NULL':
return RationalTypeFactory::create(0, 1);
case 'boolean':
return RationalTypeFactory::create($value ? 1 : 0, 1);
default:
throw new MatrixException('Rational expects int, float, string, Rational or NumericTypeInterface ');
}
}