Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Interesting - could someone try this with VS2010?

Interesting - could someone try this with VS2010?

Scheduled Pinned Locked Moved The Lounge
questioncsharpcomtutorial
9 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #1

    This isn't a programming question, it's a "I don't know where else to put it" question. I was answering a Q&A question http://www.codeproject.com/Questions/133921/float-number-loosing-percision.aspx[^] So, I gave it a try in C#:

    string s = "30.15";
    float f = float.Parse(s);
    int i = (int) (f * 100);
    float g = f * 100;
    int j = (int) (g);
    MessageBox.Show(i.ToString() + ":" + j.ToString());

    Gives the message box

    3014:3015

    Which means that creating a local variable gives a different result... Could someone try this in VS2010 and see if it still occurs? Because I wouldn't expect that...

    Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    S L D M 4 Replies Last reply
    0
    • OriginalGriffO OriginalGriff

      This isn't a programming question, it's a "I don't know where else to put it" question. I was answering a Q&A question http://www.codeproject.com/Questions/133921/float-number-loosing-percision.aspx[^] So, I gave it a try in C#:

      string s = "30.15";
      float f = float.Parse(s);
      int i = (int) (f * 100);
      float g = f * 100;
      int j = (int) (g);
      MessageBox.Show(i.ToString() + ":" + j.ToString());

      Gives the message box

      3014:3015

      Which means that creating a local variable gives a different result... Could someone try this in VS2010 and see if it still occurs? Because I wouldn't expect that...

      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

      S Offline
      S Offline
      Sathesh Sakthivel
      wrote on last edited by
      #2

      The same for me too.

      Regards, Sathesh. The best way to express one's gratitude to the Divine is to feel simply Happy..

      1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        This isn't a programming question, it's a "I don't know where else to put it" question. I was answering a Q&A question http://www.codeproject.com/Questions/133921/float-number-loosing-percision.aspx[^] So, I gave it a try in C#:

        string s = "30.15";
        float f = float.Parse(s);
        int i = (int) (f * 100);
        float g = f * 100;
        int j = (int) (g);
        MessageBox.Show(i.ToString() + ":" + j.ToString());

        Gives the message box

        3014:3015

        Which means that creating a local variable gives a different result... Could someone try this in VS2010 and see if it still occurs? Because I wouldn't expect that...

        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Not in 64bit mode edit: and not with optimizations enabled. Ok, looking at the disassembly. Likely cause: the FPU uses 80bit floats, saving as anything other than that throws bits away.

        D 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          This isn't a programming question, it's a "I don't know where else to put it" question. I was answering a Q&A question http://www.codeproject.com/Questions/133921/float-number-loosing-percision.aspx[^] So, I gave it a try in C#:

          string s = "30.15";
          float f = float.Parse(s);
          int i = (int) (f * 100);
          float g = f * 100;
          int j = (int) (g);
          MessageBox.Show(i.ToString() + ":" + j.ToString());

          Gives the message box

          3014:3015

          Which means that creating a local variable gives a different result... Could someone try this in VS2010 and see if it still occurs? Because I wouldn't expect that...

          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

          D Offline
          D Offline
          Dan Mos
          wrote on last edited by
          #4

          I get 3014:3014 VS 2010 32 bit. At work I can not test the using a 64 bit OS/bulit cause there's just 32 bit boohoo here. :)

          I used to think.... Finally I realized it's no good.

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            This isn't a programming question, it's a "I don't know where else to put it" question. I was answering a Q&A question http://www.codeproject.com/Questions/133921/float-number-loosing-percision.aspx[^] So, I gave it a try in C#:

            string s = "30.15";
            float f = float.Parse(s);
            int i = (int) (f * 100);
            float g = f * 100;
            int j = (int) (g);
            MessageBox.Show(i.ToString() + ":" + j.ToString());

            Gives the message box

            3014:3015

            Which means that creating a local variable gives a different result... Could someone try this in VS2010 and see if it still occurs? Because I wouldn't expect that...

            Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

            M Offline
            M Offline
            moon_stick
            wrote on last edited by
            #5

            It's not that surprising - the actual value returned by float.Parse(s) is 30.149999(...). The problem occurs when you do the cast to int as this drops the fractional part of the number resulting in 3014. Floats are inherently an approximation of an actual number; I guess the correct process here would be to round the float before the cast to ensure that the whole value is accounted for. If better accuracy is required then you should be using double rather than float. Nice discussion about FPA here[^].

            Sarchasm : The gulf between the author of sarcastic wit and the person who doesn't get it.

            OriginalGriffO 1 Reply Last reply
            0
            • M moon_stick

              It's not that surprising - the actual value returned by float.Parse(s) is 30.149999(...). The problem occurs when you do the cast to int as this drops the fractional part of the number resulting in 3014. Floats are inherently an approximation of an actual number; I guess the correct process here would be to round the float before the cast to ensure that the whole value is accounted for. If better accuracy is required then you should be using double rather than float. Nice discussion about FPA here[^].

              Sarchasm : The gulf between the author of sarcastic wit and the person who doesn't get it.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Yes - that was what I was expecting. The surprise was that the addition of a local variable to store the intermediate value changed the results...

              Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              L M 2 Replies Last reply
              0
              • OriginalGriffO OriginalGriff

                Yes - that was what I was expecting. The surprise was that the addition of a local variable to store the intermediate value changed the results...

                Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                It's not surprising at all.

                1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  Yes - that was what I was expecting. The surprise was that the addition of a local variable to store the intermediate value changed the results...

                  Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

                  M Offline
                  M Offline
                  moon_stick
                  wrote on last edited by
                  #8

                  If you change your third line to: int i = (int)(float)(f * 100); then you get the correct result. I agree it seems like unusual behaviour but I think the processes between building i and j aren't quite syntactically equivalent...??

                  Sarchasm : The gulf between the author of sarcastic wit and the person who doesn't get it.

                  1 Reply Last reply
                  0
                  • L Lost User

                    Not in 64bit mode edit: and not with optimizations enabled. Ok, looking at the disassembly. Likely cause: the FPU uses 80bit floats, saving as anything other than that throws bits away.

                    D Offline
                    D Offline
                    Dan Neely
                    wrote on last edited by
                    #9

                    That's about what I'd've guessed, except that I'd've figured it was probably doing the math internally with a double instead of an 80bit float.

                    3x12=36 2x12=24 1x12=12 0x12=18

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups