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. General Programming
  3. C / C++ / MFC
  4. problem with conversion of string to float

problem with conversion of string to float

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
7 Posts 6 Posters 0 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.
  • M Offline
    M Offline
    materatsu
    wrote on last edited by
    #1

    I am currently having this problem using this:

    LPCSTR numStr = "128.40";

    float num = 0.0;
    wstringstream stream;
    stream<>num;

    i am getting 128.3999 in num, this happens in 128.3, 128.2, 128.1, and any number over 128 with decimal x.1,x.2,x.3,x.4, but does not happens in numbers under 128. I have just tested another numbers and this happens until 511.4 it gives me 511.3999 but with 512.1 it gives me 512.100001, after 512 all numbers give me that, the same behaviour maybe is repeated again later in series. Just have tested the function atof and _tstof and both fail. What do i am missing here, how to solve this issue??:confused:

    _ G B M 4 Replies Last reply
    0
    • M materatsu

      I am currently having this problem using this:

      LPCSTR numStr = "128.40";

      float num = 0.0;
      wstringstream stream;
      stream<>num;

      i am getting 128.3999 in num, this happens in 128.3, 128.2, 128.1, and any number over 128 with decimal x.1,x.2,x.3,x.4, but does not happens in numbers under 128. I have just tested another numbers and this happens until 511.4 it gives me 511.3999 but with 512.1 it gives me 512.100001, after 512 all numbers give me that, the same behaviour maybe is repeated again later in series. Just have tested the function atof and _tstof and both fail. What do i am missing here, how to solve this issue??:confused:

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      This is not an issue. It happens because of the way floating point numbers are stored in memory. Try this example by changing the value passed to setprecision -

      float num = 128.384624;
      cout << setiosflags(ios_base::fixed) << setprecision(2) << num << endl;

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++)

      Polymorphism in C

      1 Reply Last reply
      0
      • M materatsu

        I am currently having this problem using this:

        LPCSTR numStr = "128.40";

        float num = 0.0;
        wstringstream stream;
        stream<>num;

        i am getting 128.3999 in num, this happens in 128.3, 128.2, 128.1, and any number over 128 with decimal x.1,x.2,x.3,x.4, but does not happens in numbers under 128. I have just tested another numbers and this happens until 511.4 it gives me 511.3999 but with 512.1 it gives me 512.100001, after 512 all numbers give me that, the same behaviour maybe is repeated again later in series. Just have tested the function atof and _tstof and both fail. What do i am missing here, how to solve this issue??:confused:

        G Offline
        G Offline
        gebri
        wrote on last edited by
        #3

        use double type instead of float

        1 Reply Last reply
        0
        • M materatsu

          I am currently having this problem using this:

          LPCSTR numStr = "128.40";

          float num = 0.0;
          wstringstream stream;
          stream<>num;

          i am getting 128.3999 in num, this happens in 128.3, 128.2, 128.1, and any number over 128 with decimal x.1,x.2,x.3,x.4, but does not happens in numbers under 128. I have just tested another numbers and this happens until 511.4 it gives me 511.3999 but with 512.1 it gives me 512.100001, after 512 all numbers give me that, the same behaviour maybe is repeated again later in series. Just have tested the function atof and _tstof and both fail. What do i am missing here, how to solve this issue??:confused:

          B Offline
          B Offline
          Bram van Kampen
          wrote on last edited by
          #4

          Hi, You ran into precission and rounding problems. This is always a problem when dealing with Floating Point Arithmatic. The first question is , after setting

          stream<<setprecision(2);

          You probably typed in quite larger amounts, but I stay with your example, because it hits a fundamental problem, which is not very well understood. you get things like '511.3999' or '512.100001' I very seldomly use streams, but I know, your setprecission(2) would have printed these as '511.39' and '512.10' However, I can see where you are coming from. You typed in $511.40, and it appeared elsewhere as $511.39. So you started to investigate. Widened the Precission Spec, and noted that $512.10, was printed as: $ 512.100001! The Concept of Numbers, Rational Numbers, and Real Numbers is perfect, manipulation of these numbers is always perfect, under the rules of mathematics. At the same time, there is also an Infinite number of Numbers. The Human brain can work with Concepts, and conclude that the square of the square root of 2 equals 2. Computers cannot do that naturally,(Unless if they run an Artificial Inteligence Application designed to do those things). In general, a Computer can work with a Very Large (but Finite) list of Numbers, and the question is how do we encode numbers in real life. Very Large Numbers Very Precice, is very difficult, but then again Who wants to know the US National Budget to the nearest Cent. that's where floats come in. Floats sacrifice speed and storage space for accuracy. If you want to keep account of say funds. Store it in Cents or Fractions of Cents, but essentially as an Integer value. In other words, you Store it as Cents, but Show it as Dollars, by sprintf(Buf,"Value \t%c %d.%04d",'$',Val/100,Val%100); My 30 year adage is: Storing Money as a Float is nearly always a Daft Idea. Regards, :|

          Bram van Kampen

          1 Reply Last reply
          0
          • M materatsu

            I am currently having this problem using this:

            LPCSTR numStr = "128.40";

            float num = 0.0;
            wstringstream stream;
            stream<>num;

            i am getting 128.3999 in num, this happens in 128.3, 128.2, 128.1, and any number over 128 with decimal x.1,x.2,x.3,x.4, but does not happens in numbers under 128. I have just tested another numbers and this happens until 511.4 it gives me 511.3999 but with 512.1 it gives me 512.100001, after 512 all numbers give me that, the same behaviour maybe is repeated again later in series. Just have tested the function atof and _tstof and both fail. What do i am missing here, how to solve this issue??:confused:

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

            this even happens with the C runtime library!, i use atof and i got the same problems, maybe it is true that is the way the floats are saved in memory as stated before, i even tried double numbers and that even work.. saying this, someone can recommend me a good tutorial on floating numbers manipulation? Thanks

            D S 2 Replies Last reply
            0
            • M materatsu

              this even happens with the C runtime library!, i use atof and i got the same problems, maybe it is true that is the way the floats are saved in memory as stated before, i even tried double numbers and that even work.. saying this, someone can recommend me a good tutorial on floating numbers manipulation? Thanks

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              materatsu wrote:

              ...maybe it is true that is the way the floats are saved in memory as stated before...

              No maybe about it.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

              1 Reply Last reply
              0
              • M materatsu

                this even happens with the C runtime library!, i use atof and i got the same problems, maybe it is true that is the way the floats are saved in memory as stated before, i even tried double numbers and that even work.. saying this, someone can recommend me a good tutorial on floating numbers manipulation? Thanks

                S Offline
                S Offline
                Stefan_Lang
                wrote on last edited by
                #7

                Try http://en.wikipedia.org/wiki/Floating_point[^]. There is a chapter about Accuracy which describes the cause of your problem. There are several ways to get around this problem, but none of them is exactly simple: 1. The easiest one would be to ignore it, and use mathematical rounding whenever converting between text and floating point numbers. The only thing you really need are two conversion functions, and of course you always have to use those when conversion is required. 2. A somewhat better approach would be to multiply all monetary values by 100 and store them as int (or long). However, some monetary-based operations may require to consider fractions of Cents, so a factor of 100 may not suffice. Of course, calculations based on these numbers will also be tricky. 3. The best C++ approach would probably be to create a class for your currency, with conversions from and to strings, and basic mathematical operators that take care of however you choose to represent your values internally. While this would cause a lot of effort, there may be existing implementations on the internet that you can use.

                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