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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gert Kok

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

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

    Why the difference?
  • PaulB

    #2
    Re: echo (int) (100 * (7.77 - 7 )); Why 76 ?

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


    Read the warning.

    Paul

    --
    Add an underscore after the p to reply


    Comment

    • C. (http://symcbean.blogspot.com/)

      #3
      Re: echo (int) (100 * (7.77 - 7 )); Why 76 ?

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

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

      Comment

      • sheldonlg

        #4
        Re: echo (int) (100 * (7.77 - 7 )); Why 76 ?

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

        while
        76.999999999999 9 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.

        Comment

        Working...