Hello mje,

I did a bit of a research as McA did but on a different area. I came up with the following:

Ok let's start with the basics, I tried to load both Math::FixedPrecision and bignum. As a result, I got the same error with you.

Then I went I start reading for bignum and I found this:

All operators (including basic math operations) are overloaded. Integer and floating-point constants are created as proper BigInts or BigFloats, respectively.

If you do use bignum;

at the top of your script, Math::BigFloat and Math::BigInt will be loaded and any constant number will be converted to an object (Math::BigFloat for floats like 3.1415 and Math::BigInt for integers like 1234).

So at this point I can imagine there is a conflict between the packages Math::FixedPrecision and bignum.

So as a second step I decided to go through the documentation of Math::BigFloat and Math::BigInt where are used by bignum in order to create my solution to your problem.

If I understand correctly, your problem is that you want to have accuracy to the nth digit of your float, but at the same time you want to use the bignum module.

Update: I just thought that maybe you are interested in just getting the 2 digits after the dot of a float, without rounding. In that case the solution could be much more simpler by using substr. If this is what you are looking for you can simply do that:

Update 2: wrong output, correcting output. I was experimenting and accidentally I posted before the experimentation.

my $pi = "3.1415"; my $substr = substr $pi, 0, 4; print "\substring: ".$substr."\n"; # $substr: 1.14

Alternatively if my assumption is correct, sample of working code with the output:

#!/usr/bin/perl use bignum; use strict; use warnings; use Math::BigFloat; sub math { my $pi = "3.1415"; my $substr = substr $pi, 0, 4; print "\$substr: ".$substr."\n"; my $N = Math::BigFloat->new($pi); print "\$N: ".$N."\n"; my $round = $N->copy()->ffround(-3); print "\$round:" .$round. "\n"; } math(); sub alternative { my $x = 2 + 4.5; # BigFloat 6.5 print "My \$x: ".$x."\n"; 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 * 0.1,"\n"; # a normal Perl scalar now } } alternative(); __END__ $substr: 3.14 $N: 3.1415 $round:3.142 My $x: 6.5 1340780792994259709957402499820584612747936582059239337772356144372176 +403007354697680187429816690342769003185818648605085375388281194656994 +643364900608409.6 inf NaN 1.15792089237316e+76
Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: Math::FixedPrecision and bignum clash by thanos1983
in thread Math::FixedPrecision and bignum clash by mje

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.