473,385 Members | 1,610 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,385 software developers and data experts.

checking if a number is zero within machine precision

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 precision? I am trying out this
method to check for a practical zero in an algorithm I am implementing
in C++.
-------------------------
#include <iostream>
#include <iomanip>
#include <limits>
#include <cmath>

int main(int argc, char ** argv){
if (argc != 2){
std::cerr << "No number given, quitting\n";
exit(-1);
}
double num = std::atof(argv[1]);
std::cout << std::numeric_limits<double>::epsilon() << std::endl;

if (std::fabs(num) < std::numeric_limits<double>::epsilon())
std::cout << "Numer is practically zero\n";
else
std::cout << "Number is non-zero\n";

return 0;
}
-------------------------

Here is a test run:
$g++ -Wall -ansi myeps.cc -o myeps -lm
$./myeps 1e-15
2.22045e-16
Number is non-zero

thanks,
->HS
Aug 21 '07 #1
4 7894
H.S. wrote:
I am trying out a few methods with which to test of a given number is
practically zero.
What's your definition of "zero"?
as an example, does the following test correctly if
a given number is zero within machine precision?
According to what defintion of "zero"? It tests if a given number is
below the "machine zero", which is the largest number that does not
change 1 if added to it, IOW, "machine zero" (eps): 1.0 + eps == 1.0
(or, in some definitions the machine zero is the smallest number that
when added to 1 actually changes it, eps: 1.0 + eps != 1.0). You have
to give your definition (even if it's just "use the machine zero").
I am trying out this
method to check for a practical zero in an algorithm I am implementing
in C++.
It all comes down to _your_ definition of "practical zero". For all we
know when it comes to placing a car on a city map, plus or minus one
foot does not really matter, so a foot is the "practical zero". But,
when manufacturing computer components, for example, a foot would be
a really large distance, and cannot be seen as "practical zero".

Catch my drift?
-------------------------
#include <iostream>
#include <iomanip>
#include <limits>
#include <cmath>

int main(int argc, char ** argv){
if (argc != 2){
std::cerr << "No number given, quitting\n";
exit(-1);
}
double num = std::atof(argv[1]);
std::cout << std::numeric_limits<double>::epsilon() <<
std::endl;

if (std::fabs(num) < std::numeric_limits<double>::epsilon())
std::cout << "Numer is practically zero\n";
else
std::cout << "Number is non-zero\n";

return 0;
}
-------------------------

Here is a test run:
$g++ -Wall -ansi myeps.cc -o myeps -lm
$./myeps 1e-15
2.22045e-16
Number is non-zero

thanks,
->HS
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 21 '07 #2
Victor Bazarov wrote:
H.S. wrote:
>I am trying out a few methods with which to test of a given number is
practically zero.

What's your definition of "zero"?
>as an example, does the following test correctly if
a given number is zero within machine precision?

According to what defintion of "zero"? It tests if a given number is
below the "machine zero", which is the largest number that does not
change 1 if added to it, IOW, "machine zero" (eps): 1.0 + eps == 1.0
Okay. Lets take this definition of zero you gave above.

->HS
Aug 21 '07 #3
H.S. wrote:
Victor Bazarov wrote:
>H.S. wrote:
>>I am trying out a few methods with which to test of a given number
is practically zero.

What's your definition of "zero"?
>>as an example, does the following test correctly if
a given number is zero within machine precision?

According to what defintion of "zero"? It tests if a given number is
below the "machine zero", which is the largest number that does not
change 1 if added to it, IOW, "machine zero" (eps): 1.0 + eps == 1.0

Okay. Lets take this definition of zero you gave above.
If you take that definition, you got your epsilon already. Keep in
mind that it doesn't necessarily fit the problem domain (which you
didn't mention at all). Consider also asking about those things in
'sci.math.num-analysis'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 22 '07 #4
In article <fa**********@aioe.org>, hs**************@gmail.com says...
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 precision? I am trying out this
method to check for a practical zero in an algorithm I am implementing
in C++.
Testing for near equality of two floating point numbers is a rather
tricky operation at best. The case you've given (one of the numbers is
zero) is an especially tricky case.

When you have two floating point numbers, and want to figure out whether
they're practically equal, you normally want to find the magnitudes of
the numbers, then use that magnitude to scale epsilon to figure out the
smallest difference that can be represented between numbers of that
magnitude. You then decide on the tolerance you'll allow (i.e. your
estimate of approximately how much rounding error your calculations may
have introduced) and multiply the scaled magnitude by that. Then you
check whether the absolute difference between the numbers falls within
that range or not.

Depending on the magnitudes of the numbers you started with, 1e-100
might be so large it means a lot, or it might be so small that it's
meaningless. For example, if you have 1.1e-100 and 1.0e-100, they're
probably not equal. While the absolute difference is about 1e-101, the
relative difference is about 10%, which is more than you'd expect to see
as rounding error from most calculations.

By contrast, 1.000000000000000001e-70 and 1.00000000000000002e-70
probably are practically equal. The absolute difference between the two
numbers is much larger than in the case above (about 1e-88 if I've
counted correctly), but the relative difference is _much_ smaller -- a
single bit about 18 places after the decimal point. This is often close
to the limit of the smallest difference that can be represented, so even
the slightest roundoff error in the calculation could lead to a
difference this large.

Attempting to compare to zero, however, means we no longer have the
magnitudes of the two numbers to guide us in guessing how much rounding
error we might expect. The only way the operation can be made meaningful
is by providing an explicit statement of the value below which we
consider the number "practically" zero. If we were to assume that one
range of numbers was as likely as any other, that value would be epsilon
about once ot of 1e309 times (or so). In reality, epsilon probably is
rightt more often than that, but only a little bit more often.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 25 '07 #5

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

Similar topics

19
by: alain dhaene | last post by:
Hi, I have a form with a input field. A user has to give a number greater than 0. I have build a validation that alerts the user when he gives a double, or a number less than 0. But I have...
2
by: Clint Olsen | last post by:
Hello: I posted a thread on comp.programming awhile back asking about an algorithm I implemented on square root. The idea was to use the square root of a prime number as a convenient way to get...
7
by: A. L. | last post by:
Consider following code segment: #1: double pi = 3.141592653589; #2: printf("%lf\n", pi); #3: printf("%1.12lf\n", pi); #4: printf("%1.15lf\n", pi); The above code outputs as following: ...
109
by: jmcgill | last post by:
Hello. Is there a method for computing the number of digits, in a given numeric base, of N factorial, without actually computing the factorial? For example, 8! has 5 digits in base 10; 10! has...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
11
by: Bryan Crouse | last post by:
I am looking a way to do error checking on a string at compile time, and if the string isn't the correct length have then have the compiler throw an error. I am working an embedded software that...
23
by: neha_chhatre | last post by:
which is the best format specifier(data type) if i have to work with decimal number. also please tell me the syntax for truncating a decimal number please reply as soon as possible
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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
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,...

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.