473,513 Members | 3,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A flirt with epsilon

When I run the following code ons ome (obscure) platforms,
I get strange results.
#include <stdio.h>
#include <float.h>
#include <math.h>

/* attempt to find something like DBL_EPSILON at run time */
double dbl_epsilon(void)
{
double x = 1, y;
do
{
y = x;
x *= 0.5;
}
while (x+1!=1);
return y;
}

int main(void)
{
#define log14_13 0.074107972153721878469097423336037692907
printf("DBL_EPSILON %g\n",DBL_EPSILON);
printf("dbl_epsilon %g\n",dbl_epsilon());
printf("log rel err at 14/13 %g\n",log(14./13)/log14_13-1);
printf("log1p rel err at 1/13 %g\n",log1p(1./13)/log14_13-1);
return 0;
}
On one platform I get:
DBL_EPSILON 2.22045e-16
dbl_epsilon 1.0842e-19 <- way better than DBL_EPSILON
log rel err at 14/13 5.42101e-19
log1p rel err at 1/13 -1.0842e-19
On another platform I get:
DBL_EPSILON 2.22045e-16
dbl_epsilon 2.22045e-16
log rel err at 14/13 -5.95187e-16
log1p rel err at 1/13 -3.33936e-17

How can the later result be much better than DBL_EPSILON,
by nearly 3 bits ?

TIA,

François Grieu
Nov 14 '05 #1
2 3788
Francois Grieu <fg****@francenet.fr> writes:
When I run the following code ons ome (obscure) platforms,
I get strange results.
I can reproduce this even on a mainstream platform (x86, gcc 3.3).
#include <stdio.h>
#include <float.h>
#include <math.h>

/* attempt to find something like DBL_EPSILON at run time */
double dbl_epsilon(void)
{
double x = 1, y;
do
{
y = x;
x *= 0.5;
}
while (x+1!=1);
return y;
}

int main(void)
{
#define log14_13 0.074107972153721878469097423336037692907
printf("DBL_EPSILON %g\n",DBL_EPSILON);
printf("dbl_epsilon %g\n",dbl_epsilon());
printf("log rel err at 14/13 %g\n",log(14./13)/log14_13-1);
printf("log1p rel err at 1/13 %g\n",log1p(1./13)/log14_13-1);
return 0;
}
On one platform I get:
DBL_EPSILON 2.22045e-16
dbl_epsilon 1.0842e-19 <- way better than DBL_EPSILON
log rel err at 14/13 5.42101e-19
log1p rel err at 1/13 -1.0842e-19
On another platform I get:
DBL_EPSILON 2.22045e-16
dbl_epsilon 2.22045e-16
log rel err at 14/13 -5.95187e-16
log1p rel err at 1/13 -3.33936e-17

How can the later result be much better than DBL_EPSILON,
by nearly 3 bits ?


I'm not sure your function to calculate DBL_EPSILON is correct. In
particular, the compiler can optimize the expression `x + 1 != 1'.

On my platform, if I replace said expression with `!cmp (x + 1.0, 1.0)'
and define

int cmp (double x, double y) { return x == y; }

in a different translation unit (to defeat any optimization attempt),
`dbl_epsilon ()' yields 2.22045e-16.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #2
Francois Grieu wrote:

When I run the following code ons ome (obscure) platforms,
I get strange results.

#include <stdio.h>
#include <float.h>
#include <math.h>

/* attempt to find something like DBL_EPSILON at run time */
double dbl_epsilon(void)
{
double x = 1, y;
do
{
y = x;
x *= 0.5;
}
while (x+1!=1);
return y;
}

int main(void)
{
#define log14_13 0.074107972153721878469097423336037692907
printf("DBL_EPSILON %g\n",DBL_EPSILON);
printf("dbl_epsilon %g\n",dbl_epsilon());
printf("log rel err at 14/13 %g\n",log(14./13)/log14_13-1);
printf("log1p rel err at 1/13 %g\n",log1p(1./13)/log14_13-1);
return 0;
}

On one platform I get:
DBL_EPSILON 2.22045e-16
dbl_epsilon 1.0842e-19 <- way better than DBL_EPSILON
log rel err at 14/13 5.42101e-19
log1p rel err at 1/13 -1.0842e-19

On another platform I get:
DBL_EPSILON 2.22045e-16
dbl_epsilon 2.22045e-16
log rel err at 14/13 -5.95187e-16
log1p rel err at 1/13 -3.33936e-17

How can the later result be much better than DBL_EPSILON,
by nearly 3 bits ?


You will probably find that your loop is using something like 80
bit floats, and leaving all results in the FP processor.
Investigate your compiler flags.

--
"I'm a war president. I make decisions here in the Oval Office
in foreign policy matters with war on my mind." - Bush.
"Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses." - James Rhodes.
Nov 14 '05 #3

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

Similar topics

0
867
by: Francois Grieu | last post by:
When I run the following code onsome (obscure) platforms, I get strange results. #include <stdio.h> #include <float.h> #include <math.h> /* attempt to find something like DBL_EPSILON at run time */ double dbl_epsilon(void)
2
2551
by: mao | last post by:
Hello all, Does anyone knows how to return the machine epsilon using C# or .NET in general ? In C++ it's numeric_limits<double>::epsilon() ; what about the C# equivalent? Thanks,
44
16278
by: Daniel | last post by:
I am grappling with the idea of double.Epsilon. I have written the following test: public void FuzzyDivisionTest() { double a = 0.33333d; double b = 1d / 3d; Assert.IsFalse(a == b, "Built-in == operator should not be
4
1792
by: David Veeneman | last post by:
Are System.Double operators overloaded to perform epsilon comparisons, or do these comparisons have to be performed by the programmer. In other words, if I have two doubles, A and B, and I test "A > B", does C# do a strict mathematical comparison, or does it do a "A - B > Double.Epsilon" comparison? Thanks
3
5650
by: Piotrekk | last post by:
Hi I have important question. This is the way iam calculating machine epsilon float fEps = 1.0f, fStore = 2.0f; int i1 = 0; // Calculating epsilon for float while (fStore > 1.0f) {
39
17753
by: jacob navia | last post by:
Hi. Continuing with my tutorial, here is an entry I have added recently. I hope it is not controversial. If you see any errors/ambiguities/etc please just answer in this thread. Thanks in advance for your help. ---------------------------------------------------------------------- The machine epsilon
0
7270
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, 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...
0
7178
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...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7563
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5703
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3252
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...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
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
0
470
bsmnconsultancy
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...

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.