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.0909090826637 827
1.036174 / 11.397911 = 0.0909091148369 205
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. 10 8849
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.0909090826637 827
1.036174 / 11.397911 = 0.0909091148369 205
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.
Well but with double the results are exactly the same...
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/
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.
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....@gm ail.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.
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.090909090909 09915440
; 1.048065059110 / 11.528715650212
~0.090909090909 07513819
; 1.002323075899* 11.528715650212 - 1.048065059110* 11.025553834888
~0.000000000003 05271121
So maybe results are actually good and your estimates aren't?
Best regards,
Yevgen
"Artemio" <ar*****@kdemai l.netwrote in message
news:11******** **************@ n33g2000cwc.goo glegroups.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.0909090826637 827
1.036174 / 11.397911 = 0.0909091148369 205
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.0023230758990 0000000
b == 11.025553834887 99900000
a / b == 0.0909090909090 9916900
x == 1.0480650591100 0000000
y == 11.528715650212 00000000
x / y == 0.0909090909090 7514600
"What Every Computer Scientist Should
Know About Floating-Point Arithmetic": http://docs.sun.com/source/806-3568/ncg_goldberg.html
-Mike
Thanks very much Mike, I'll give that one a thorough read!
Artemiy.
On Mar 8, 7:46 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
"Artemio" <arte...@kdemai l.netwrote in message
news:11******** **************@ n33g2000cwc.goo glegroups.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.0909090826637 827
1.036174 / 11.397911 = 0.0909091148369 205
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.0023230758990 0000000
b == 11.025553834887 99900000
a / b == 0.0909090909090 9916900
x == 1.0480650591100 0000000
y == 11.528715650212 00000000
x / y == 0.0909090909090 7514600
"What Every Computer Scientist Should
Know About Floating-Point Arithmetic":http://docs.sun.com/source/806-3568/ncg_goldberg.html
-Mike
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.0909090826637 827
1.036174 / 11.397911 = 0.0909091148369 205
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 need to be and normally, 1.70 will do.
Thank you,
Brad
|
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 as the results.
I expect 0.916666
|
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, inside the IDE
B) Debug build, outside the IDE (e.g. launched from Explorer)
C) Release build, inside the IDE
D) Release build, outside the IDE
Using methods A-C produces one result, and using method D produces a
|
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
operations. I'm using gcc on x86, but a lot of what I say applies to
other architectures.
First, I assume the div and ldiv functions are more efficient than
using / and % (obviously it depends on the compiler and level of
optimization, but...
|
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 replacement" and would
generally like 5/2 ==2.5
So, I was contemplating to default all my modules/scripts to start with
"from __future__ import division"
but if it is never coming (in this decade, that is) then it would be a
| |
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' division by zero and
just punch up a 0 result where such division is encountered so that I
can remove the where-clause?
Here's the query, I think it's very clear what I want to do:
select substr(tablespace_name, 1, 120) as TBSPC_NAME, used_pages,...
|
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 this way:
unsigned int dividend :
divident store low 32 bits of big integer, dividend store high 32
bits of big integer.
|
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 keeping a counter that kept a tab on
how many times I subtracted.
Later, the EA sport guy told me that of course there are can be better
technique by using bit operator.
|
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 : 1,190,476,179.~
but SQL gives me: 1,190,476,532.37.~
Is any way to force SQLServer to use maximum precision in constants and division results in a formula?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |