I searched for (but didn't find) a way to get the human "how many?" function, so I wrote:
#!/usr/bin/perl # fuzzy-size.pl # = Copyright 2010 Xiong Changnian <xiong@sf-id.com> = # = Free Software = Artistic License 2.0 = NO WARRANTY = use strict; use warnings; use feature qw( say ); # "How many apples are in the box?" # This is an ordinary question asked by ordinary people every day. # It's not the same as "Count the apples in the box." # A rough estimate will be fine. # # fuzz() returns 0 for 0 and the exact number for 1, 2, or 3. # It returns 3 ("a few") for 4 also and 6 ("half dozen") for 5..7; # and so forth for increasing magnitudes. # fuzz() returns "the same" (negated) values for negative integers; # we sometimes ask "How many do we need" as well. # But fractions are ignored; you don't count broken apples. # my @raw = ( -768, -9, -3, -2, -1, -0.5, 0, 0.005, 0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 100, 768, 18433, 633999, ); my $in ; my $out ; foreach $in ( @raw ) { $out = fuzz( $in ); say $in, qq{\t}, $out; }; sub fuzz { my $in = int shift; # no floats here, move + along my $out ; return 0 if ( $in == 0 ); my $negative = ( $in < 0 ); # store algebraic sign $in = abs $in; $out = int sqrt exp ( (int log $in**2) + 0.7 ); if ( $negative ) { $out = 0 - $out; }; # restore sign return $out; }; __DATA__ Output: -768 -943 -9 -10 -3 -3 -2 -2 -1 -1 -0.5 0 0 0 0.005 0 0.5 0 1 1 2 2 3 3 4 3 5 6 6 6 7 6 8 10 9 10 10 10 11 10 12 10 13 17 14 17 15 17 16 17 17 17 18 17 19 17 20 17 21 28 23 28 100 127 768 943 18433 18958 633999 627814 __END__
In reply to Fuzzy How-Many by Xiong
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |