Boost logo

Boost :

Subject: Re: [boost] [mp_int] new release
From: Brandon Kohn (blkohn_at_[hidden])
Date: 2008-10-10 11:37:20


--------------------------------------------------
From: "Kevin Sopp" <baraclese_at_[hidden]>
Sent: Friday, October 10, 2008 10:25 AM
To: <boost_at_[hidden]>
Subject: Re: [boost] [mp_int] new release

>> Here are the initial values:
>>
>> - den
>> - digits_ 0x003023e0 unsigned int *
>> 185745031 unsigned int
>> used_ 10 unsigned int
>
> Since 185745031 is smaller than maxLong it should never enter the loop
> to subtract it. And I can confirm by manually subtracting it that it
> gives the right result. However a used count of 10 is definitely wrong
> thus signaling that it is actually larger than maxLong.

Ah, yes.. good point. It is pulled out of the boost::rational< mp_int<>
>::denominator() call. So it will have undergone many arithmetic operations
by the time I call it. I have an approximation algorithm for pi which I'm
doing. Here's the code, perhaps you can replicate from it:

template <typename T, unsigned int Iterations>
T calc_pi()
{
    T intermediate_result(0);
    T numerator(0);
    T pi;

    for( int k = 0; k <= Iterations; ++k )
    {
        if( k%2 )
        {
            numerator = -1;
        }
        else
        {
            numerator = 1;
        }

        intermediate_result += numerator / ( T(2) * T(k) + T(1) );
        pi = T(4) * intermediate_result; }

    return pi;
}

You'll need these:

template <>
class numeric_limits< boost::mp_math::mp_int<> >
{
private:
  typedef boost::mp_math::mp_int<> Type;
public:
  static const bool is_specialized = true;
  static const int digits = 0;
  static const int digits10 = 0;
  static const bool is_signed = true;
  static const bool is_integer = true;
  static const bool is_exact = true;
  static const int radix = 2;
  static const int min_exponent = 0;
  static const int min_exponent10 = 0;
  static const int max_exponent = 0;
  static const int max_exponent10 = 0;
  static const bool has_infinity = false;
  static const bool has_quiet_NaN = false;
  static const bool has_signaling_NaN = false;
  static const float_denorm_style has_denorm = denorm_absent;
  static const bool has_denorm_loss = false;
  static const bool is_iec559 = false;
  static const bool is_bounded = false;
  static const bool is_modulo = false;
  static const bool traps = false;
  static const bool tinyness_before = false;
  static const float_round_style round_style = round_toward_zero;
  static Type min()
  {
    return static_cast<Type>(0);
  }
  static Type max()
  {
    return static_cast<Type>(0);
  }

  static Type epsilon()
        {
            return static_cast<Type>(1);
        }

        static Type round_error()
        {
            return static_cast<Type>(1);
        }

        static Type infinity()
        {
            return static_cast<Type>(0);
        }

        static Type quiet_NaN()
        {
            return static_cast<Type>(0);
        }

        static Type denorm_min()
        {
            return static_cast<Type>(1);
        }
    };
} // namespace std

Call it like this:

typedef boost::rational< mp_int<> > rational_int;
rational_int pi = calc_pi< rational_int, 100>();

>
> How do you create the input? den seems to be corrupted before it even
> enters the function.
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk