当前位置:Gxlcms > PHP教程 > 新手学php的疑问(周末结贴)

新手学php的疑问(周末结贴)

时间:2021-07-01 10:21:17 帮助过:30人阅读

echo (int) ( (0.1+0.7) * 10 ); // 显示 7
echo (int) ( (0.2+0.7) * 10 ); // 显示 9
?>
第一条的执行结果为什么不是8


回复讨论(解决方案)

浮点数的精度问题

 printf('%0.16f',  (0.1+0.7) * 10 ); // 7.9999999999999991




浮点数的精度问题

 printf('%0.16f',  (0.1+0.7) * 10 ); // 7.9999999999999991



没加int 它就是8
echo ( (0.1+0.7) * 10 ); // 显示 8

echo (int) ( (0.1+0.7) * 10 ); // 显示 7
我的理解是:将右括号内的结果强制转化为int类型 即 echo(int)8;

那是你理解错了
(int) 强制转换成整数,此时只将小数部分截掉,并无其他处理
echo (int)2.2; //2
echo (int)2.9; //2
同理
echo (int)7.9999999999999991; //7

echo (0.1+0.7) * 10; //8
是因为 php 按有效数规则做了处理

浮??精度??。
http://segmentfault.com/q/1010000002123613

问题1:已解决
问题2:php如何调试项目?

打开错误日志。

??建?安?xdebug

http://www.cnblogs.com/qiantuwuliang/archive/2011/01/23/1942382.html

class A{    function foo()    {        if (isset($this)) {                                //这里的$this指的是类的对象吗?            echo '$this is defined (';            echo get_class($this);            echo ")\n";        } else {            echo "\$this is not defined.\n";        }    }}$a = new A();$a->foo();     //
输出$this is defined (A) echo '
';A::foo(); //$this is not defined

A::foo()跟$a->foo() 调用的方法都一样,为什么输出来却不一样?

$this 是实例化后的对象

A::foo(); 是以静态方式调用类的方法
php 5.3 以后将会有一个 Strict Standards 级别的错误警告:
Non-static method A::foo() should not be called statically
不能以静态方式调用非静态方法

既然你是初学,建议使用的 php 版本为 5.4 及以上,错误检查级别为全部
这样比较容易形成良好的习惯

$this 是实例化后的对象

A::foo(); 是以静态方式调用类的方法
php 5.3 以后将会有一个 Strict Standards 级别的错误警告:
Non-static method A::foo() should not be called statically
不能以静态方式调用非静态方法

既然你是初学,建议使用的 php 版本为 5.4 及以上,错误检查级别为全部
这样比较容易形成良好的习惯

在赶集网投简历的时候,怎么判断那家公司是不是骗人的?

在回帖的时候,怎么判断楼主是不是骗人的?

人气教程排行