# Hi, how to increase accuracy of float result ?

Discussion in 'C++' started by dkultasev@gmail.com, May 5, 2005.

1. ### Guest

I mean, when I am trying to get result of 1/3 (or smf) it returns
0.33333333334 or something. I understand that result can not be 100%
right, but it is it possibly to get more accuracy ?
Sincerely,
sidukas

, May 5, 2005

2. ### Victor BazarovGuest

wrote:
> I mean, when I am trying to get result of 1/3 (or smf) it returns
> 0.33333333334 or something. I understand that result can not be 100%
> right, but it is it possibly to get more accuracy ?

Use 'double'. Some platforms have 'long double' different from 'double',
use that. Or you could use some kind of "arbitrary precision" package or
"exact arithmetic" package. Look for those on the web.

V

Victor Bazarov, May 5, 2005

3. ### Guest

long double doesn't give you accuracy. It increases limits. The limits
of float are enough for me.

, May 5, 2005
4. ### Artie GoldGuest

Artie Gold, May 5, 2005
5. ### Victor BazarovGuest

wrote:
> long double doesn't give you accuracy. It increases limits. The limits
> of float are enough for me.

Why are you arguing? You asked, I answered. If you don't believe me
(which is fine by me), use other sources of information.

'double' on most systems has at least 16 decimal digits of precision,
while 'float' has only 6. 'long double' (where it is different from
'double') has as many as 31 decimal digits (AIX for example). That
in my book says that by using 'double' you achieve higher accuracy.
The range is immaterial.

V

Victor Bazarov, May 5, 2005
6. ### Lionel BGuest

<> wrote in message news:...
>
> long double doesn't give you accuracy.

No, you misunderstand floating point; double may give you more precision than float, long double may give you more
precision than double.

> It increases limits. The limits of float are enough for me.

You may, in addition, get larger limits too. Have a look at the first couple of paragraphs of the article "What Every
Computer Scientist Should Know About Floating-Point Arithmetic":

http://docs.sun.com/source/806-3568/ncg_goldberg.html

for a brief intro.

--
Lionel B

Lionel B, May 5, 2005
7. ### Guest

I am not not arguing. I tried already double and long double but it
didnt give me enough accuracy.
that it will help. (spasibo)
Sincerely,
sidukas

, May 5, 2005
8. ### Victor BazarovGuest

wrote:
> I am not not arguing. I tried already double and long double but it
> didnt give me enough accuracy.

Could it be that you're judging the "accuracy" it gave you by simply
looking at the output? The default number of digits you get when
outputting a floating point value is 7. See 'setprecision' manipulator
to change that number.

> Thanks anyway, I downloaded some exact arithmetic packages, and I hope,
> that it will help. (spasibo)

It might not help. You need to fix your understanding of accuracy before
you move to a "better" representation.

V

Victor Bazarov, May 5, 2005
9. ### Guest

I am not looking to results on the screen. The problem is that I have
many operations with 2 float's. And the result supposed to be the same,
and then I am trying to do x=(a-b)/a*100 it gives ~1E-6<x<0.2, that is
not very good.

, May 5, 2005
10. ### Victor BazarovGuest

wrote:
> I am not looking to results on the screen. The problem is that I have
> many operations with 2 float's. And the result supposed to be the same,
> and then I am trying to do x=(a-b)/a*100 it gives ~1E-6<x<0.2, that is
> not very good.
>

I think this is covered in the FAQ 5.8.

V

Victor Bazarov, May 5, 2005
11. ### Andrew KoenigGuest

<> wrote in message
news:...

>I am not not arguing. I tried already double and long double but it
> didnt give me enough accuracy.

How much accuracy do you want, and why?

Andrew Koenig, May 5, 2005
12. ### Lionel BGuest

<> wrote in message news:...
> I am not looking to results on the screen. The problem is that I have
> many operations with 2 float's. And the result supposed to be the same,
> and then I am trying to do x=(a-b)/a*100 it gives ~1E-6<x<0.2, that is
> not very good.

I'm not entirely sure what you're trying to do (a fuller explanation might help), but my suspicion might be that you're
suffering from "catastrophic cancellation". This can happen when subtracting nearby floating point numbers which suffer
rounding error due to some calculation (a classic example of this is a naive implementation of the solution of quadratic
equations).

Again I urge you to look at: "What Every Computer Scientist Should Know About Floating-Point Arithmetic":

http://docs.sun.com/source/806-3568/ncg_goldberg.html

and look at the section "Cancellation".

--
Lionel B

Lionel B, May 5, 2005
13. ### Guest

I want as much as is it possible. Why? Because my program requaries it.
Sincerely,

, May 5, 2005
14. ### Guest

Thank You,
I can not look here for that moment, because I am not at home, I
Thanks a lot for everyone.

, May 5, 2005
15. ### Default UserGuest

wrote:
> long double doesn't give you accuracy. It increases limits. The

limits
> of float are enough for me.

Please quote enough of the previous message for context in your
replies. To do so using the Google interface, click on "show options"

Brian

Default User, May 5, 2005
16. ### David HarmonGuest

On 5 May 2005 09:26:37 -0700 in comp.lang.c++,
wrote,
>I want as much as is it possible. Why? Because my program requaries it.

Then your program is badly designed. You are not going to be happy
until you fix it to work with a _realistic_ degree of accuracy in your
floating point arithmetic, as all such programs that work are
designed. The answer does not lie in more accurate floats, because
there is no such thing as "as much as is possible."

But, if you are using "float" you should quickly switch to "double".

David Harmon, May 5, 2005