]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/perf/perldriver/Format.pm
2 # ********************************************************************
4 # * Copyright (c) 2002, International Business Machines Corporation and
5 # * others. All Rights Reserved.
6 # ********************************************************************
8 my $PLUS_MINUS = "±";
10 #|#---------------------------------------------------------------------
11 #|# Format a confidence interval, as given by a Dataset. Output is as
13 #|# 241.23 - 241.98 => 241.5 +/- 0.3
14 #|# 241.2 - 243.8 => 242 +/- 1
15 #|# 211.0 - 241.0 => 226 +/- 15 or? 230 +/- 20
16 #|# 220.3 - 234.3 => 227 +/- 7
17 #|# 220.3 - 300.3 => 260 +/- 40
18 #|# 220.3 - 1000 => 610 +/- 390 or? 600 +/- 400
19 #|# 0.022 - 0.024 => 0.023 +/- 0.001
20 #|# 0.022 - 0.032 => 0.027 +/- 0.005
21 #|# 0.022 - 1.000 => 0.5 +/- 0.5
22 #|# In other words, take one significant digit of the error value and
23 #|# display the mean to the same precision.
26 #| my $lower = $ds->getMean() - $ds->getError();
27 #| my $upper = $ds->getMean() + $ds->getError();
29 #| # Find how many initial digits are the same
30 #| while ($lower < 1 ||
31 #| int($lower) == int($upper)) {
36 #| while ($lower >= 10 &&
37 #| int($lower) == int($upper)) {
44 #---------------------------------------------------------------------
45 # Format a number, optionally with a +/- delta, to n significant
48 # @param significant digit, a value >= 1
50 # @param time in seconds to be formatted
51 # @optional delta in seconds
53 # @return string of the form "23" or "23 +/- 10".
59 my $delta = shift; # may be undef
61 my $result = formatSigDig
($sigdig, $a*$mult);
62 if (defined($delta)) {
63 my $d = formatSigDig
($sigdig, $delta*$mult);
64 # restrict PRECISION of delta to that of main number
65 if ($result =~ /\.(\d+)/) {
66 # TODO make this work for values with all significant
67 # digits to the left of the decimal, e.g., 1234000.
69 # TODO the other thing wrong with this is that it
70 # isn't rounding the $delta properly. Have to put
71 # this logic into formatSigDig().
73 $d =~ s/\.(\d{$x})\d+/.$1/;
75 $result .= " $PLUS_MINUS " . $d;
80 #---------------------------------------------------------------------
81 # Format a time, optionally with a +/- delta, to n significant
84 # @param significant digit, a value >= 1
85 # @param time in seconds to be formatted
86 # @optional delta in seconds
88 # @return string of the form "23 ms" or "23 +/- 10 ms".
93 my $delta = shift; # may be undef
95 my @MULT = (1 , 1e3, 1e6, 1e9);
96 my @SUFF = ('s' , 'ms', 'us', 'ns');
100 #always do seconds if the following line is commented out
101 ++$i while ($a*$MULT[$i] < 1 && $i < @MULT);
103 formatNumber
($sigdig, $MULT[$i], $a, $delta) . ' ' . $SUFF[$i];
106 #---------------------------------------------------------------------
107 # Format a percentage, optionally with a +/- delta, to n significant
110 # @param significant digit, a value >= 1
111 # @param value to be formatted, as a fraction, e.g. 0.5 for 50%
112 # @optional delta, as a fraction
114 # @return string of the form "23 %" or "23 +/- 10 %".
119 my $delta = shift; # may be undef
121 formatNumber
($sigdig, 100, $a, $delta) . '%';
124 #---------------------------------------------------------------------
125 # Format a number to n significant digits without using exponential
128 # @param significant digit, a value >= 1
129 # @param number to be formatted
131 # @return string of the form "1234" "12.34" or "0.001234". If
132 # number was negative, prefixed by '-'.
138 local $_ = sprintf("%.${n}e", $a);
139 my $sign = (s/^-//) ? '-' : '';
143 if (/^(\d)\.(\d+)e([-+]\d+)$/) {
144 my ($d, $dn, $e) = ($1, $2, $3);
148 $d .= '0' while ($e > length($d));
153 if ($e == length($d)) {
154 $result = $sign . $d;
156 $result = $sign . substr($d, 0, $e) . '.' . substr($d, $e);
159 die "Can't parse $_";