473,466 Members | 1,376 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

check for divisor equal to zero if when handling floats?

Hi,

I have a function that takes in a float and then performs a division.
Since I know that it is impossible to check if the value I pass is equal
to zero (being a float), I don't perform any check on that value. Now I
doubt if this is a safe approach and ask for your advice.

Thanks a lot in advance.

/G
May 15 '07 #1
4 3218
"Giff" <gi******@gmail.com.invalidwrote in message
news:f2**********@aioe.org...
Hi,

I have a function that takes in a float and then performs a division.
Since I know that it is impossible to check if the value I pass is equal
to zero (being a float), I don't perform any check on that value. Now I
doubt if this is a safe approach and ask for your advice.

Thanks a lot in advance.
On my platform this will produce NaN (Not a Number). Although it's
difficult to test for NaN.
But, you can check if it's 0.0f because anything else won't be 0. I.E.
0.000000000000001 is not 0 and can be a divisor.
May 15 '07 #2
Giff wrote:
Since I know that it is impossible to check if the value I pass is equal
to zero (being a float), I don't perform any check on that value.
What do you mean it's "impossible"? Of course it's not impossible.

I think you have understood the nature of floating point values
wrongly. Floating point values are not some kind of fuzzy quantum
states which you can't really measure with accuracy and which hover
erratically around some value, never being exactly anything.

Floating point values are a deterministic and well-defined group of
bits. You might not be able to represent all possible rational values
with them (such as 0.1) but that doesn't mean you can't represent any
values with exact accuracy. For example integers can be represented
with exact accuracy up to a rather big value (to something like 2^51
or such when using double-precision floating point, a fact which often
surprises many people).

Thus this should work just fine:

double d = 0;
if(d == 0) { it's 0 }

Of course a completely different story is whether certain mathematical
expressions which should give 0 as result really give that, or if
rounding errors kick in along the way making the result just slightly
off target.
For example (0.5+0.5-1) should give exactly 0, but (0.9+0.1-1) might
not (because 0.9 and 0.1 cannot be represented accurately).

And then there's the issue, of course, that dividing by a value which
is very close to 0 (even if it's not supposed to be exactly 0 in the
first place) might give a result which is too large.

What you can do is to check if the value is very close to 0 and
act accordingly.
May 15 '07 #3
Juha Nieminen ha scritto:
What you can do is to check if the value is very close to 0 and
act accordingly.
Thanks very much indeed to both. Surely I was a bit confused.
May 15 '07 #4
On Tue, 15 May 2007 01:20:00 -0700, Jim Langston wrote:
"Giff" <gi******@gmail.com.invalidwrote in message
news:f2**********@aioe.org...
>Hi,

I have a function that takes in a float and then performs a division.
Since I know that it is impossible to check if the value I pass is
equal to zero (being a float), I don't perform any check on that value.
Now I doubt if this is a safe approach and ask for your advice.

Thanks a lot in advance.

On my platform this will produce NaN (Not a Number). Although it's
difficult to test for NaN.
ISO C99 supplies the isnan() function.

--
Lionel B
May 15 '07 #5

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

Similar topics

17
by: Craig Bailey | last post by:
Someone please explain what alternate universe I fell into this afternoon when PHP started telling me that 2 doesn't equal 2. Not sure about you, but when I run this, it tells me 59001.31 doesn't...
26
by: 69dbb24b2db3daad932c457cccfd6 | last post by:
Hello, I have to initialize all elements of a very big float point array to zero. It seems memset(a, 0, len) is faster than a simple loop. I just want to know whether it is safe to do so, since I...
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
17
by: Mark A | last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP 11). Using the SAMPLE database, tables EMP and EMLOYEE. In the followng stored procedure, 2 NULL columns (COMM) are...
18
by: zzpat | last post by:
I've come to the conclusion that float was never made for designing a page, but is there a good way to float three columns with the following characteristics? leftColumn 20% rightColumn 20%...
6
by: Dan Henry | last post by:
I need a sanity check. The following is an exchange on comp.arch.embedded with CBFalconer in a rather long MISRA related thread. Since my little section of that thread and area of interest was...
2
by: sdlt85 | last post by:
Hi, Can someone help me with an idea on how to start writing a C++ code for generating greatest common divisor and the linear combination of two intergers represented as gcd(m, n)= mx + ny and...
7
by: jamaicaboy | last post by:
This is the code that i need some help with........... #include<iostream.h> #include<iomanip.h> class test { public: void store( int);
11
by: Bernard.Mangay | last post by:
The remainder is non zero due to rounding errors. How can I remove the rounding errors?
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...
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,...
0
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...
0
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,...
0
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.