Jump to content

Output Vary Problem


somepalli

Recommended Posts

(1)

 

<?phpecho (int) ((0.2+0.7)*10);?>                                                 

                       

The Output Of This code is : 9

 

(2)

 

<?phpecho (int) ((0.1+0.7)*10);?>                                                                   

         

The Output Of This code is : 7

 

In the (2) Program as per my calculation it has to come '8' but i am getting '7'

why?

 

[sorry for poor english]

[/]

Link to comment
https://forums.phpfreaks.com/topic/149776-output-vary-problem/
Share on other sites

The reason is due to floating point math and it's documented inaccuracies.

 

Multiply --- 0.2+0.7 --- each of those numbers by 10, add together, then divide by ten, and it'll work as expected.

 

The idea is convert the 0.2 and 0.7 to an integer first (2 and 7) add them, then convert back to floating point.

Link to comment
https://forums.phpfreaks.com/topic/149776-output-vary-problem/#findComment-786491
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.