472,122 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 5539
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Wolfgang Meier | last post: by
2 posts views Thread by mao | last post: by
44 posts views Thread by Daniel | last post: by
39 posts views Thread by jacob navia | last post: by
reply views Thread by leo001 | last post: by

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.