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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.