develooper Front page | perl.perl5.porters | Postings from April 2007

bignum et.al and "constants"

From:
Tels
Date:
April 7, 2007 10:53
Subject:
bignum et.al and "constants"
Message ID:
200704071953.53694@bloodgate.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Moin,

I just noticed that when you do:

	perl -Mbignum -wle 'print 5'
	perl -Mbignum -wle 'print 5.1'

you end up with a Math::BigInt resp. Math::BigFloat "constant". However, 
these can be modified, and are sometimes, and sometimes not:

	# perl -Mbignum -wle 'for (0..2) { print 5->blog() }'
	1.609437912434100374600759333226187639526
	1.609437912434100374600759333226187639526
	1.609437912434100374600759333226187639526
	# perl -Mbignum -wle 'for (0..2) { print 5.1->blog() }'
	1.629240539730280087626788400111288032635
	0.4881139796953300666097741422180843672917
	-0.7172063354515893755243480929933879353687

The difference comes from the fact that in the first case, 
Math::BigFlow->blog() gets a Math::BigInt, makes a copy of that and returns 
the result. In the second case it actually modifies the passed-in value. 
Oops.

Now, technically you *should* write it like this (note "copy->"!):

	# perl -Mbignum -wle 'for (0..2) { print 5->copy->blog() }'
	1.609437912434100374600759333226187639526
	1.609437912434100374600759333226187639526
	1.609437912434100374600759333226187639526
	# perl -Mbignum -wle 'for (0..2) { print 5.1->copy->blog() }'
	1.609437912434100374600759333226187639526
	1.609437912434100374600759333226187639526
	1.609437912434100374600759333226187639526

I think the right thing to do would be to catch modifications of constants, 
and warn. However, that would need Math::BigInt::Constant in the core, plus 
some tuits to make it actually work and not break anything else.

	# perl -Mbignum -MMath::BigInt::Constant  -wle 'for (0..2)\
	 { print Math::BigInt::Constant->new(5)->blog() }'
	Can not modify Math::BigInt::Constant 5 via blog()

Oh, and I just noticed Math::BigFloat::Constant doesn't even exist yet - 
although with all the hooks already in place, it would be trivial to set it 
up. Math::BigInt::Constant is about 1K of Perl code.

(But also maybe somebody else has a better (faster, less overhead) idea how 
to make Math::Big* objects constant.

All the best,

Tels
	
- -- 
 Signed on Sat Apr  7 19:45:37 2007 with key 0x93B84C15.
 View my photo gallery: http://bloodgate.com/photos
 PGP key on http://bloodgate.com/tels.asc or per email.

 "Look, I'm about to buy me a double barreled sawed off shotgun and show
 Linus what I think about backspace and delete not working."

  -- some anonymous .signature
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iQEVAwUBRhf20XcLPEOTuEwVAQJ1Owf9ECryR3Hw+FQG4/RHNqGVOY5vw57bjpqI
uBP72ua1D8b10xENOzUAYLzSfSHhc9qNVR+Tru84xiWy0FS7KwOgk2dRtS7u4jzs
ylyyeCeG/KIbkGN5XD6k/tNSEo4HXiEsfx1WSzda9TBgs8iveCkdqnaJbmjXMyss
tCc2S0kv3WrmR9YPjs24NUannxGUyotK243yP2GxQNJfWaknS4/eRFc40O2uxeHR
ZkgYGtbET29p+Rt5qtMnk7lMkt/6A4N10eCUHa7pZs3zSPIEB/3RY4UI4x6FsaUz
FFhgauDwfCdPmSOB3N5uYXKHwl2XPnKtMHGBPgjlV3sccQ90He+80g==
=cFDY
-----END PGP SIGNATURE-----



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About