Although you use strict, you don't use warnings. If you had you'd have received a slew of 'Use of uninitialized value' warnings.
However, there are a bundle of problems and areas for improvement from using lexical file handles and three parameter open to processing input data and checking parameters. The following addresses those issues:
use strict; use warnings; my $data = <<DATA; 4,8,3 2,2,3 -1,2,8 DATA open my $inFile, '<', \$data or die "File open failed: $!\n"; my @temp = map {[split ',']} <$inFile>; close $inFile; for my $values (@temp) { my $quadratic = quadratic (@$values); if (! defined $quadratic) { printf "a = %f, b = %f, c = %f: No solution\n", @$values; next; } printf "a = %f, b = %f, c = %f: %f\n", @$values, $quadratic; } sub quadratic { my ($a, $b, $c) = @_; my $root = $b*$b - 4*$a * $c; return if ! $a; return if $root < 0; return -($b + (0 < $b ? 1 : -1) * sqrt($root)) / 2*$a; }
Prints:
a = 4.000000, b = 8.000000, c = 3.000000: -24.000000 a = 2.000000, b = 2.000000, c = 3.000000: No solution a = -1.000000, b = 2.000000, c = 8.000000: 4.000000
In reply to Re: solving a quadratic equation
by GrandFather
in thread solving a quadratic equation
by vis1982
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |