For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2007 > line-equation program gives wrong results









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author line-equation program gives wrong results
Camotito

2007-10-23, 10:00 pm

Hello!

I was just implementing this small program for calculating the line-
equation, given two points.
Just in case you don't remind this equation.... y = slope * x + b.
First I calculate the slope and then b:

$slope = ($ARGV[1] - $ARGV[3]) / ($ARGV[0] - $ARGV[2]);
$b = $ARGV[1] - ($ARGV[0] * $slope);
print "\n$ARGV[1] = $slope * $ARGV[0] + $b\n";

For this input :

perl my_program 16.81 16.57 0 0

It gives me this result :

16.57 = 0.985722784057109 * 16.81 + 0

Clearly b can be 0 only if the slope is 1. I don't know the reason for
this result (I guess precission but that's the only thing I could
say), neither how to solve it.
Please what should I do to get the correct result?

Thank you

camotito

Lawrence Statton

2007-10-23, 10:00 pm



>
> $slope = ($ARGV[1] - $ARGV[3]) / ($ARGV[0] - $ARGV[2]);
> $b = $ARGV[1] - ($ARGV[0] * $slope);
> print "\n$ARGV[1] = $slope * $ARGV[0] + $b\n";
>
> For this input :
>
> perl my_program 16.81 16.57 0 0
>
> It gives me this result :
>
> 16.57 = 0.985722784057109 * 16.81 + 0
>
> Clearly b can be 0 only if the slope is 1.


Not at all clear to me ... Since one of the given points is at the
origin, then the intercept *MUST* be zero.

> I don't know the reason for
> this result (I guess precission but that's the only thing I could
> say), neither how to solve it.
> Please what should I do to get the correct result?
>
> Thank you
>
> camotito
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>


Tom Phoenix

2007-10-23, 10:00 pm

On 10/23/07, camotito <camotito@gmail.com> wrote:

> I was just implementing this small program for calculating the line-
> equation, given two points.
> Just in case you don't remind this equation.... y = slope * x + b.
> First I calculate the slope and then b:
>
> $slope = ($ARGV[1] - $ARGV[3]) / ($ARGV[0] - $ARGV[2]);
> $b = $ARGV[1] - ($ARGV[0] * $slope);
> print "\n$ARGV[1] = $slope * $ARGV[0] + $b\n";


It's easier to read, write, and maintain code that uses names instead
of indices. So I'd re-write your code so far like this:

use strict;
use warnings;
# @ARGV holds two points (two xy pairs)
my($x1, $y1, $x2, $y2) = @ARGV;
my $slope = ($y1 - $y2) / ($x1 - $x2);
my $b = $y1 - ($x1 * $slope);
print "\n";
print "$y1 = $slope * $x1 + $b\n";

> For this input :
>
> perl my_program 16.81 16.57 0 0
>
> It gives me this result :
>
> 16.57 = 0.985722784057109 * 16.81 + 0
>
> Clearly b can be 0 only if the slope is 1.


One must pay special attention to those points a mathematician
describes as "clear". :-) You must have meant to say, clearly, b can
be 0 only if the line passes through the origin. But check that sort
of detail with your algebra teacher or in a mathematics forum; perl is
doing what you're asking of it.

Cheers!

--Tom Phoenix
Stonehenge Perl Training
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com