473,472 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Division precision issue

Hello all!

I just stumbled across a weird problem with precision of a division
operation. I am on Mac OS X, GCC 4.0.1.

Say I have two float or double numbers, and I want to divide one by
another, e.g.

float A, B, C;
C = A / B;

The problem is that the result my program gives for two different A
and B values are the same:

1.002323 / 11.025554 = 0.090909090909
1.036174 / 11.397911 = 0.090909090909

But with a calculator application, I get:

1.002323 / 11.025554 = 0.0909090826637827
1.036174 / 11.397911 = 0.0909091148369205

Does anyone have any suggestions as what I may be doing wrong? Is
there any special division function I should be using?
Thanks very much,

Artemiy.

Mar 8 '07 #1
10 8761
Artemio wrote:
Hello all!

I just stumbled across a weird problem with precision of a division
operation. I am on Mac OS X, GCC 4.0.1.

Say I have two float or double numbers, and I want to divide one by
another, e.g.

float A, B, C;
C = A / B;

The problem is that the result my program gives for two different A
and B values are the same:

1.002323 / 11.025554 = 0.090909090909
1.036174 / 11.397911 = 0.090909090909

But with a calculator application, I get:

1.002323 / 11.025554 = 0.0909090826637827
1.036174 / 11.397911 = 0.0909091148369205

Does anyone have any suggestions as what I may be doing wrong? Is
there any special division function I should be using?
How can you call this weird? You specify C float data type, defined in
std C as supporting 6 or more equivalent decimal digits precision, then
you compare to a calculator which has 15 decimal digits precision. YOur
comparison should be much closer if you use C double data type. But, if
your data are accurate only to 6 or 7 decimal digits, you may not care
how digits beyond those are generated.
Mar 8 '07 #2
Well but with double the results are exactly the same...

Mar 8 '07 #3
Artemio wrote:
Well but with double the results are exactly the same...
Show code.

--
Chris "electric hedgehog" Dollin
"It was the first really clever thing the King had said that day."
/Alice in Wonderland/

Mar 8 '07 #4
Artemio wrote:
Well but with double the results are exactly the same...
Generally calculator programs use platform specific types or
multiprecision libraries to provide a high level of precision. Plain C
has a long double type which might be sufficient for most purposes,
but it makes little sense to compare it with specialised mathematical
programs.

PS. Please quote the message to which you reply. Some people download
new messages and read, and respond, to them offline. This becomes
difficult when messages are not fairly standalone, in terms of context.

Mar 8 '07 #5
Okay guys, sorry for not being more clear with the issue.

Let's say I have this code:

double A, B;

printf("%0.12f / %0.12f = %0.12f\n", A, B, A / B );
For two different A and B it gives:

1.002323075899 / 11.025553834888 = 0.090909090909

and

1.048065059110 / 11.528715650212 = 0.090909090909

E.g. 1/11 and 1/11.5 are not that precise as you guess but the result
is the same! How in the world can that be? :-(
Artemiy.

On Mar 8, 5:51 pm, "santosh" <santosh....@gmail.comwrote:
Artemio wrote:
Well but with double the results are exactly the same...

Generally calculator programs use platform specific types or
multiprecision libraries to provide a high level of precision. Plain C
has a long double type which might be sufficient for most purposes,
but it makes little sense to compare it with specialised mathematical
programs.

PS. Please quote the message to which you reply. Some people download
new messages and read, and respond, to them offline. This becomes
difficult when messages are not fairly standalone, in terms of context.

Mar 8 '07 #6
Artemio wrote:
Okay guys, sorry for not being more clear with the issue.

Let's say I have this code:

double A, B;

printf("%0.12f / %0.12f = %0.12f\n", A, B, A / B );
For two different A and B it gives:

1.002323075899 / 11.025553834888 = 0.090909090909

and

1.048065059110 / 11.528715650212 = 0.090909090909

E.g. 1/11 and 1/11.5 are not that precise as you guess but the result
is the same! How in the world can that be? :-(
muntyan@munt10:~$ calc
C-style arbitrary precision calculator (version 2.12.1.5)
Calc is open software. For license details type: help copyright
[Type "exit" to exit, or "help" for help.]

; 1.002323075899 / 11.025553834888
~0.09090909090909915440
; 1.048065059110 / 11.528715650212
~0.09090909090907513819
; 1.002323075899*11.528715650212 - 1.048065059110*11.025553834888
~0.00000000000305271121

So maybe results are actually good and your estimates aren't?

Best regards,
Yevgen
Mar 8 '07 #7

"Artemio" <ar*****@kdemail.netwrote in message
news:11**********************@n33g2000cwc.googlegr oups.com...
Hello all!

I just stumbled across a weird problem with precision of a division
operation. I am on Mac OS X, GCC 4.0.1.

Say I have two float or double numbers, and I want to divide one by
another, e.g.

float A, B, C;
C = A / B;

The problem is that the result my program gives for two different A
and B values are the same:

1.002323 / 11.025554 = 0.090909090909
1.036174 / 11.397911 = 0.090909090909

But with a calculator application, I get:

1.002323 / 11.025554 = 0.0909090826637827
1.036174 / 11.397911 = 0.0909091148369205

Does anyone have any suggestions as what I may be doing wrong? Is
there any special division function I should be using?
#include <stdio.h>

int main()
{
double a = 1.002323075899;
double b = 11.025553834888;
double c = a / b;
double x = 1.048065059110;
double y = 11.528715650212;
double z = x / y;

printf("a == %.20f\n"
"b == %.20f\n"
"a / b == %.20f\n\n"
"x == %.20f\n"
"y == %.20f\n"
"x / y == %.20f\n",
a, b, c, x, y, z);

return 0;
}

Output (MSVC 2005 Express, Pentium 4):

a == 1.00232307589900000000
b == 11.02555383488799900000
a / b == 0.09090909090909916900

x == 1.04806505911000000000
y == 11.52871565021200000000
x / y == 0.09090909090907514600
"What Every Computer Scientist Should
Know About Floating-Point Arithmetic":
http://docs.sun.com/source/806-3568/ncg_goldberg.html
-Mike
Mar 8 '07 #8
Thanks very much Mike, I'll give that one a thorough read!

Artemiy.

On Mar 8, 7:46 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"Artemio" <arte...@kdemail.netwrote in message

news:11**********************@n33g2000cwc.googlegr oups.com...
Hello all!
I just stumbled across a weird problem with precision of a division
operation. I am on Mac OS X, GCC 4.0.1.
Say I have two float or double numbers, and I want to divide one by
another, e.g.
float A, B, C;
C = A / B;
The problem is that the result my program gives for two different A
and B values are the same:
1.002323 / 11.025554 = 0.090909090909
1.036174 / 11.397911 = 0.090909090909
But with a calculator application, I get:
1.002323 / 11.025554 = 0.0909090826637827
1.036174 / 11.397911 = 0.0909091148369205
Does anyone have any suggestions as what I may be doing wrong? Is
there any special division function I should be using?

#include <stdio.h>

int main()
{
double a = 1.002323075899;
double b = 11.025553834888;
double c = a / b;
double x = 1.048065059110;
double y = 11.528715650212;
double z = x / y;

printf("a == %.20f\n"
"b == %.20f\n"
"a / b == %.20f\n\n"
"x == %.20f\n"
"y == %.20f\n"
"x / y == %.20f\n",
a, b, c, x, y, z);

return 0;

}

Output (MSVC 2005 Express, Pentium 4):

a == 1.00232307589900000000
b == 11.02555383488799900000
a / b == 0.09090909090909916900

x == 1.04806505911000000000
y == 11.52871565021200000000
x / y == 0.09090909090907514600

"What Every Computer Scientist Should
Know About Floating-Point Arithmetic":http://docs.sun.com/source/806-3568/ncg_goldberg.html

-Mike

Mar 8 '07 #9
Artemio wrote:
Say I have two float or double numbers, and I want to divide one by
another, e.g.

float A, B, C;
C = A / B;

The problem is that the result my program gives for two different A
and B values are the same:

1.002323 / 11.025554 = 0.090909090909
1.036174 / 11.397911 = 0.090909090909

But with a calculator application, I get:

1.002323 / 11.025554 = 0.0909090826637827
1.036174 / 11.397911 = 0.0909091148369205

Does anyone have any suggestions as what I may be doing wrong?
The problem is loss of precision, but the important question is where.
The actual results are 6 and 5 significant digits, worse than expected
for most single precision floating point implementations.

You get much closer to the results indicated earlier with

1.002323 / 11.025553
1.036174 / 11.397914

I suspect this is due to rounding error with limited precision of
conversion on the denominators. Try increasing the precision of the
conversion and storing all values in double, or long double, if the
results you get above is not accurate enough for your application.

--
Thad
Mar 9 '07 #10
Artemio wrote:
>
Thanks very much Mike, I'll give that one a thorough read!
You have already been asked to correct the top-posting. Do so, or
you are likely to be ignored by many here. Your answer belongs
after (or intermixed with) the *snipped* material to which you
reply. The snipping removes anything not germane to that reply.
See the following links:

--
Some informative links:
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/ (taming google)
<http://members.fortunecity.com/nnqweb/ (newusers)

--
Posted via a free Usenet account from http://www.teranews.com

Mar 9 '07 #11

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

Similar topics

14
by: Brad Tilley | last post by:
Hello, What is the proper way to limit the results of division to only a few spaces after the decimal? I don't need rocket-science like precision. Here's an example: 1.775 is as exact as I...
8
by: Dave Brown | last post by:
Hi all, I'm having trouble understanding which data type to use to get results I expect. using float, decimal, and double I have tried the following, .. float test = 55/60; I always get 0...
3
by: John C Kirk | last post by:
I've come across an odd situation, where doing a floating point division produces different results for the same numbers. Basically, there are 4 ways to run this application: A) Debug build,...
29
by: nessuno | last post by:
I can't find any discussion of this question in this NG. I'd like to implement some variable precision integer arithmetic in C, and do it efficiently. A problem arises with the divide/remainder...
17
by: seb.haase | last post by:
Hi, Is it true that that "Python 3000" is dead ? Honestly I think that e.g. changing 5/2 to be 2.5 (instead of 2) would just break to much code :-( On the otherhand I'm using Python as "Matlab...
2
by: Pakna | last post by:
Why do I have a zero result on this query for all non-zero rows? Do I have to declare specifically the precision of ratio division? How do I do that? And is there a way to make DB2 'ignore'...
13
by: jamesonang | last post by:
Supposed unsigned int(32 bits) is the largest number that computer can represent with a single variable. Now, i have a big integer ( less than 64 bit, but great than 32 bit) . i represent it by...
94
by: krypto.wizard | last post by:
Last month I appeared for an interview with EA sports and they asked me this question. How would you divide a number by 7 without using division operator ? I did by doing a subtraction and...
1
by: OmidLeo | last post by:
Hi All, A very simple but confusing problem, I've now a lot of calculations in stored procedures that are all inaccurate!, e.g 1234567890 * (1.0 - 0.5/14.0) the result is actually...
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...
1
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.