Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > check for divisor equal to zero if when handling floats?

Reply
Thread Tools

check for divisor equal to zero if when handling floats?

 
 
Giff
Guest
Posts: n/a
 
      05-15-2007
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
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      05-15-2007
"Giff" <(E-Mail Removed)> wrote in message
news:f2bpk9$2ri$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      05-15-2007
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.
 
Reply With Quote
 
Giff
Guest
Posts: n/a
 
      05-15-2007
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.
 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      05-15-2007
On Tue, 15 May 2007 01:20:00 -0700, Jim Langston wrote:

> "Giff" <(E-Mail Removed)> wrote in message
> news:f2bpk9$2ri$(E-Mail Removed)...
>> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Internet Sharing: Equal upload speeds but un-equal download speeds =?Utf-8?B?TkpU?= Wireless Networking 3 09-15-2007 06:22 AM
Greatest Common Divisor using Extended Euclid's Algorithm sdlt85@gmail.com C Programming 4 05-30-2007 06:02 PM
Greatest Common Divisor using Extended Euclid's Algorithm sdlt85@gmail.com C++ 2 05-30-2007 09:27 AM
Greatest Common Divisor using Extended Euclid's Algorithm sdlt85@gmail.com C Programming 0 05-30-2007 04:03 AM
4 bit divisor with flip-flop ? eric VHDL 15 02-05-2004 03:31 AM



Advertisments