c++ float and integer

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

    #1

    c++ float and integer

    Why when I multiply a float , for example X10, and than convert it as
    an integer the new value is changed?

    Example:

    float fl = 1.8
    int x;

    x = int(fl * 10);

    x = 17 <<<<<<<< I think this is not correct :-)

    this happen a lot of time in my code with other float numbers.

    is this a bug of VisualC6 or I need a solution?

  • TB

    #2
    Re: c++ float and integer

    Geky sade:[color=blue]
    > Why when I multiply a float , for example X10, and than convert it as
    > an integer the new value is changed?
    >
    > Example:
    >
    > float fl = 1.8
    > int x;
    >
    > x = int(fl * 10);
    >
    > x = 17 <<<<<<<< I think this is not correct :-)
    >
    > this happen a lot of time in my code with other float numbers.
    >
    > is this a bug of VisualC6 or I need a solution?
    >[/color]

    Most likely because (1.8f * 10) can't be perfectly represented.
    Internally it might be 17.9999999999 which when you truncate it
    to an integer becomes 17.

    --
    TB @ SWEDEN

    Comment

    • Victor Bazarov

      #3
      Re: c++ float and integer

      Geky wrote:[color=blue]
      > Why when I multiply a float[..][/color]

      Please see my answer to the same post of yours in the newsgroup
      'microsoft.publ ic.vc.language' .

      V

      Comment

      • Shark

        #4
        Re: c++ float and integer

        Geky wrote:[color=blue]
        > Why when I multiply a float , for example X10, and than convert it as
        > an integer the new value is changed?[/color]

        Apart from being a VC question (which you should ask in a relevant
        group) consider what happens when you convert from a float to an int. A
        float can be represented in many ways, and generally the convention is
        to use IEEE754-1985 standard.

        Lets say you have a number 32 bits long, and you try jamming those 32
        bits into 8 bits. What happens? 24 bits are left out!!!! So 3/4 times
        information about your float number is lost! Makes sense? No wonder you
        are getting a wrong answer.
        [color=blue]
        >
        > Example:
        >
        > float fl = 1.8
        > int x;
        >
        > x = int(fl * 10);
        >
        > x = 17 <<<<<<<< I think this is not correct :-)
        >
        > this happen a lot of time in my code with other float numbers.
        >
        > is this a bug of VisualC6 or I need a solution?[/color]

        Comment

        • Mike Wahler

          #5
          Re: c++ float and integer


          "Geky" <attackack@yaho o.com> wrote in message
          news:1137438599 .417562.35500@o 13g2000cwo.goog legroups.com...[color=blue]
          > Why when I multiply a float , for example X10, and than convert it as
          > an integer the new value is changed?
          >
          > Example:
          >
          > float fl = 1.8
          > int x;
          >
          > x = int(fl * 10);[/color]

          std::cout << std::fixed << fl << '\n';
          std::cout << std::fixed << fl * 10 << '\n';

          [color=blue]
          >
          > x = 17 <<<<<<<< I think this is not correct :-)
          >
          > this happen a lot of time in my code with other float numbers.
          >
          > is this a bug of VisualC6[/color]

          No.
          [color=blue]
          > or I need a solution?[/color]



          -Mike


          Comment

          Working...