1. ## accuracy

i recently wrote a money-involved program and am just wondering:

[list=1][*]is it normal for a float to be inaccurate to the hundredth/thousandth?[*]what is the accuarcy (approx, of course) of a float[*]what is the accuracy of a double[*]any good algorithms for rounding to a certain number of decimal places (like setprecision, except with rounding)[/list=1]

i'm not sure if all this has to do with the OS/System, but yeah...

2. I haven't read through the entire thread, but there might be something helpful here

3. I read most of that post, and it seems to have failed to mention one very critical point about why the inacuraccies occur. You're dealing with base 10 decimal numbers. The computer is dealing with base 2 decimal numbers. Given a small mantissa, it is difficult to represent one as the other with perfect accuracy. So, to answer (1), yes.

For (2) and (3), the answer is in C++PL, I believe (under numeric limits), but I have no idea where my copy of it is right now.

For (4), take the number of digits you want, and then look at the one following it (by dividing by the appropriate power of 10). This'll be better for printing than storing, as if the machine could not accurately represent the number once, it probably won't be able to the second time.

Hope this is at least somewhat useful.

Cheers

4. Cheers
Just wondering, are you from Britain? Everyone I know from there says that

5. Originally posted by Zach L.
For (2) and (3), the answer is in C++PL, I believe (under numeric limits), but I have no idea where my copy of it is right now.
what is that and where can i get it?

I also didn't think about the binary thing... if forgot about that...

6. Actually, I'm from the small, third-world country of New Mexico.

Sorry 'bout the acronym. C++PL is Stroustrup's The C++ Programming Language : Special Edition. It really is a great reference. Anyways, it talks about the numeric_limits templated class in <limits>. Essentially, its a specialized type which has a lot of machine-independent information (consequently, the exact values will vary). However, the examples he uses are of fairly common representations for floats/doubles/etc.

For example (I dug it off the book shelf), for a float, it gives a base 2 mantissa of 24 bits (approx. 6 decimal digits), and an epsilon of 1.19...E-7 (that is, the smallest epsion such that 1.0 + epsilon != 1.0 within machine limits).

7. thanks... i'll look into getting a copy... now i just need money

8. Lots of Australians also say cheers.

I just recently bought a copy of Stroustrups C++PL, also effective C++ and Programming Windows, talk about expensive!

C++PL is difficult to read after reading Shidlt, Shidlt is a good writer, even if he's not a good coder.

I'm sure it's a good book but wow, he goes into some advanced \$\$\$\$ in the first 4 chapters, if I was newbie at C++ I would burn the thing. First thing he talks about is user defined types, then he goes onto the basics of STL.

9. It is an excellent reference. For the faint of heart, or those unfamiliar with the language already though, it can be rough. It is good for learning advanced stuff though.

10. I use it as a reference also. But I think it starts on chapter 4 (or maybe 5??) with the real learning stuff. Chapters 1-3 are just introduction, which may be why its even harder to read.

11. Originally posted by Zach L.
It is an excellent reference. For the faint of heart, or those unfamiliar with the language already though, it can be rough. It is good for learning advanced stuff though.
what would you call advanced?

Popular pages Recent additions