bignum t float
am 19.06.2011 15:07:50 von Patrick Dupre
---559023410-1804928587-1308488870=:4977
Content-Type: TEXT/PLAIN; charset=UTF-8; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE
Hello,
I need to convert the result of an aritmetic operation on 2
bignum into a "normal" float (ie double)
I did not find a way to do this.
no bignum ;
my $res =3D $A - $B ;
gives me always a bignum ($res) if $A and $B are bignum.
Thank.
--=20
---
==================== =====3D=
==================== =====3D=
==================== ====
Patrick DUPRÃ | |
Department of Chemistry | | Phone: (44)-(0)-1904-434384
The University of York | | Fax: (44)-(0)-1904-432516
Heslington | |
York YO10 5DD United Kingdom | | email: patrick.dupre@york.ac.uk
==================== =====3D=
==================== =====3D=
==================== ====
---559023410-1804928587-1308488870=:4977
Content-Type: text/plain; charset=us-ascii
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
---559023410-1804928587-1308488870=:4977--
Re: bignum t float
am 19.06.2011 21:29:19 von Rob Dixon
On 19/06/2011 14:07, Patrick Dupre wrote:
> Hello,
>
> I need to convert the result of an aritmetic operation on 2
> bignum into a "normal" float (ie double)
>
> I did not find a way to do this.
> no bignum ;
> my $res = $A - $B ;
>
> gives me always a bignum ($res) if $A and $B are bignum.
Hey Patrick
I suggest you convert the values to strings, which will force them to be
converted to normal floats before the arithmetic is done.
no bignum ;
my $res = $A->bstr - $B->bstr;
It is also probably better to use Math::BigFloat instead of bignum, so
that you can better control which variables are big floats and which are
normal.
I also wonder why you want to convert big floats into normal ones? The
only reason I can think of is to improve execution speed.
HTH,
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: bignum t float
am 19.06.2011 22:43:11 von Patrick Dupre
---559023410-341603450-1308516191=:4977
Content-Type: TEXT/PLAIN; charset=UTF-8; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE
On Sun, 19 Jun 2011, Rob Dixon wrote:
> On 19/06/2011 14:07, Patrick Dupre wrote:
>> Hello,
>>=20
>> I need to convert the result of an aritmetic operation on 2
>> bignum into a "normal" float (ie double)
>>=20
>> I did not find a way to do this.
>> no bignum ;
>> my $res =3D $A - $B ;
>>=20
>> gives me always a bignum ($res) if $A and $B are bignum.
>
> Hey Patrick
>
> I suggest you convert the values to strings, which will force them to be
> converted to normal floats before the arithmetic is done.
>
> no bignum ;
> my $res =3D $A->bstr - $B->bstr;
There is not option like (double) $res ?
>
> It is also probably better to use Math::BigFloat instead of bignum, so
> that you can better control which variables are big floats and which are
> normal.
>
> I also wonder why you want to convert big floats into normal ones? The
> only reason I can think of is to improve execution speed.
Not really, I have a big application calling c procedures
and I do not want to make changes with are not usuable and even tricky.
However, I need to make a couple of operations in BigFloat.
--=20
---
==================== =====3D=
==================== =====3D=
==================== ====
Patrick DUPRÃ | |
Department of Chemistry | | Phone: (44)-(0)-1904-434384
The University of York | | Fax: (44)-(0)-1904-432516
Heslington | |
York YO10 5DD United Kingdom | | email: patrick.dupre@york.ac.uk
==================== =====3D=
==================== =====3D=
==================== ====
---559023410-341603450-1308516191=:4977
Content-Type: text/plain; charset=us-ascii
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
---559023410-341603450-1308516191=:4977--
Re: bignum t float
am 19.06.2011 23:01:40 von Uri Guttman
>>>>> "PD" == Patrick Dupre writes:
PD> There is not option like (double) $res ?
rtfm to the rescue!
the synopsis shows how to do it. since it is a pragma it affects all
calculations in the scope. disabling the pragma is how you can get it
back to a regular float:
use bignum;
$x = 2 + 4.5,"\n"; # BigFloat 6.5
print 2 ** 512 * 0.1,"\n"; # really is what you think it is
print inf * inf,"\n"; # prints inf
print NaN * 3,"\n"; # prints NaN
{
no bignum;
print 2 ** 256,"\n"; # a normal Perl scalar now
}
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: bignum t float
am 19.06.2011 23:12:15 von Patrick Dupre
---559023410-579758561-1308517935=:4977
Content-Type: TEXT/PLAIN; charset=UTF-8; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE
On Sun, 19 Jun 2011, Uri Guttman wrote:
>>>>>> "PD" == Patrick Dupre writes:
>
> PD> There is not option like (double) $res ?
>
> rtfm to the rescue!
>
> the synopsis shows how to do it. since it is a pragma it affects all
> calculations in the scope. disabling the pragma is how you can get it
> back to a regular float:
>
> =09use bignum;
>
> $x =3D 2 + 4.5,"\n"; # BigFloat 6.5
> print 2 ** 512 * 0.1,"\n"; # really is what you think =
it is
> print inf * inf,"\n"; # prints inf
> print NaN * 3,"\n"; # prints NaN
>
> {
> no bignum;
> print 2 ** 256,"\n"; # a normal Perl scalar now
> }
>
This is what I tried first, the point is when you quite the
enclosure, even if you use no bignum, since the variables have declared
previous as bignumn any calculation using such variables previous
declared as bignum will be store in variables of the same type.
This make sense for perl, only a cast should allow changing the size.
In addition, the physical size (SV) of a bignum seems a lot bigger
than a usual NV.
Thank.
--=20
---
==================== =====3D=
==================== =====3D=
==================== ====
Patrick DUPRÃ | |
Department of Chemistry | | Phone: (44)-(0)-1904-434384
The University of York | | Fax: (44)-(0)-1904-432516
Heslington | |
York YO10 5DD United Kingdom | | email: patrick.dupre@york.ac.uk
==================== =====3D=
==================== =====3D=
==================== ====
---559023410-579758561-1308517935=:4977
Content-Type: text/plain; charset=us-ascii
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
---559023410-579758561-1308517935=:4977--
Re: bignum t float
am 20.06.2011 00:03:13 von Uri Guttman
>>>>> "PD" == Patrick Dupre writes:
PD> On Sun, 19 Jun 2011, Uri Guttman wrote:
>> use bignum;
>>
>> $x = 2 + 4.5,"\n"; # BigFloat 6.5
>>
>> {
>> no bignum;
>> print 2 ** 256,"\n"; # a normal Perl scalar now
>> }
>>
PD> This is what I tried first, the point is when you quite the
PD> enclosure, even if you use no bignum, since the variables have declared
PD> previous as bignumn any calculation using such variables previous
PD> declared as bignum will be store in variables of the same type.
PD> This make sense for perl, only a cast should allow changing the size.
PD> In addition, the physical size (SV) of a bignum seems a lot bigger
PD> than a usual NV.
as rob said, you can always sprintf the bignum and then numify it into a
regular float (where bignum isn't in effect). slow but it should work
unless the bignum exponent is actually larger than any supported float
can handle.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: bignum t float
am 20.06.2011 04:00:24 von Rob Dixon
On 19/06/2011 21:43, Patrick Dupre wrote:
> On Sun, 19 Jun 2011, Rob Dixon wrote:
>
>> On 19/06/2011 14:07, Patrick Dupre wrote:
>>> Hello,
>>>
>>> I need to convert the result of an aritmetic operation on 2
>>> bignum into a "normal" float (ie double)
>>>
>>> I did not find a way to do this.
>>> no bignum ;
>>> my $res = $A - $B ;
>>>
>>> gives me always a bignum ($res) if $A and $B are bignum.
>>
>> Hey Patrick
>>
>> I suggest you convert the values to strings, which will force them to be
>> converted to normal floats before the arithmetic is done.
>>
>> no bignum ;
>> my $res = $A->bstr - $B->bstr;
>
> There is not option like (double) $res ?
Not as such, but Perl uses the string and binary representations of
numbers interchangeably. The bstr method is fine as a conversion to
standard floating point, although it is something that you should avoid
doing repetitively if the efficiency of your code is significant.
>> It is also probably better to use Math::BigFloat instead of bignum,
>> so that you can better control which variables are big floats and
>> which are normal.
>>
>> I also wonder why you want to convert big floats into normal ones?
>> The only reason I can think of is to improve execution speed.
>
> Not really, I have a big application calling c procedures and I do
> not want to make changes with are not usuable and even tricky.
> However, I need to make a couple of operations in BigFloat.
So you have C code working on the Perl floating point values? Fair enough.
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: bignum t float
am 20.06.2011 04:22:24 von Rob Dixon
On 19/06/2011 22:12, Patrick Dupre wrote:
> On Sun, 19 Jun 2011, Uri Guttman wrote:
>
>>>>>>> "PD" == Patrick Dupre writes:
>>
>> PD> There is not option like (double) $res ?
>>
>> rtfm to the rescue!
>>
>> the synopsis shows how to do it. since it is a pragma it affects all
>> calculations in the scope. disabling the pragma is how you can get it
>> back to a regular float:
>>
>> use bignum;
>>
>> $x = 2 + 4.5,"\n"; # BigFloat 6.5
>> print 2 ** 512 * 0.1,"\n"; # really is what you think it is
>> print inf * inf,"\n"; # prints inf
>> print NaN * 3,"\n"; # prints NaN
>>
>> {
>> no bignum;
>> print 2 ** 256,"\n"; # a normal Perl scalar now
>> }
>>
> This is what I tried first, the point is when you quite the
> enclosure, even if you use no bignum, since the variables have declared
> previous as bignumn any calculation using such variables previous
> declared as bignum will be store in variables of the same type.
> This make sense for perl, only a cast should allow changing the size.
> In addition, the physical size (SV) of a bignum seems a lot bigger
> than a usual NV.
BigFloat values are Perl Math::BigFloat obects, consisting of a blessed
anonymous hash with four fields, two of which have anonymous arrays as
values. That is why they take up so much more memory than standard
numeric values, and because the data structure only has meaning when
accessed by the object methods, there is no implicit conversion to the
standard format.
There is no casting in Perl - it simply converts between formats at run
time as required by the code.
As I have said before, I strongly recommend using Math::BigFloat instead
of bignum, as it makes it far easier to mix standard and BigFloat
values. All bignum does is to force all numeric values to be stored
implicitly as Math::BigFloat objects, and special care must be taken to
keep track of whether bignum is in effect or not at different places in
the code.
Cheers,
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: bignum t float
am 20.06.2011 05:34:42 von sisyphus1
----- Original Message -----
From: "Rob Dixon"
>> I need to convert the result of an aritmetic operation on 2
>> bignum into a "normal" float (ie double)
> I suggest you convert the values to strings, which will force them to be
> converted to normal floats before the arithmetic is done.
>
> no bignum ;
> my $res = $A->bstr - $B->bstr;
>
Bear in mind that $res will then hold a "long double" (not "double") iff
perl was built to use long doubles. (Check 'perl -V:nvsize'.)
Cheers,
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/