博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP 浮点数比较
阅读量:7044 次
发布时间:2019-06-28

本文共 972 字,大约阅读时间需要 3 分钟。

hot3.png

浮点数由于精度问题,不能直接进行比较。

$a = 100.411;$b = 100;$c = 0.411;$d = $a - $b;var_dump($c == $d);  // false

PHP提供了BC函数,BC Math

For arbitrary precision mathematics PHP offers the Binary Calculator which supports numbers of any size and precision, represented as strings.

其中bccomp用来比较两个任意精度的数值:

$a = 100.411;$b = 100;$c = 0.411;$d = $a - $b;var_dump(bccomp($c, $d, 3));  // 0 表示相等

Description
int bccomp ( string $left_operand = "" , string $right_operand = "" [, int $scale = int ] )

Compares the left_operand to the right_operand and returns the result as an integer.

Parameters

left_operand

The left operand, as a string.

right_operand

The right operand, as a string.

scale

The optional scale parameter is used to set the number of digits after the decimal place which will be used in the comparison. Default value 0.

Return Values
  • 0 if the two operands are equal,
  • 1 if the left_operand is larger than the right_operand,
  • -1 otherwise.

转载于:https://my.oschina.net/gilbertch/blog/490515

你可能感兴趣的文章
英特尔马子雅:深度学习四大痛点与BigDL解决之道
查看>>
Linux基础命令介绍八:文本分析awk
查看>>
浅谈数据中心高可用网络系统设计
查看>>
提高 Java 代码性能的各种技巧
查看>>
大数据和警察
查看>>
PostgreSQL服务器管理:从源代码安装
查看>>
英国交通运输业面临网络安全威胁 投入资源加强防范
查看>>
施耐德电气:打造新一代数据中心基础设施
查看>>
朱共山:光伏重组不搞拉郞配 市场唱主角
查看>>
乐天中国官网疑遭黑客攻击瘫痪,有技术人员呼吁继续声讨
查看>>
投融界成功案例告诉你:太阳能光伏发电项目该如何吸引资本加入
查看>>
《MongoDB管理与开发精要》——3.4节游标和存储过程
查看>>
云计算的小兄弟:雾计算和霾计算
查看>>
Xerafy携手VUEMED,扩大了医疗供应链技术解决方案
查看>>
后监管时代P2P生存路径:大数据风控能力是核心
查看>>
美国将再建两个海上风力发电场
查看>>
新ICT时代来了,有没有感觉到不一样?
查看>>
全球物联网并购投资趋热 电信运营商如何看清风向?
查看>>
土地紧张使香港数据中心市场增长乏力
查看>>
《贵州省大数据发展报告(2016)》白皮书发布
查看>>