no error on float division by zero? (vc6)

Started by
13 comments, last by wamingo 20 years, 3 months ago
I fell over a bug in my code and thought that was strange because the program ran flawlessly as if there was no bug... I was diving an integer by a float which at a certain stage equalled zero. I never thought of it any further because it never made an error or warning or anything... If you divide an integer by 0 stored in a float it errors out... but if you divide by a float it does not? eg: int A=0; float B=0; A = int( 10 / B ); this compiles and runs flawlessly in vc++6.0 but is it okay or is it bad practice? And more specifcly how come it doesn''t error out? thanks.
Advertisement
division by zero with integers makes your program crash, but if you do it with floating point numbers, the result will be some special floating point number called nan (not a number). I guess if you convert a nan to an int, it results in 0.

My Site
yes it equals zero.
but is it okay practice to do this?
I''d love if it was fine because then I don''t have to make any if-div0 on my handling of screen size proportion which is where I found my "bug".
but on the other hand I don''t wanna get some bad programming habbit either...
Why would you WANT to do it?

RM.

PS: And I would say (although I could be wrong) that accomodating known bugs is bad programming.
------------------------------------------------------------Yes. it''s true, I DO wield the ugly stick. And I see I have beaten you with it before!
They say something about this in msdn. There is a way to have it so floats call up exceptions, the default is that they don't do anyhting when they have dividing or... forgot the other one... errors.

[edited by - Newfound Ajarn on January 15, 2004 6:52:03 PM]
VLAjarn: Cause it (linux) NEVER crashesMrPr0Grmer: lol ur wrongMrPr0Grmer: RedHat crashesVLAjarn: I'm saying good builds of linux
Ajarn, I looked really hard but couldn''t find anything :/ But thanks though.

RM, I don''t WANT to divide by zero as such it''s just that in the example above ''B'' "sometimes" equals 0; for instance if you wanted to divide by ANY pixel coordinate, well, pixels 0,0 is also a valid cordinate and so I ask whether I should be bothered with it or not... I just feel it''s good to know these things because then I stop worrying about it and do whatever is common practice.
It would be great if you could choose what 8/0 ment, either 8 0 or error.

new CPU thing
It doesn''t error out explicitly, but you can catch it. Try this:
float a = 0.ffloat b = 6 / a;if (b != b){// NaN, float division by 0}


It''s an interesting property. After performing the math on b, the result is NaN (not a number). A NaN, when tested for equality, is not even equal it itself, that''s why you can use the if conditional like that.
hehe thats pretty funny.

didnt know that.

Lazzar
---------------------------------------------------------if god gave us the source code, we could change the world!
If you run fallenang3l's code in .NET 2003, b = 1.#INF, or positive infinity, non NaN. This makes me wonder about three things. One, why was it changed from VC++6 to VC++7, since (at least to me) it makes more sense to have NaN returned for an undefined result rather than positive infinity? Two, why is a hardware exception not thrown, as is the case with integer division by 0? Third, if you search through MSDN, there's a section on C++ Multiplicative Operators which says "Division by 0 in either a division or a modulus expression is undefined and causes a run-time error." Not only is the result here not undefined (it's actually positive infinity), but it does not cause a run-time error. Apparently, Microsoft sometimes doesn't even conform to it's own documentation--kind of like b != b, eh?

[edited by - iamjoesname on January 18, 2004 1:45:04 AM]

This topic is closed to new replies.

Advertisement