C: Dividing a double by a double outputs lnf
http://pa开发者_如何学JAVAstie.org/752941
When I divide dy/dx it returns lnf. Please tell me what this stands for and how I can find the actual result of dy/dx. Thanks.
When you divide by zero (or a small enough number), there is no way to represent the result in floating point notation. In that case you get one of the infinities (+Inf or -Inf).
As an example, if you already have the largest number that can be represented by IEEE754 doubles, and you divide it by 0.1 (i.e., multiply it by 10), you'll get +Inf.
IEEE754 allocates some of the bit patterns to special values like +/- infinity and NaN (not a number).
I long ago stopped trying to represent lines as a (gradient,y-intersect) pair on computers (it's okay on paper since you can calculate with infinities in your head). You can avoid all the hassles of infinities by simply representing lines as (x1,y1,x2,y2) 4-tuples - you'll find algorithms that work quite well for all gradients if you represent your line segments that way.
Otherwise, you'll always need special handling for near-vertical lines that cause overflow when calculating the gradient.
Inf = Infinity. You get this because dx is 0 and hence you divide by 0. This may be valid - e.g. a vertical line.
Check to make sure that dx has the value you want before calculating the slope.
精彩评论