473,508 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to test a 'float' or 'double' zero numerically?

Hi,

Suppose T is 'float' or 'double'.

T x;

x < 10 * std::numeric_limits<T>::epsilon();

I can use the above comparison to test if 'x' is numerically zero. But
I'm wondering what should be a good multiplicative constant before
epsilon?

Thanks,
Peng
Sep 13 '08 #1
15 27759
I can use the above comparison to test if 'x' is numerically zero.

No, as x can also be negative.
But I'm wondering what should be a good multiplicative constant before
epsilon?
Epsilon is the smallest value such such that 1.0 + epsilon != 1.0. You
need to scale it with the numbers to compare with. Comparing against
zero is always hard. You are probably best of with using abs(x) <
your_own_epsilon. Set your_own_epsilon to what ever you want, such as
0.00000001 perhaps.

Regards,
Anders Dalvander
Sep 13 '08 #2
On Sep 13, 11:10 am, Anders Dalvander <goo...@dalvander.comwrote:
I can use the above comparison to test if 'x' is numerically zero.

No, as x can also be negative.
Right, I meant std::abs(x).
But I'm wondering what should be a good multiplicative constant before
epsilon?

Epsilon is the smallest value such such that 1.0 + epsilon != 1.0. You
need to scale it with the numbers to compare with. Comparing against
zero is always hard. You are probably best of with using abs(x) <
your_own_epsilon. Set your_own_epsilon to what ever you want, such as
0.00000001 perhaps.
Therefore, there is no general accept such epsilon?

Thanks,
Peng
Sep 13 '08 #3
Peng Yu <Pe*******@gmail.comkirjutas:
Hi,

Suppose T is 'float' or 'double'.

T x;

x < 10 * std::numeric_limits<T>::epsilon();

I can use the above comparison to test if 'x' is numerically zero. But
Really? What if x is -10000? What if it is equal to std::numeric_limits
<T>::epsilon()?
I'm wondering what should be a good multiplicative constant before
epsilon?
To answer your question literally, then comparing to zero is easy, just
use if(x==0). However, this usually does not give you much if x is a
result of some computation, with this expression you can pretty much just
check whether x has been assigned literal zero beforehand.

If you want to compare values appearing in some numeric algorithm then
this all very much depends on the algorithm. What is your actual problem
you are trying to solve?

Paavo


Sep 13 '08 #4
Peng Yu wrote:
x < 10 * std::numeric_limits<T>::epsilon();

I can use the above comparison to test if 'x' is numerically zero.
No you can't. A value of x distinct from zero might also test as
"zero" with that.
Sep 13 '08 #5
Hi,

Consider a machine where the smallest number that can be represented is
0.0001

Lets asume I have the following calculation (lets assume the 0.00005 would
be the result of some calculaton).
0.0001 -0.00005 - 0.00005
Now it is obvious that this should result in zero. However the last two
results would be zero since the machine can only have up to four digits
behind the dot. So what should be zero is actually 0.0001 so a correct value
for a multiplier for epsilon would be 0.0002. Reasoning 0.0001 < 0.0002
therefore it is zero?

Consider then the following

The same formula only we also divide by 0.0001 afterwards
( 0.0001 -0.00005 - 0.00005 ) / 0.0001 = 1 However the one actually should
be a zero therefore our first conclusion was incorrect. A correct multiplier
for epsilon should be 10001

Of course one could go on, epsilons multiplier could be anything.

Conclusion there is not a correct multiplier for epsilon. There can be one
per formula but that is probably not very practical.
Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

"Peng Yu" <Pe*******@gmail.comwrote in message
news:2e**********************************@w7g2000h sa.googlegroups.com...
Hi,

Suppose T is 'float' or 'double'.

T x;

x < 10 * std::numeric_limits<T>::epsilon();

I can use the above comparison to test if 'x' is numerically zero. But
I'm wondering what should be a good multiplicative constant before
epsilon?

Thanks,
Peng

Sep 13 '08 #6
On Sep 13, 4:48 pm, "Ron AF Greve" <ron@localhostwrote:
Hi,

Consider a machine where the smallest number that can be represented is
0.0001

Lets asume I have the following calculation (lets assume the 0.00005 would
be the result of some calculaton).
0.0001 -0.00005 - 0.00005
Now it is obvious that this should result in zero. However the last two
results would be zero since the machine can only have up to four digits
behind the dot. So what should be zero is actually 0.0001 so a correct value
for a multiplier for epsilon would be 0.0002. Reasoning 0.0001 < 0.0002
therefore it is zero?

Consider then the following

The same formula only we also divide by 0.0001 afterwards
( 0.0001 -0.00005 - 0.00005 ) / 0.0001 = 1 However the one actually should
be a zero therefore our first conclusion was incorrect. A correct multiplier
for epsilon should be 10001

Of course one could go on, epsilons multiplier could be anything.

Conclusion there is not a correct multiplier for epsilon. There can be one
per formula but that is probably not very practical.
I see. Then the problem is how to derive it for a particular formula.

Probably, I need to write down the formula and take the derivatives of
all its arguments, check how much errors there could be for each
arguments. Then I would end up with a bound of the rounding error
(epsilon is equivalent to it). Right?

Thanks,
Peng
Sep 13 '08 #7
On 14 Sep, 01:53, Peng Yu <PengYu...@gmail.comwrote:
On Sep 13, 4:48 pm, "Ron AF Greve" <ron@localhostwrote:
Of course one could go on, epsilons multiplier could be anything.
Conclusion there is not a correct multiplier for epsilon. There can be one
per formula but that is probably not very practical.

I see. Then the problem is how to derive it for a particular formula.

Probably, I need to write down the formula and take the derivatives of
all its arguments, check how much errors there could be for each
arguments. Then I would end up with a bound of the rounding error
(epsilon is equivalent to it). Right?
Numerical analysis is an art in itself. There are departments
in universities which deal almost exclusively with the analysis
of numerics, which essentially boils down to error analysis.

In my field of work certain analytical solutions were formulated
in the early '50s, but a stable numerical solution wasn't found
until the early/mid '90s.

You might want to check with the math department at your local
university on how to approach whatever problem you work with.

Rune
Sep 14 '08 #8
In my field of work certain analytical solutions were formulated
in the early '50s, but a stable numerical solution wasn't found
until the early/mid '90s.
Would you please give some example references on this?

Thanks,
Peng

Sep 14 '08 #9
On 2008-09-13 18:16, Peng Yu wrote:
On Sep 13, 11:10 am, Anders Dalvander <goo...@dalvander.comwrote:
I can use the above comparison to test if 'x' is numerically zero.

No, as x can also be negative.

Right, I meant std::abs(x).
But I'm wondering what should be a good multiplicative constant before
epsilon?

Epsilon is the smallest value such such that 1.0 + epsilon != 1.0. You
need to scale it with the numbers to compare with. Comparing against
zero is always hard. You are probably best of with using abs(x) <
your_own_epsilon. Set your_own_epsilon to what ever you want, such as
0.00000001 perhaps.

Therefore, there is no general accept such epsilon?
No, different applications requires different precision, some would
consider a variable equal to zero if it was 0.0001 from zero while
others might require 0.0000001. You have to analyse your problem to find
a value that suites you.

--
Erik Wikström
Sep 14 '08 #10
On 14 Sep, 04:06, Peng Yu <PengYu...@gmail.comwrote:
In my field of work certain analytical solutions were formulated
in the early '50s, but a stable numerical solution wasn't found
until the early/mid '90s.

Would you please give some example references on this?
At the risk of becoming inaccurate, as I haven't reviewed
the material in 5 years and write off the top of my head:

Around 1953-55 Tompson and Haskell proposed a method to
compute the propagation of seismic waves through layered
media. The method used terms on the form

x = (exp(y)+1)/(exp(z)+1)

where y and z were of large magnitude and 'almost equal'.
In a perfect formulation x would be very close to 1.

Since y and z are large an one uses an imperfect numerical
representation, the computation errors in the exponents
become important. So basically the terms that should
cancel didn't, and one was left with a numerically unstable
solution.

There were made several attempts to handle this (Ng and Reid
in the '70s, Henrik Schmidt in the '80), with varoius
degrees of success. And complexity. As far as I am concerned,
the problem wasn't solved until around 1993 when Sven Ivansson
came up with a numerically stable scheme.

What all these attempts had in common was that they took
the original analytical formulation and organized the terms
in various ways to avoid the complicated, large-magnitude
internal terms.

I am sure there are simuilar examples in other areas.

As for an example on error analysis, you could check out the
analysis of Horner's rule for evaluating polynomials, which
is tretaed in most intro books on numerical analysis.

Rune
Sep 14 '08 #11
On 14 Sep, 12:38, Rune Allnor <all...@tele.ntnu.nowrote:
Around 1953-55 Tompson and Haskell proposed a method to
compute the propagation of seismic waves through layered
media. The method used terms on the form

*x = (exp(y)+1)/(exp(z)+1)

where y and z were of large magnitude and 'almost equal'.
In a perfect formulation x would be very close to 1.
Typo correction: The problematics terms were on the form

x = exp(y)-exp(z)

where y and z are large and x is small.

Rune
Sep 14 '08 #12
Hi,

You could indeed do an analysis that way. Actually that kind of thing is
also done when measuring something and one has to know the error in the
measurement. Taking in account the accuracy of measuring equipment and the
kind of operation you (multiplying, addition etc) you can the tell what the
error range is (like I measured 5V +/- 0.5V.

It is a lot of work though.

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

"Peng Yu" <Pe*******@gmail.comwrote in message
news:79**********************************@f63g2000 hsf.googlegroups.com...
On Sep 13, 4:48 pm, "Ron AF Greve" <ron@localhostwrote:
>Hi,

Consider a machine where the smallest number that can be represented is
0.0001

Lets asume I have the following calculation (lets assume the 0.00005
would
be the result of some calculaton).
0.0001 -0.00005 - 0.00005
Now it is obvious that this should result in zero. However the last two
results would be zero since the machine can only have up to four digits
behind the dot. So what should be zero is actually 0.0001 so a correct
value
for a multiplier for epsilon would be 0.0002. Reasoning 0.0001 < 0.0002
therefore it is zero?

Consider then the following

The same formula only we also divide by 0.0001 afterwards
( 0.0001 -0.00005 - 0.00005 ) / 0.0001 = 1 However the one actually
should
be a zero therefore our first conclusion was incorrect. A correct
multiplier
for epsilon should be 10001

Of course one could go on, epsilons multiplier could be anything.

Conclusion there is not a correct multiplier for epsilon. There can be
one
per formula but that is probably not very practical.

I see. Then the problem is how to derive it for a particular formula.

Probably, I need to write down the formula and take the derivatives of
all its arguments, check how much errors there could be for each
arguments. Then I would end up with a bound of the rounding error
(epsilon is equivalent to it). Right?

Thanks,
Peng

Sep 14 '08 #13
On Sep 13, 5:35 pm, Peng Yu <PengYu...@gmail.comwrote:
Suppose T is 'float' or 'double'.
T x;
x < 10 * std::numeric_limits<T>::epsilon();
I can use the above comparison to test if 'x' is numerically
zero.
If you want to test whether x is numerically zero, "x == 0.0" is
the only correct way.
But I'm wondering what should be a good multiplicative
constant before epsilon?
There isn't one, since the idiom is broken (in general---there
are specific cases where it might be appropriate).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 15 '08 #14
On Sep 13, 6:23 pm, Paavo Helde <nob...@ebi.eewrote:
Peng Yu <PengYu...@gmail.comkirjutas:
Suppose T is 'float' or 'double'.
T x;
x < 10 * std::numeric_limits<T>::epsilon();
I can use the above comparison to test if 'x' is numerically zero. But
Really? What if x is -10000? What if it is equal to
std::numeric_limits <T>::epsilon()?
I'm wondering what should be a good multiplicative constant before
epsilon?
To answer your question literally, then comparing to zero is
easy, just use if(x==0). However, this usually does not give
you much if x is a result of some computation, with this
expression you can pretty much just check whether x has been
assigned literal zero beforehand.
It depends on the computation. There are a lot of contexts
where you get 0.0 exactly, and that's what you want to test for.
There are less contexts where this is true for other values (0.0
is a bit special), but they also exist.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 15 '08 #15
On Sep 14, 1:53 am, Peng Yu <PengYu...@gmail.comwrote:
On Sep 13, 4:48 pm, "Ron AF Greve" <ron@localhostwrote:
Consider a machine where the smallest number that can be
represented is 0.0001
Lets asume I have the following calculation (lets assume the
0.00005 would be the result of some calculaton).
0.0001 -0.00005 - 0.00005
Now it is obvious that this should result in zero. However
the last two results would be zero since the machine can
only have up to four digits behind the dot. So what should
be zero is actually 0.0001 so a correct value for a
multiplier for epsilon would be 0.0002. Reasoning 0.0001 <
0.0002 therefore it is zero?
Consider then the following
The same formula only we also divide by 0.0001 afterwards
( 0.0001 -0.00005 - 0.00005 ) / 0.0001 = 1 However the one
actually should be a zero therefore our first conclusion was
incorrect. A correct multiplier for epsilon should be 10001
Of course one could go on, epsilons multiplier could be anything.
Conclusion there is not a correct multiplier for epsilon.
There can be one per formula but that is probably not very
practical.
I see. Then the problem is how to derive it for a particular formula.
No. The problem is how to implement the formula so that it
gives the correct results.
Probably, I need to write down the formula and take the
derivatives of all its arguments, check how much errors there
could be for each arguments. Then I would end up with a bound
of the rounding error (epsilon is equivalent to it). Right?
Not necessarily. You need to better understand how machine
floating point works, and the mathematics which underlies it.

Think of it for a minute. If I had a system in which sin(0.0)
returned anything but 0.0 (exactly), I'd consider it defective.
For other values, this is somewhat less obvious, but 0.0 (and in
some contexts, 1.0 and -1.0) are a bit special.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 15 '08 #16

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

Similar topics

11
508
by: Gurikar | last post by:
Hello, Can any one tell me is the code below correct. #include<iostream.h> int main() { int i = 1; float f = i / 2; if(f) cout<<"HI";
2
32273
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in...
6
2630
by: Nate Bargmann | last post by:
I am working on a function that takes degrees, minutes, seconds coordinates and converts them to decimal representation. Traditionally, in DMS notation the '-' sign, to indicate west longitude or...
54
8293
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This...
16
2531
by: Gerald Lafreniere | last post by:
{ float F=123.456000; F*=1000; // Actually I used a for loop F*=10 three times. printf("%f\n", F); } This will produce something like 123456.00XXXX, where XXXX are garbage digits. Why...
6
7594
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
40
2118
by: Allan M. Bruce | last post by:
I am applying for my first jobs after completing my PhD. I have been asked by a company to go and take a C programming test to see how my C skills are. Apparantly this test is mostly finding...
116
35678
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
45
5240
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
0
7326
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,...
0
7385
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...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5053
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
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3195
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
1558
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
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
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.