Skip to content
Newer
Older
100644 61 lines (53 sloc) 1.81 KB
a37d91b @crazedpsyc convert another batch to new API
crazedpsyc authored Feb 29, 2012
1 package DDG::Goodie::SigFigs;
5481cf4 @mwmiller Fix minor code layout issues.
mwmiller authored Aug 20, 2014
2 # ABSTRACT: Count the significant figures in a number.
a37d91b @crazedpsyc convert another batch to new API
crazedpsyc authored Feb 29, 2012
3
56c5ea4 @mintsoft Goodies: Enabling `use strict` on all goodies
mintsoft authored Feb 22, 2015
4 use strict;
a37d91b @crazedpsyc convert another batch to new API
crazedpsyc authored Feb 29, 2012
5 use DDG::Goodie;
b886023 @GuiltyDolphin Add support for more number systems
GuiltyDolphin authored Dec 28, 2015
6 with 'DDG::GoodieRole::NumberStyler';
a37d91b @crazedpsyc convert another batch to new API
crazedpsyc authored Feb 29, 2012
7
6e55266 @GuiltyDolphin Remove 'sf' trigger
GuiltyDolphin authored Feb 3, 2016
8 triggers any => 'sigfigs', 'sig figs', 'significant';
9d72a38 @Getty Cleanup, adding multiword trigger, checking for html encoded requirem…
Getty authored May 24, 2012
9
cbcfc30 @hunterlang apparently i can't read after 10pm
hunterlang authored Mar 21, 2012
10 zci answer_type => "sig_figs";
ca83e76 @hunterlang updated tests and goodies
hunterlang authored Mar 24, 2012
11 zci is_cached => 1;
9d72a38 @Getty Cleanup, adding multiword trigger, checking for html encoded requirem…
Getty authored May 24, 2012
12
6511199 @GuiltyDolphin Improve general handling of significant figures
GuiltyDolphin authored Dec 28, 2015
13 sub get_sig_figs {
14 my $num = shift;
15 $num =~ s/^[-+]+//;
16 # Leading digits NEVER contribute towards significant figures.
17 $num =~ s/^0+//;
18 $num =~ /^(?<int_part>\d*+)(\.(?<frac_part>\d*))?$/;
19 my $int_part = $+{'int_part'};
20 my $frac_part = $+{'frac_part'};
21 my $sigfigs = length $int_part;
22 if (defined $frac_part) {
23 # Leading zeros after decimal point aren't significant if there
24 # was no integer part or the integer part consisted of only zeros.
25 $frac_part =~ s/^0+// if $sigfigs == 0;
26 return $sigfigs + length $frac_part;
27 };
28 # This isn't necessarily correct - significant figures can be
29 # ambiguous when not using scientific notation.
30 $int_part =~ s/0+$//;
31 return length $int_part;
32 }
33
0a5d01e @GuiltyDolphin Improve triggers
GuiltyDolphin authored Dec 28, 2015
34 my $number_re = number_style_regex();
35
36 handle query_raw => sub {
b886023 @GuiltyDolphin Add support for more number systems
GuiltyDolphin authored Dec 28, 2015
37 my $query = $_;
6e55266 @GuiltyDolphin Remove 'sf' trigger
GuiltyDolphin authored Feb 3, 2016
38 $query =~ s/.*?(sig(nificant)? ?(fig(ure)?|digit)s)[^,.\d]*+//i;
6511199 @GuiltyDolphin Improve general handling of significant figures
GuiltyDolphin authored Dec 28, 2015
39 return if $query eq '';
1203c78 @GuiltyDolphin Fix triggering with non-numbers
GuiltyDolphin authored Feb 3, 2016
40 $query =~ /^($number_re)\??$/ or return;
0a5d01e @GuiltyDolphin Improve triggers
GuiltyDolphin authored Dec 28, 2015
41 my $number_match = $1;
42 my $style = number_style_for($number_match);
b886023 @GuiltyDolphin Add support for more number systems
GuiltyDolphin authored Dec 28, 2015
43 return unless $style;
0a5d01e @GuiltyDolphin Improve triggers
GuiltyDolphin authored Dec 28, 2015
44 my $formatted_input = $style->for_display($number_match);
45 my $to_compute = $style->for_computation($number_match);
6511199 @GuiltyDolphin Improve general handling of significant figures
GuiltyDolphin authored Dec 28, 2015
46 my $sigfigs = get_sig_figs $to_compute;
47 return unless defined $sigfigs;
48
49 return $sigfigs, structured_answer => {
85ec54e @GuiltyDolphin Update answer to use text template
GuiltyDolphin authored Dec 27, 2015
50 data => {
6511199 @GuiltyDolphin Improve general handling of significant figures
GuiltyDolphin authored Dec 28, 2015
51 title => "$sigfigs",
8008d40 @GuiltyDolphin Update formatted input
GuiltyDolphin authored Jan 2, 2016
52 subtitle => "Number of Significant Figures in $formatted_input",
85ec54e @GuiltyDolphin Update answer to use text template
GuiltyDolphin authored Dec 27, 2015
53 },
54 templates => {
55 group => 'text',
56 moreAt => 0,
57 },
58 };
a37d91b @crazedpsyc convert another batch to new API
crazedpsyc authored Feb 29, 2012
59 };
60 1;
Something went wrong with that request. Please try again.