Click here to get back home

bignum incompatible with looks_like_number() ???

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
bignum incompatible with looks_like_number() ??? C B 06-26-2007
Posted by C B on June 26, 2007, 10:17 pm
Please log in for more thread options


Apparently, "use bignum;" causes problems with looks_like_number.

Background:

My code makes a call to Finance::Math:IRR to compute an Internal Rate of
return.

my call looks like this:
$result = xirr(%HashOfDatesAndCashflows, precision => 0.000001);

This works fine, except that I'm getting answers that are a little off
numerically.

So, in my main .pl file, I added the line:
use bignum;

Which resulted in an error message:
ERROR: precision is not a valid number at /Users/cb/main.pl line 189

After digging around, I discovered that Finance::Math::IRR calls
looks_like_number($x) where $x is set to the value of the precision hash
key.

Sure enough, the debugger reveals that I'm calling xirr with:
         
'precision' => bless( {
                    '_m' => [1],
                        '_es' => '-',
                        '_e' => [6],
                        'sign' => '+'
                       }, 'Math::BigFloat' )

which SHOULD pass the looks_like_number test, but it doesn't.

I can't believe two such frequently-used components are incompatible.

bignyum is included in most Perl builds.

looks_like_number() is part of the perl api according to
http://search.cpan.org/~nwclark/perl-5.8.8/pod/perlapi.pod


What am I doing wrong?

I'm using Perl 5.8.6 under MacOS Darwin

Thanks
CB

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet
News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+
Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption
=----

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+
Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Posted by Sisyphus on June 26, 2007, 10:52 pm
Please log in for more thread options



.
.
>
> my call looks like this:
> $result = xirr(%HashOfDatesAndCashflows, precision => 0.000001);
>
> This works fine, except that I'm getting answers that are a little off
> numerically.

That would probably be because decimal fractions like 0.000001 cannot be
represented with complete accuracy in the internal binary form that
computers use (irrespective of the number of bits of precision that have
been allocated).

>
> So, in my main .pl file, I added the line:
> use bignum;
>
> Which resulted in an error message:
> ERROR: precision is not a valid number at /Users/cb/main.pl line 189
>
> After digging around, I discovered that Finance::Math::IRR calls
> looks_like_number($x) where $x is set to the value of the precision hash
> key.
>
> Sure enough, the debugger reveals that I'm calling xirr with:
>
> 'precision' => bless( {
> '_m' => [1],
> '_es' => '-',
> '_e' => [6],
> 'sign' => '+'
> }, 'Math::BigFloat' )
>
> which SHOULD pass the looks_like_number test, but it doesn't.

No - it's a Math::BigFloat object, which looks nothing like a number to
(both me and) Scalar::Util::looks_like_number().

I guess the basic issue is that Math::Finance::IRR is not built to handle
Math::BigFloat objects.

Cheers,
Rob


Posted by C B on June 27, 2007, 4:02 am
Please log in for more thread options



>> This works fine, except that I'm getting answers that are a little off
>> numerically.

> because decimal fractions like 0.000001 cannot be
> represented with complete accuracy in the internal binary form that
> computers use (irrespective of the number of bits of precision that have
> been allocated).

Yeah, that's exactly why I'm trying to use bignum -- to extend the
precision out far enough so that data representation errors are below
the radar. My boss's radar certainly notices a 500 ppm error, which is
what I'm getting from Financials::Math::IRR --- which incidentally
brags about being used and tested extensively in serious financial
environments.

> >
> > which SHOULD pass the looks_like_number test, but it doesn't.
>
> No - it's a Math::BigFloat object, which looks nothing like a number to
> (both me and) Scalar::Util::looks_like_number().

That answer seems so far out of line with the Perl philosophy that I'm
having trouble believing it.

Firstly, the number in Math::BigFloat format is clearly +1E-6 when you
remove the OPCODEs --- which sure looks like a number to me.

Secondly, does 40490FD0 look like a number? It's 3.14159 in floating
point hex. You can't judge a number by its numerals.

Finally, I cannot believe Larry would allow fundamental things like
bignums and looks_like_number() to be incompatible --- they are both
essentially part of the Perl distro.

If you can't trust those, what parts of Perl CAN you trust?

Is there a better answer?

CB

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+
Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Posted by Sisyphus on June 27, 2007, 5:15 am
Please log in for more thread options



.
.
>> because decimal fractions like 0.000001 cannot be
>> represented with complete accuracy in the internal binary form that
>> computers use (irrespective of the number of bits of precision that have
>> been allocated).
>
> Yeah, that's exactly why I'm trying to use bignum -- to extend the
> precision out far enough so that data representation errors are below
> the radar. My boss's radar certainly notices a 500 ppm error, which is
> what I'm getting from Financials::Math::IRR --- which incidentally
> brags about being used and tested extensively in serious financial
> environments.
>

Well ... *I* certainly don't brag about that ... and I don't think Larry
does, either.

As it stands, Financial::Math::IRR apparently accommodates only 53 bits of
precision - or however many bits of precision that an NV provides on your
build of perl.

If that's not enough, then you (or someone else) will have to rewrite
Financial::Math::IRR so that it does accommodate the precision you need.
(And one way to do that would be to rewrite it so that it does work with
bignum objects.)

>> >
>> > which SHOULD pass the looks_like_number test, but it doesn't.
>>
>> No - it's a Math::BigFloat object, which looks nothing like a number to
>> (both me and) Scalar::Util::looks_like_number().
>
> That answer seems so far out of line with the Perl philosophy that I'm
> having trouble believing it.

AFAICT, it's the way things are. Whether it's out of line or not, I wouldn't
know - I'm not a philosopher.

>
> Firstly, the number in Math::BigFloat format is clearly +1E-6 when you
> remove the OPCODEs --- which sure looks like a number to me.
>
> Secondly, does 40490FD0 look like a number? It's 3.14159 in floating
> point hex. You can't judge a number by its numerals.

I believe it's also the decimal integer 1078530000 expressed in hex.

>
> Finally, I cannot believe Larry would allow fundamental things like
> bignums and looks_like_number() to be incompatible --- they are both
> essentially part of the Perl distro.

If Scalar::Util::looks_like_number (which just wraps the perlapi function of
the same name) thought that bignum objects looked like a number then there
would be no need for the bignum package to exist in the first place.

Seems to me you're suggesting that perl should be able to seamlessly handle
numbers of whatever you precision you choose. I've no problem with that
suggestion - but it's not the way things are. Perl will natively and
seamlessly handle precision only up to the precision of the NV - usually the
same as a C double, but as much as a C long double (if perl was built that
way).

If you want to go beyond the precision provided, then you need packages such
as bignum, and you need modules that support those packages.

Maybe someone needs to write Financial::Math::IRR::BigFloat.

Cheers,
Rob


Similar ThreadsPosted
bignum incompatible with loos_like_number ??? June 26, 2007, 10:05 pm
bignum 0.22 July 4, 2007, 9:43 am
Problem around bignum March 28, 2006, 6:04 am
Error in perl module "bignum"... May 14, 2006, 4:31 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap