Hi, how to increase accuracy of float result ?

D

dkultasev

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
 
V

Victor Bazarov

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
 
D

dkultasev

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

Victor Bazarov

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
 
L

Lionel B

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.
 
D

dkultasev

I am not not arguing. I tried already double and long double but it
didnt give me enough accuracy.
Thanks anyway, I downloaded some exact arithmetic packages, and I hope,
that it will help. (spasibo:))
Sincerely,
sidukas
 
V

Victor Bazarov

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
 
D

dkultasev

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.
 
V

Victor Bazarov

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
 
L

Lionel B

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".
 
D

dkultasev

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

dkultasev

Thank You,
I can not look here for that moment, because I am not at home, I
downloaded this page and I will take a look at home.
Thanks a lot for everyone.
 
D

Default User

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"
and use the Reply shown in the expanded header.




Brian
 
D

David Harmon

On 5 May 2005 09:26:37 -0700 in comp.lang.c++, (e-mail address removed)
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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top