PHP | gmp_root()函数

gmp_root()是PHP中的一个内置函数,它返回gmp数的第N个根的整数部分( 多精度 :对于大数字)。 语法:

null
gmp_root( $num, $n )

参数: 该函数接受两个必需参数$num和$n。

  1. $num –这是返回第n个根的整数部分的GMP编号。参数是PHP 5.6及更高版本中的GMP对象,或者我们也可以传递一个数字字符串,前提是可以将该字符串转换为数字。
  2. $n –数字的正n次方根。它是一个整数值。

返回值: 此函数返回一个正值 GMP 是$num的第N个根的整数部分。 例如:

Input : $num = "20" $n = 2Output : 4 Input : $num = "9" $n = 2Output : 2

下面的程序演示了gmp_root()函数: 项目1: 下面的程序演示了当gmp编号作为参数传递时gmp_root()函数的工作。。

php

<?php
// PHP program to calculate the
// integer part of N-th root of
// a GMP number
// GMP number as arguments
$num = gmp_init( "1001" , 2);
$n = 3;
// function calculates the pow raised to
// number modulo mod
//  integer part of cubic root of 9
$root = gmp_root( $num , $n );
// gmp_strval Convert GMP number to string
// representation in given base(default 10).
echo gmp_strval( $root , 2);
?>


输出:

10

项目2: 下面的程序演示了当数字字符串作为参数传递时,gmp_root()的工作原理。

php

<?php
// PHP program to calculate the
// integer part of N-th root of
// a GMP number
// GMP number as arguments
$num = "9" ;
$n = 3;
// function calculates the pow raised to
// number modulo mod
// integer part of cubic root of 9
$root = gmp_root( $num , $n );
echo $root ;
?>


输出:

2

方案3: 求一个数的平方根的整数部分的程序。

php

<?php
// PHP program to calculate the
// integer part of N-th root of
// a GMP number
// GMP number as arguments
$num = "25" ;
$n = 2;
// function calculates the pow raised to
// number modulo mod
// integer part of square root of 25
$root = gmp_root( $num , $n );
echo $root ;
?>


输出:

5

参考: http://php.net/manual/en/function.gmp-root.php

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享