doujing2497 2015-07-30 14:45
浏览 9
已采纳

试图理解这种代码行为

Today i was working on a PHP project and came across this code behaviour

<?php
$x = 5.5;
$y = 0;
echo $z = floor($x * $y * -1);
?>

This gave the output of -0 . Can anyone shed light on why this echoes -0 . But I expected 0 Only when adding floor this appears to happen. I tried the same in java.

class Sample {
    public static void main(String[] args) {
        float x =5.5f;
        int y = 0;
        System.out.println(Math.floor(x*y*-1));
    }
}

This also prints -0.0 .

  • 写回答

2条回答 默认 最新

  • douyi1341 2015-07-30 14:54
    关注

    float and double have both a positive 0 and a negative 0. When you multiple 0 * -1 you get -0 as specified in the IEEE 754 standard.

    Note: 1/0 is positive infinity but 1/-0 is negative infinity.

    You can see that http://ideone.com/tBd41l

    System.out.println(0f * -1);
    

    prints

    -0.0
    

    Math.floor is not required.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效