I am quite new to the world of Perl and I am stuck with the sqrt function. By stuck I mean the function is not returning the value it should.

After reading a text file with coordinate information, 8 values are stored in separate variables ($x1, $y1, $x2, $y2 and so forth). Then, a subroutine is called, which calculates the distance between the points and then other things. However, it doesn't do what it is supposed to do because the results of the sqrt function are not the ones they should! I thought it was a problem with how the variables were obtained and stored, but after performing the sqrt with the literal values, it also produces a wrong number.

Here are the values

    -2130.07 207.56  -2084.46 210.76  -1892.78 -2525.74  -1938.39 -2528.93

And here are the sqrt calculations...

    $side1=sqrt(($x1-$x2)^2+($y1-$y2)^2);
    $sidecheck=sqrt((-2130.07-(-2084.46))^2+(207.56-210.76)^2);

Both $side1 and $sidecheck return a value of 6.7823 instead of 45.722.

Is there a way to sort this out? Thanks!

有帮助吗?

解决方案

In Perl and few other Languages, the power of a number is not the caret, its a double asterisk. So you need to write

$sidecheck=sqrt((-2130.07-(-2084.46))**2+(207.56-210.76)**2);

The ^ is the bitwise XOR operator. To square a value, use **

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top