Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

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

Question posted by: Gert Kok (Guest) on March 18th, 2008 06:55 PM
echo (100 * (7.77 - 7 )); --77
echo (int) (100 * (7.77 - 7 )); --76

Why the difference?
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
PaulB's Avatar
PaulB
Guest
n/a Posts
March 18th, 2008
07:35 PM
#2

Re: echo (int) (100 * (7.77 - 7 )); Why 76 ?
Gert Kok wrote:
Quote:
Originally Posted by
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



C. (http://symcbean.blogspot.com/)'s Avatar
C. (http://symcbean.blogspot.com/)
Guest
n/a Posts
March 18th, 2008
09:05 PM
#3

Re: echo (int) (100 * (7.77 - 7 )); Why 76 ?
On 18 Mar, 19:30, "PaulB" <pb...@ntlworld.comwrote:
Quote:
Originally Posted by
Gert Kok wrote:
Quote:
Originally Posted by
echo (100 * (7.77 - 7 )); --77
echo (int) (100 * (7.77 - 7 )); --76

>
Quote:
Originally Posted by
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.

sheldonlg's Avatar
sheldonlg
Guest
n/a Posts
March 19th, 2008
06:45 PM
#4

Re: echo (int) (100 * (7.77 - 7 )); Why 76 ?
Gert Kok wrote:
Quote:
Originally Posted by
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.

 
Not the answer you were looking for? Post your question . . .
182,081 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors