473,662 Members | 2,637 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_li mits<T>::epsilo n();

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 27848
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_epsilo n. Set your_own_epsilo n 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...@dalvand er.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_epsilo n. Set your_own_epsilo n 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*******@gmai l.comkirjutas:
Hi,

Suppose T is 'float' or 'double'.

T x;

x < 10 * std::numeric_li mits<T>::epsilo n();

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_li mits
<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_li mits<T>::epsilo n();

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*******@gmai l.comwrote in message
news:2e******** *************** ***********@w7g 2000hsa.googleg roups.com...
Hi,

Suppose T is 'float' or 'double'.

T x;

x < 10 * std::numeric_li mits<T>::epsilo n();

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@localhostw rote:
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...@gmai l.comwrote:
On Sep 13, 4:48 pm, "Ron AF Greve" <ron@localhostw rote:
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...@dalvand er.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_epsil on. Set your_own_epsilo n 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

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
32311
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 what order the data is stored so i try both ways. unsigned char array2 = { 0x23, 0x44, 0xc2, 0xde}; float *pfloat1, *pfloat2;
6
2639
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 north latitude, precedes the degree value, e.i. -96° 59' 59". The float type is capable of carrying a signed ZERO for the purpose of representing between 0 and 1 degree west or south. My problem comes from trying to select the correct...
54
8345
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 creates a big hole in a 32-bit timer routine I wrote. Questions. 1. Why does this happen? 2. Is there C macros/functions I can call to tell me when two non-zero numbers are multiplied and the
16
2549
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 is this happening, and how do I get rid of it? (using Dev-C++
6
7607
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, Karthi
40
2159
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 errors in small snippets of code but I was wondering if anyone could give me tips on what kind of things I should be looking out for. The one area I dont feel confident in is how to declare arrays of pointers and initialising multi-dimensional arrays....
116
35836
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 problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going haywire. So my question is, given this code: int main() { float f = 59.89F;
45
5278
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 multiple it by 1000 to get 11111 and the preform bitwise like <<2 to get 88888 and then divide by 1000 to go back to float 8.8888. but these seem like "nasty" way to do it. So maybe some of you have great tips? Thank you in advance! L R
0
8435
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8768
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7368
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5655
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4181
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
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 we have to send another system

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.