Jump to content

Dividing by zero problem


 Share

Recommended Posts

I'm trying to write a program that, given two points, each of which is a solution for 2 linear equations, draws a line that includes both points.

Points are represented in 2-element arrays:

dim $point[2]=[$xValue,$yValue]

Equations are represented in point-slope form (a.k.a. y-y1=m(x-x1)) in 3-element arrays:

dim $equation[3]=[$xValue,$yValue,$slope]

To make an equation, I need to find the slope. Here's my slope-calculating function, which accepts 2 points as its arguments:

func calcSlope($p1,$p2)
    $slope=($p1[1]-$p2[1])/($p1[0]-$p2[0])
    return $slope
EndFunc

My problem is that sometimes the line between the two points is vertical, which means that the slope is some value divided by zero. But if $p1[0]-$p2[0] = 0, then $slope is undefined.

I thought about working around this by defining slope as a ratio instead of a decimal, e.g.

dim $slope[2]=[$deltaY,$deltaX]

but that isn't good enough, because I'll eventually need to change slopes into decimals so that I can compare them, e.g. to check if two equations represent the same line.

So, my basic question: what is the best way to handle division by zero, so that a graphing program can include vertical lines?

Link to comment
Share on other sites

You will have to put an If statement in to check if one of the values is zero, then draw a vertical line accordingly.

I don't think it matters if one of them is zero, just the difference. Maybe:
func calcSlope($p1,$p2)
    If $p1[0] - $p2[0] = 0 Then Return 1
    Return ($p1[1]-$p2[1])/($p1[0]-$p2[0])
EndFunc

Also, isn't the slope of a vertical line 1 and a horizontal line 0? (I'm too old to remember high school algebra!) Anyway, that was why I made the return 1, so I could be wrong there.

:P

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I don't think it matters if one of them is zero, just the difference. Maybe:

func calcSlope($p1,$p2)
    If $p1[0] - $p2[0] = 0 Then Return 1
    Return ($p1[1]-$p2[1])/($p1[0]-$p2[0])
EndFunc

Also, isn't the slope of a vertical line 1 and a horizontal line 0? (I'm too old to remember high school algebra!) Anyway, that was why I made the return 1, so I could be wrong there.

:P

Nope :P

Slope of a vertical line is undefined. a slope of 1 just makes a diagonal line (Up 1 - Right 1)

Link to comment
Share on other sites

Nope :P

Slope of a vertical line is undefined. a slope of 1 just makes a diagonal line (Up 1 - Right 1)

You're right, and that makes more sense! So this would be better:
func calcSlope($p1,$p2)
    If $p1[0] - $p2[0] = 0 Then Return SetError(1); @error = 1 for vertical line (undefined slope)
    Return ($p1[1]-$p2[1])/($p1[0]-$p2[0])
EndFunc

:P

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Actually you should return 0, which would result in a vertical line, without any error. The slope is not undefined, it is defined as zero by the definition of the slope.

In the beginning there was nothing and then even that exploded - anonymous

Link to comment
Share on other sites

Actually you should return 0, which would result in a vertical line, without any error. The slope is not undefined, it is defined as zero by the definition of the slope.

Nope, Paulie called it. Quoting Wikipedia (which would have saved me a stupid post if checked earlier):

The larger the absolute value of a slope, the steeper the line. A horizontal line has slope 0, a 45° rising line has a slope of +1, and a 45° falling line has a slope of -1. A vertical line's slope is undefined meaning it has "no slope."

:P

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...