problem with conversion of string to float
-
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:
-
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:
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.
-
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:
-
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:
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
-
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:
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
-
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
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
-
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
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
(orlong
). 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.