473,511 Members | 16,864 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

machine epsilon for float - i dont agree

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)
{
fEps /= 2;
fStore = fEps + 1.0f;
i1--;
}
i1++; fEps *=2;
Can anyone explain me why when i calculate using floats and obtain
float value :-0.0388556346
this value is smaller than calculated epsilon?
Thanks for your time.
My epsilon is: 1,1920929E-07 - true due to IEEE standards
PK

Nov 23 '05 #1
3 5649
Piotrekk <Pi*************@gmail.com> wrote:
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)
{
fEps /= 2;
fStore = fEps + 1.0f;
i1--;
}
i1++; fEps *=2;
Can anyone explain me why when i calculate using floats and obtain
float value :-0.0388556346
Odd - I get: 2.220446E-16
this value is smaller than calculated epsilon?
Thanks for your time.
My epsilon is: 1,1920929E-07 - true due to IEEE standards


Because your variables are local variables, I believe the JIT is
actually deciding to keep them in the 80 bit floating point registers,
only converting them to real 32-bit floats where necessary. They're
therefore more precise than you might expect.

Try the following program, which is slightly different:
using System;

public class Test
{
static volatile float fStore=2.0f;

static void Main()
{
float fEps = 1.0f;
int i1 = 0;

// Calculating epsilon for float
while (fStore > 1.0f)
{
fEps /= 2;
fStore = fEps + 1.0f;
i1--;
}
i1++; fEps *=2;
Console.WriteLine (fEps);
}
}

Note that fStore is now a static variable (and volatile preventing any
"delayed writing" - it worked for me without it, but it doesn't hurt!)
you should find you get the expected answer.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 23 '05 #2
Nope. I am still gettin
1,1920929E-07

Do you think it might be caused by C# 2005?

Nov 23 '05 #3
Piotrekk <Pi*************@gmail.com> wrote:
Nope. I am still gettin
1,1920929E-07

Do you think it might be caused by C# 2005?


I thought 1,1920929E-07 was what you expected to get. After all, you
wrote:

<quote>
My epsilon is: 1,1920929E-07 - true due to IEEE standards
</quote>

Is that not the value you're expecting?

Note that you're only using 32-bit floating point values here. If you
use doubles, you'll get 2.22044604925031E-16. Is that what you were
expecting? I'm not sure where you got the value of 0.0388556346 which
you mentioned in your original post - could you elaborate on that?

Note that your idea of epsilon (the smallest floating point such that
adding it to 1 gives a result distinct to 1) is not the same as the
values of Double.Epsilon or Single.Epsilon, each of which is the
smallest strictly positive value for that type. That's a much smaller
number in each case.

Given the definition of machine epsilon given at
http://www.netlib.org/lapack/lug/node74.html, I'm not sure how the
routine you've given is meant to be calculating it - I have to say
though, I haven't looked at xLAMCH myself, and possibly your code is
based on that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 23 '05 #4

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

Similar topics

16
2579
by: Wolfgang Meier | last post by:
Hi, Although it might seem like I am firing out random posts in quick succession this matter is indeed one I thought about for quite some time without coming to a conclusion: Why is it that...
2
2550
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
16276
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,...
39
17752
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...
4
7909
by: H.S. | last post by:
Hello, I am trying out a few methods with which to test of a given number is practically zero. as an example, does the following test correctly if a given number is zero within machine...
0
7138
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
7423
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
5668
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
5066
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
3225
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...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
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 ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
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...

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.