473,785 Members | 2,325 Online
Bytes | Software Development & Data Engineering Community
+ 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 3234
"Giff" <gi******@gmail .com.invalidwro te 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.0000000000000 01 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.invalidwro te 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
4182
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 equal 59001.31. Change each side of the equation by a hundreth or two and it checks. Change it a bit more, and it won't. _What_ is going on here?! <?php
26
26228
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 know it's danger to initialize NULL pointers this way. But how about floats? Zhang Le
32
4120
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 ); } int main() { std::deque<double> pt ;
17
4534
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 selected into 2 different SP variables and compared for equal. They are both NULL, but do not compare as equal. When the Not NULL columns (SALARY) are compared, they do compare as equal.
18
3332
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% centerColumn use what's left I'm currently defining centerColumn as a %, but to get this to work properly the margin, padding and border settings have to be exactly the
6
3398
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 never really about MISRA, but rather the one's complement representation of integer constant 0 and now how to zero memory portably, I am bringing the topic to this group. Try to ignore CBF's errors in the 1's complement -1 and sign magnitude -1...
2
3677
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 adding them it will give us the greatest common divisor and I need to use the extended Euclidean algorithm. I already have the code for the greatest common divisor but I dont know how to do the linear combination.
7
1859
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
2675
by: Bernard.Mangay | last post by:
The remainder is non zero due to rounding errors. How can I remove the rounding errors?
0
9645
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
9481
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
10341
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10095
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6741
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
5383
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...
1
4054
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
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.