首页 > 语言 > PHP > 正文

PHP高精确度运算BC函数库实例详解

2024-09-04 11:49:26
字体:大 中 小
来源:转载
供稿:网友
  1. <?php 
  2.  /*************************************************************************************** 
  3.  *php BC高精确度函数库 
  4.  *php bc math 包含了:相加,比较,相除,相减,求余,相乘,n次方,配置默认小数点数目,求平方 
  5.  *这些函数在涉及到有关金钱的计算时比较有用 
  6.  *************************************************************************************** 
  7.  *两个高精度数比较 
  8.  *工作中遇到一种情况,0.00 != 0 
  9.  *int bccomp ( string $left_operand , string $right_operand [, int $scale ] ) 
  10.  *$left=$right 返回 0 
  11.  *$left<$right 返回 -1 
  12.  *$left>$right 返回 1 
  13.  *$scale 小数点位数 
  14.  ***************************************************************************************/ 
  15. $a = 4.45; 
  16. $b = 5.54; 
  17. if(bccomp($a, $b, 2) == 0) 
  18. { 
  19. } 
  20. /*************************************************************************************** 
  21.  *两个高精度数相加 
  22.  *string bcadd ( string $left_operand , string $right_operand [, int $scale ] ) 
  23.  *$scale 返回的小数点个数 
  24.  ***************************************************************************************/ 
  25. $a = 1.0321456; 
  26. $b = 0.0123456; 
  27. $c = bcadd($a, $b, 2); 
  28. //var_dump($c); 
  29. /*************************************************************************************** 
  30.  *两个高精度数相减 
  31.  *sstring bcsub ( string $left_operand , string $right_operand [, int $scale ] ) 
  32.  *$scale 返回的小数点个数 
  33.  ***************************************************************************************/ 
  34. $a = 1.0321456; 
  35. $b = 3.0123456; 
  36. $c = bcsub($a, $b, 2); 
  37. var_dump($c); 
  38. /******************************************************** 
  39.  *两个高精度数求余/取模 
  40.  *string bcmod ( string $left_operand , string $modulus ) 
  41.  *******************************************************/ 
  42. $a = 6; 
  43. $b = 4; 
  44. $c = bcmod($a, $b); 
  45. //var_dump($c); 
  46. /*************************************************************************************** 
  47.  *两个高精度数相除 
  48.  *string bcdiv ( string $left_operand , string $right_operand [, int $scale ] ) 
  49.  *$scale小数点位数默认为 0 
  50.  ***************************************************************************************/ 
  51.  $a = 6; 
  52.  $b = 5; 
  53.  $c = bcdiv($a, $b, 3); 
  54.  //var_dump($c); 
  55. /*************************************************************************************** 
  56.  *两个高精度数相乘 
  57.  *string bcmul ( string $left_operand , string $right_operand [, int $scale ] ) 
  58.  *$scale小数点位数默认为 0 
  59.  ***************************************************************************************/ 
  60. $a = 3.1415926; 
  61. $b = 2.4569874566; 
  62. $c = bcmul($a, $b, 6); 
  63. //var_dump($c); 
  64. /*************************************************************************************** 
  65.  *两个高精度数的次方值 
  66.  *string bcpow ( string $left_operand , string $right_operand [, int $scale ] ) 
  67.  *$scale小数点位数默认为 0 
  68.  ***************************************************************************************/ 
  69. $a = 3.1415926; 
  70. $b = 2; 
  71. $c = bcpow($a, $b, 3); 
  72. //var_dump($c); 
  73. /************************************************** 
  74.  *求高精度数的平方根 
  75.  *string bcsqrt ( string $operand [, int $scale ] ) 
  76.  *$scale小数点位数默认为 0 
  77.  ***************************************************/ 
  78. $b = bcsqrt($a, 6); 
  79. //var_dump($b); 
  80. /****************************** 
  81.  *设置bc函数的小数点位数 
  82.  *bool bcscale ( int $scale ) 
  83.  *$scale小数点位数默认为 0 
  84.  ******************************/ 
  85. //bcscale(); 
  86. ?> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表