PHP对我的程序很奇怪 [英] PHP acting weird on my program

查看:127
本文介绍了PHP对我的程序很奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我开始写一个小的PHP代码,它让我困惑,所以我停在这里。

Today i started writing a small PHP code and it left me confusing and so i parked here.

<?php
echo (int) ((0.5 + 0.3) * 10); // Outputs 8 as expected

<?php
echo (int) ((0.1 + 0.7) * 10); // Outputs 7 . How ????

有人可以详细解释吗?

推荐答案

这是因为计算机中的浮点表示对于某些数字不够精确。正如已经在评论中所说, 0.7 在内部表示为 0.699999 等。

It is because floating point representation in computers is not exact for some numbers. As already said in the comments, 0.7 is represented internally as 0.699999 or so.

有两个网站在这类问题中不断出现:

There are two websites that continuously pop up in these kind of questions:


  1. http://floating-point-gui.de/

  2. http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg .html

  1. http://floating-point-gui.de/
  2. http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

我更喜欢第一个,因为它在学术上有点轻。阅读这些信息,你会明白。

I prefer the first one, as it is a little lighter on the academics. Read that information and you'll understand.

这篇关于PHP对我的程序很奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆