Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Interchange: users

[PATCH] Expand range of round_to_frac_digits to handle exponentized numbers

 

 

Interchange users RSS feed   Index | Next | Previous | View Threaded


brian at endpoint

Sep 2, 2009, 7:15 AM

Post #1 of 4 (763 views)
Permalink
[PATCH] Expand range of round_to_frac_digits to handle exponentized numbers

---
lib/Vend/Util.pm | 20 +++++++++++++++-----
1 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/lib/Vend/Util.pm b/lib/Vend/Util.pm
index 8356b84..5be6b5a 100644
--- a/lib/Vend/Util.pm
+++ b/lib/Vend/Util.pm
@@ -269,12 +269,22 @@ sub round_to_frac_digits {
else {
$digits = 2;
}
- my @frac;
- $num =~ /^(-?)(\d*)(?:\.(\d+))?$/
- or return $num;
+ if ($num =~ /^(-?)(\d*)(?:\.(\d+)(?:e-(\d+)))?$/) {
+ # this number sufficiently close to zero for our purposes
+ return 0;
+ }
+ elsif ($num =~ /^(-?)(\d*)(?:\.(\d+))?$/) {
+ # no op
+ }
+ else {
+ warn "Vend::Util::round_to_frac_digits: invalid number ($num)\n";
+ return $num;
+ }
+
my $sign = $1 || '';
- my $int = $2;
- @frac = split(m{}, ($3 || 0));
+ my $int = $2;
+ my @frac = split(m{}, ($3 || 0));
+
local($^W) = 0;
my $frac = join "", @frac[0 .. $digits - 1];
if($frac[$digits] > 4) {
--
1.5.6.3


_______________________________________________
interchange-users mailing list
interchange-users [at] icdevgroup
http://www.icdevgroup.org/mailman/listinfo/interchange-users


mike at perusion

Sep 2, 2009, 8:15 AM

Post #2 of 4 (695 views)
Permalink
Re: [PATCH] Expand range of round_to_frac_digits to handle exponentized numbers [In reply to]

Quoting Brian J. Miller (brian [at] endpoint):
> ---
> lib/Vend/Util.pm | 20 +++++++++++++++-----
> 1 files changed, 15 insertions(+), 5 deletions(-)
>
> diff --git a/lib/Vend/Util.pm b/lib/Vend/Util.pm
> index 8356b84..5be6b5a 100644
> --- a/lib/Vend/Util.pm
> +++ b/lib/Vend/Util.pm
> @@ -269,12 +269,22 @@ sub round_to_frac_digits {
> else {
> $digits = 2;
> }
> - my @frac;
> - $num =~ /^(-?)(\d*)(?:\.(\d+))?$/
> - or return $num;
> + if ($num =~ /^(-?)(\d*)(?:\.(\d+)(?:e-(\d+)))?$/) {
> + # this number sufficiently close to zero for our purposes
> + return 0;
> + }

Don't we want to return 0.00 (or whatever the frac_digits are? What if it is -1?

> + elsif ($num =~ /^(-?)(\d*)(?:\.(\d+))?$/) {
> + # no op
> + }
> + else {
> + warn "Vend::Util::round_to_frac_digits: invalid number ($num)\n";
> + return $num;
> + }
> +
> my $sign = $1 || '';
> - my $int = $2;
> - @frac = split(m{}, ($3 || 0));
> + my $int = $2;
> + my @frac = split(m{}, ($3 || 0));
> +
> local($^W) = 0;
> my $frac = join "", @frac[0 .. $digits - 1];
> if($frac[$digits] > 4) {
> --
> 1.5.6.3
>
>
> _______________________________________________
> interchange-users mailing list
> interchange-users [at] icdevgroup
> http://www.icdevgroup.org/mailman/listinfo/interchange-users
>

--
Mike Heins
Perusion -- Expert Interchange Consulting http://www.perusion.com/
phone +1.765.328.4479 <mike [at] perusion>

I don't want to get to the end of my life and find I have just
lived the length of it. I want to have lived the width of it as
well. -- Diane Ackerman

_______________________________________________
interchange-users mailing list
interchange-users [at] icdevgroup
http://www.icdevgroup.org/mailman/listinfo/interchange-users


wiggins at danconia

Sep 2, 2009, 9:23 AM

Post #3 of 4 (688 views)
Permalink
Re: [PATCH] Expand range of round_to_frac_digits to handle exponentized numbers [In reply to]

Mike Heins wrote:
> Quoting Brian J. Miller (brian [at] endpoint):
>> ---
>> lib/Vend/Util.pm | 20 +++++++++++++++-----
>> 1 files changed, 15 insertions(+), 5 deletions(-)
>>
>> diff --git a/lib/Vend/Util.pm b/lib/Vend/Util.pm
>> index 8356b84..5be6b5a 100644
>> --- a/lib/Vend/Util.pm
>> +++ b/lib/Vend/Util.pm
>> @@ -269,12 +269,22 @@ sub round_to_frac_digits {
>> else {
>> $digits = 2;
>> }
>> - my @frac;
>> - $num =~ /^(-?)(\d*)(?:\.(\d+))?$/
>> - or return $num;
>> + if ($num =~ /^(-?)(\d*)(?:\.(\d+)(?:e-(\d+)))?$/) {
>> + # this number sufficiently close to zero for our purposes
>> + return 0;
>> + }
>
> Don't we want to return 0.00 (or whatever the frac_digits are? What if it is -1?
>

I guess it depends on the context used (aka numeric vs. string), but yes
I'd be fine with that. Do you mean if $frac_digits is -1? I'm not clear
exactly the use case for the subroutine, hence my e-mail here. Carl
Bailey also suggested that if $frac_digits is 0 there is currently a
trailing "." included which might need to be removed.

If you can provide more clarity above I'm happy to whip something up.
(I'm on early vacation for the US Labor Day starting tomorrow, so it
will probably be next week before I have something else out, but I
*will* follow up if no one beats me to it.)

>> + elsif ($num =~ /^(-?)(\d*)(?:\.(\d+))?$/) {
>> + # no op
>> + }
>> + else {
>> + warn "Vend::Util::round_to_frac_digits: invalid number ($num)\n";
>> + return $num;
>> + }
>> +
>> my $sign = $1 || '';
>> - my $int = $2;
>> - @frac = split(m{}, ($3 || 0));
>> + my $int = $2;
>> + my @frac = split(m{}, ($3 || 0));
>> +
>> local($^W) = 0;
>> my $frac = join "", @frac[0 .. $digits - 1];
>> if($frac[$digits] > 4) {
>> --
>> 1.5.6.3
>>
>>
>> _______________________________________________
>> interchange-users mailing list
>> interchange-users [at] icdevgroup
>> http://www.icdevgroup.org/mailman/listinfo/interchange-users
>>
>


--
Brian J. Miller
End Point Corp.
http://www.endpoint.com/
brian [at] endpoint

_______________________________________________
interchange-users mailing list
interchange-users [at] icdevgroup
http://www.icdevgroup.org/mailman/listinfo/interchange-users


mike at perusion

Sep 2, 2009, 9:38 AM

Post #4 of 4 (690 views)
Permalink
Re: [PATCH] Expand range of round_to_frac_digits to handle exponentized numbers [In reply to]

Quoting Brian J. Miller (wiggins [at] danconia):
> Mike Heins wrote:
> > Quoting Brian J. Miller (brian [at] endpoint):
> >> ---
> >> lib/Vend/Util.pm | 20 +++++++++++++++-----
> >> 1 files changed, 15 insertions(+), 5 deletions(-)
> >>
> >> diff --git a/lib/Vend/Util.pm b/lib/Vend/Util.pm
> >> index 8356b84..5be6b5a 100644
> >> --- a/lib/Vend/Util.pm
> >> +++ b/lib/Vend/Util.pm
> >> @@ -269,12 +269,22 @@ sub round_to_frac_digits {
> >> else {
> >> $digits = 2;
> >> }
> >> - my @frac;
> >> - $num =~ /^(-?)(\d*)(?:\.(\d+))?$/
> >> - or return $num;
> >> + if ($num =~ /^(-?)(\d*)(?:\.(\d+)(?:e-(\d+)))?$/) {
> >> + # this number sufficiently close to zero for our purposes
> >> + return 0;
> >> + }
> >
> > Don't we want to return 0.00 (or whatever the frac_digits are? What if it is -1?
> >
>
> I guess it depends on the context used (aka numeric vs. string), but yes
> I'd be fine with that. Do you mean if $frac_digits is -1? I'm not clear
> exactly the use case for the subroutine, hence my e-mail here. Carl
> Bailey also suggested that if $frac_digits is 0 there is currently a
> trailing "." included which might need to be removed.
>
> If you can provide more clarity above I'm happy to whip something up.

I am not trying to deal with negative digits of precision. 8-\

You have it right now as "return 0" if the number is a small negative
exponent. That means you are not going to conform to "round to frac
digits", which would mean in the standard US locale 0.00.

--
Mike Heins
Perusion -- Expert Interchange Consulting http://www.perusion.com/
phone +1.765.328.4479 <mike [at] perusion>

"Laughter is inner jogging." -- Norman Cousins

_______________________________________________
interchange-users mailing list
interchange-users [at] icdevgroup
http://www.icdevgroup.org/mailman/listinfo/interchange-users

Interchange users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.