CalcRoot.pm: fixed check if answer is a whole number / CalcRoot.t: updated test file to structured answers

master
kfloey 2015-02-26 17:12:43 +00:00
parent a009a9fe55
commit 515ee8a85b
2 changed files with 43 additions and 21 deletions

View File

@ -96,17 +96,18 @@ handle query => sub {
# Try and simplify the radical
my $count = int(abs($calc));
unless(($base - int($base))==0){
while ($count > 1) {
while ($count > 1) {
# See if the current number raised to the given exponent is a factor of our base. If it is, we can give them a simplified version of the radical in addition to the answer.
my $newBase = $base / ($count ** $exp);
# See if the current number raised to the given exponent is a factor of our base. If it is, we can give them a simplified version of the radical in addition to the answer.
my $newBase = $base / ($count ** $exp);
if ( ($newBase - int($newBase)) == 0) {
return structured($exp,"-$base","The $exp-root of -$base is $calc (-$count times the $exp-root of $newBase).", qq|<sup>$exp</sup>&radic;-$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a> (-$count&sdot;<sup>$exp</sup>&radic;$newBase)|);
}
if ( ($newBase - int($newBase)) == 0 and $newBase != 1) {
return structured($exp,"-$base","The $exp-root of -$base is $calc (-$count times the $exp-root of $newBase).", qq|<sup>$exp</sup>&radic;-$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a> (-$count&sdot;<sup>$exp</sup>&radic;$newBase)|);
$count--;
}
$count--;
}
return structured($exp,"-$base","The $exp-root of -$base is $calc.", qq|<sup>$exp</sup>&radic;-$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a>|);
}
@ -121,17 +122,20 @@ handle query => sub {
# Try and simplify the radical
my $count = int($calc);
#If the answer is not a whole number, try to simplify the radical
unless(($base - int($base))==0){
while ($count > 1) {
while ($count > 1) {
# See if the current number raised to the given exponent is a factor of our base. If it is, we can give them a simplified version of the radical in addition to the answer.
my $newBase = $base / ($count ** $exp);
# See if the current number raised to the given exponent is a factor of our base. If it is, we can give them a simplified version of the radical in addition to the answer.
my $newBase = $base / ($count ** $exp);
if ( ($newBase - int($newBase)) == 0) {
return structured($exp,$base,"The $exp-root of $base is $calc ($count times the $exp-root of $newBase).", qq|<sup>$exp</sup>&radic;$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a> ($count&sdot;<sup>$exp</sup>&radic;$newBase)|);
}
if ( ($newBase - int($newBase)) == 0 and $newBase != 1) {
return structured($exp,$base,"The $exp-root of $base is $calc ($count times the $exp-root of $newBase).", qq|<sup>$exp</sup>&radic;$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a> ($count&sdot;<sup>$exp</sup>&radic;$newBase)|);
$count--;
}
$count--;
}
return structured($exp,$base,"The $exp-root of $base is $calc.", qq|<sup>$exp</sup>&radic;$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a>|);
}
@ -142,11 +146,13 @@ handle query => sub {
sub structured{
my($exp,$base,$text, $html) = @_;
return $text, structured_answer => {
input => ["$exp-root of $base"],
operation => 'Calculate',
result => $html,
};
return $text,
heading => "Root Calculator",
structured_answer => {
input => ["$exp-root of $base"],
operation => 'Calculate',
result => $html,
};
}
1;

View File

@ -12,7 +12,23 @@ ddg_goodie_test(
[qw(
DDG::Goodie::CalcRoots
)],
'2nd root of 100' => test_zci("The 2-root of 100 is 10 (10 times the 2-root of 1).", html => qq(<sup>2</sup>&radic;100 = <a href="javascript:;" onclick="document.x.q.value='10';document.x.q.focus();">10</a> (10&sdot;<sup>2</sup>&radic;1))),
'cube root of 33' => test_zci("The 3-root of 33 is 3.20753432999583.", html => qq(<sup>3</sup>&radic;33 = <a href="javascript:;" onclick="document.x.q.value='3.20753432999583';document.x.q.focus();">3.20753432999583</a>))
'2nd root of 100' => test_zci(
"The 2-root of 100 is 10.",
heading => "Root Calculator",
structured_answer => {
input => ["2-root of 100"],
operation => 'Calculate',
result => qq|<sup>2</sup>&radic;100 = <a href="javascript:;" onclick="document.x.q.value='10';document.x.q.focus();">10</a>|
}
),
'cube root of 33' => test_zci(
"The 3-root of 33 is 3.20753432999583.",
heading => "Root Calculator",
structured_answer => {
input => ["3-root of 33"],
operation => 'Calculate',
result => qq|<sup>3</sup>&radic;33 = <a href="javascript:;" onclick="document.x.q.value='3.20753432999583';document.x.q.focus();">3.20753432999583</a>|
}
),
);
done_testing;