Jun 1, 2011, 8:42 PM
Post #7 of 7
Your solution does work, but there are several reasons to prefer the subroutine solution.
Re: [alwayslooking20] Cube root and negatives
[In reply to]
Probably the most important is that it is self documenting. It makes it very clear to any reader that you intend to take a cube root.
When you get around to my final suggestion, you will discover that we both forgot about the newline on the number. We both got away with it for now, but it is poor practice to do arithmetic with anything but pure numbers.
In the future, you may find a faster or more accurate algorithm. Change the function and you have made the change everywhere that it is used.
A function can, and should, be tested independent of the rest of the program. Do you have any idea what your program would (or even should) do if there is a blank line in the file. The function should throw an exception (die) when it finds any non-numeric argument.
You may find a well tested cube-root function on CPAN. It is a trivial change to replace the call to one function with a call to a better one.