Calculator: Fixes up trig functions. (#4339)
* Fixes up trig functions. Replace regex. * Fixed 1/(x) issues. * Update dodgy lookahead.master
parent
8e5cd3f431
commit
af0a70222d
|
@ -19,6 +19,7 @@ triggers query => qr'^
|
|||
what\sis| calculat(e|or) | solve | math | log\sof | fact(?:orial?)?(\s+of)? |
|
||||
times | mult | multiply | divided\sby | plus | minus | cos | tau |
|
||||
sin | tan | cotan | log | ln | exp | tanh |
|
||||
deg(?:rees?)? | rad(?:ians?)? |
|
||||
sec | csc | squared | sqrt | \d+\s?mod(?:ulo)?\s?\d+ | gross | dozen | pi |
|
||||
score){2,}$
|
||||
'xi;
|
||||
|
@ -136,6 +137,12 @@ sub rewriteFunctions {
|
|||
$query =~ s/log\s?(\d+)/log($1)/i;
|
||||
$query =~ s/ln\s?(\d+)/ln($1)/i;
|
||||
|
||||
# Preprocesses Trig functions
|
||||
$query =~ s/(deg)rees?/$1/ig;
|
||||
$query =~ s/rad(?:ians?)?//ig;
|
||||
|
||||
$query =~ s/(sin|cos|tan)\s?(\d+(?: deg)?)/$1($2)/ig;
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
|
@ -156,8 +163,9 @@ handle query => sub {
|
|||
};
|
||||
}
|
||||
|
||||
# We need to rewrite the functions for front-end consumption
|
||||
return if ( m/deg(rees?)?|°/i && m/rad(ians?)?/); # we don't support a mix of degrees and radians in the same query
|
||||
$query = rewriteFunctions($query);
|
||||
|
||||
# throw out obvious non-calculations immediately
|
||||
return if $query =~ qr/(\$(.+)?(?=£|€))|(£(.+)?(?=\$|€))|(€(.+)?(?=\$|£))/; # only let one currency type through
|
||||
return if $req->query_lc =~ /^0x/i; # hex maybe?
|
||||
|
|
|
@ -119,6 +119,7 @@ DDH.calculator = DDH.calculator || {};
|
|||
.replace(/dozen/g, '12')
|
||||
|
||||
// 7. last chance recovers
|
||||
.replace(/(1\/\(\d+\))/, '($1)')
|
||||
.replace(/<sup>□<\/sup>/g, '')
|
||||
.replace(/=/g, '')
|
||||
.replace(/\$|£|€/g, '')
|
||||
|
@ -679,7 +680,6 @@ DDH.calculator = DDH.calculator || {};
|
|||
evaluated = false;
|
||||
}
|
||||
|
||||
|
||||
usingState = true;
|
||||
|
||||
// stops first entry being and operand, unless it's a -
|
||||
|
@ -693,8 +693,10 @@ DDH.calculator = DDH.calculator || {};
|
|||
return false;
|
||||
}
|
||||
|
||||
// opens pseudo paren for 1/(x)
|
||||
if(element === "1/(") {
|
||||
// a guard and opens a psuedo paren for 1/(x)
|
||||
if(element === "1/(" && !Utils.isOperand(display.value[display.value.length-2])) {
|
||||
return false;
|
||||
} else if(element === "1/(") {
|
||||
ParenManager.incrementTotal();
|
||||
}
|
||||
|
||||
|
|
|
@ -396,6 +396,36 @@ ddg_goodie_test(
|
|||
'fact 20' => build_test(
|
||||
'20!'
|
||||
),
|
||||
|
||||
## Trig testing
|
||||
'tan 45' => build_test(
|
||||
'tan(45)'
|
||||
),
|
||||
'tan 45 deg' => build_test(
|
||||
'tan(45deg)'
|
||||
),
|
||||
'sin 12 + 1341' => build_test(
|
||||
'sin(12) + 1341'
|
||||
),
|
||||
'88 + 12341 * 123 + tan(4)' => build_test(
|
||||
'88 + 12341 * 123 + tan(4)'
|
||||
),
|
||||
'sin 2 + tan 3 - cos 10' => build_test(
|
||||
'sin(2) + tan(3) - cos(10)'
|
||||
),
|
||||
'75 + sin 75 deg' => build_test(
|
||||
'75 + sin(75deg)'
|
||||
),
|
||||
'tan(sin 88+2)' => build_test(
|
||||
'tan(sin(88) + 2)'
|
||||
),
|
||||
'cos(103*232+22)+2' => build_test(
|
||||
'cos(103 * 232 + 22) + 2'
|
||||
),
|
||||
|
||||
|
||||
'tan of 88 degrees and radians' => undef,
|
||||
'sin 88 degrees + sin 10 radians' => undef,
|
||||
'1432 / 28 2' => undef,
|
||||
'5 + 88 2' => undef,
|
||||
'14 8 - 22' => undef,
|
||||
|
|
Loading…
Reference in New Issue