Connecting Tech Pros Worldwide Help | Site Map

echo (int) (100 * (7.77 - 7 )); Why 76 ?

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 18th, 2008, 06:55 PM
Gert Kok
Guest
 
Posts: n/a
Default echo (int) (100 * (7.77 - 7 )); Why 76 ?

echo (100 * (7.77 - 7 )); --77
echo (int) (100 * (7.77 - 7 )); --76

Why the difference?

  #2  
Old March 18th, 2008, 07:35 PM
PaulB
Guest
 
Posts: n/a
Default Re: echo (int) (100 * (7.77 - 7 )); Why 76 ?

Gert Kok wrote:
Quote:
echo (100 * (7.77 - 7 )); --77
echo (int) (100 * (7.77 - 7 )); --76
>
Why the difference?
http://uk3.php.net/manual/en/language.types.float.php

Read the warning.

Paul

--
Add an underscore after the p to reply


  #3  
Old March 18th, 2008, 09:05 PM
C. (http://symcbean.blogspot.com/)
Guest
 
Posts: n/a
Default Re: echo (int) (100 * (7.77 - 7 )); Why 76 ?

On 18 Mar, 19:30, "PaulB" <pb...@ntlworld.comwrote:
Quote:
Gert Kok wrote:
Quote:
echo (100 * (7.77 - 7 )); --77
echo (int) (100 * (7.77 - 7 )); --76
>
Quote:
Why the difference?
>
http://uk3.php.net/manual/en/language.types.float.php
>
Read the warning.
>
This is not a PHP thing - most computer languages are like this except
for ones which deal with maths rather than arithmetic.

C.
  #4  
Old March 19th, 2008, 06:45 PM
sheldonlg
Guest
 
Posts: n/a
Default Re: echo (int) (100 * (7.77 - 7 )); Why 76 ?

Gert Kok wrote:
Quote:
echo (100 * (7.77 - 7 )); --77
echo (int) (100 * (7.77 - 7 )); --76
>
Why the difference?
int(76.9999999999999 ) ==76 (truncation)

while
76.9999999999999 shown with no decimal places is rounded to 77

This is the same as in C, C++, Java, Fortran, Basic, etc. etc. etc.

Basically, there is no way to EXACTLY store a repeating decimal in 32
bits. Floating numbers (decimal points) are approximations. The casting
with (int) truncates this approximation.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.