473,378 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 5643
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
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
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
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
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.