473,396 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Hi, how to increase accuracy of float result ?

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

Jul 23 '05 #1
15 5187
dk*******@gmail.com 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
Jul 23 '05 #2
long double doesn't give you accuracy. It increases limits. The limits
of float are enough for me.

Jul 23 '05 #3
dk*******@gmail.com wrote:
long double doesn't give you accuracy. It increases limits. The limits
of float are enough for me.

Erm, how to put this succinctly...

Wrong.

--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #4
dk*******@gmail.com 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
Jul 23 '05 #5
<dk*******@gmail.com> wrote in message news:11**********************@f14g2000cwb.googlegr oups.com...

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

Jul 23 '05 #6
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

Jul 23 '05 #7
dk*******@gmail.com 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
Jul 23 '05 #8
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.

Jul 23 '05 #9
<dk*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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?
Jul 23 '05 #10
<dk*******@gmail.com> wrote in message news:11*********************@o13g2000cwo.googlegro ups.com...
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

Jul 23 '05 #11
dk*******@gmail.com 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
Jul 23 '05 #12
I want as much as is it possible. Why? Because my program requaries it.
Sincerely,

Jul 23 '05 #13
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.

Jul 23 '05 #14

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


Brian

Jul 23 '05 #15
On 5 May 2005 09:26:37 -0700 in comp.lang.c++, dk*******@gmail.com
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".

Jul 23 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Michael Lang | last post by:
You'd think that the advantage to extra decimal places in a data type is better accuracy. Well think again. Look at this method and test results: ...
1
by: kk_oop | last post by:
Hi. We have some code that was being accessed through Corba. We recently removed the Corba and replaced it with local method calls. This caused the code to break. Our code is filled with...
8
by: Darsant | last post by:
I'm currently reading 1-n number of binary files, each with 3 different arrays of floats containing about 10,000 values a piece for a total of about 30,000 values per file. I'm looking for a way...
5
by: Mr Fish | last post by:
Is it possible for me to record floats in a std::stringstream with 100% accuracy? If so how? Here's a little prog that demonstrates how the default ss loses accuracy ...
3
by: gary | last post by:
Hi, 1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth decides their increase direction, CPU architecture, OS...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
14
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
5
by: Sensei | last post by:
Hi again! I am trying to figure a simple example of a program that shows the accuracy differences between float and double types. In particular I'm searching for an operation that works on...
16
by: Hatzigiannakis Nikos | last post by:
In the following code main() { double x; for (x=0.0;x<=1;x=x+0.05) { printf("x=%6.3f \n",x); } }
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.