H
H.S.
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
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