mpfrcpp-common
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[MPFRCPP] Re: Question about MPFRCPP's Real::isFits


From: bav . 272304
Subject: [MPFRCPP] Re: Question about MPFRCPP's Real::isFits
Date: Thu, 25 Oct 2007 15:23:52 +0400
User-agent: KMail/1.9.5

Thank you!

I'll correct it in the next release. The right code is listed below.

template <typename T> bool Real::isFits () const throw() {
    if (isNumber()) {

        if (sign() < 0 && !std::numeric_limits<T>::is_signed)
            return false;

        Real y (getPrecision());
        mpfr_abs (y.getMpfrT(), getMpfrT(),
                  getParameters().getDefaultRoundMode().getMpfrRndT());

        if (y < Real(std::numeric_limits<T>::min()))
            return false;

        if (y > Real(std::numeric_limits<T>::max()))
            return false;

        if (!std::numeric_limits<T>::is_integer) {
            if (getExponent().getMpExpT() <
                std::numeric_limits<T>::min_exponent)
                return false;
            if (getExponent().getMpExpT() >
                std::numeric_limits<T>::max_exponent)
                return false;
        }
    }
    else {
        if (isNaN() &&
                (!std::numeric_limits<T>::has_quiet_NaN ||
                 !std::numeric_limits<T>::has_signaling_NaN))
            return false;
        if (isInfinity() && !std::numeric_limits<T>::has_infinity)
            return false;
    }

    return true;

}

On Thursday 25 October 2007 08:30, you wrote:
> Dear Mr Beshenov,
>
> I want to use your C++ interface for MPFR for research purpose. I'm
> currently learning how to use it and trying to integrate it to my program.
> I've stumbled upon a problem yesterday and would like to get your advice on
> it.
>
> I've computed data with a first program and saved them as mpfr::Real in a
> file (writing them with their actual precision). I then want to load those
> data to use them in a second program. Here is what I do :
>
> mpfr::Real sRadius;
> mpfr::Real center[3];
> std::ifstream is;
> is.open(filename.c_str());
> is >> sRadius >> center[0] >> center[1] >> center[2];
> And then I create an object which needs doubles, so I call the "operator
> double" operator on all those Real.
>
> center[0].operator double() throws an exception with the following message
> printed in the console : "number does not fits in conversion target type"
>
> I printed the values stored in my variables, and center[0] is equal to
> -2.5e-01. It seems that the exception is triggered because isFits<double>()
> returns "false". Looking at isFits, it's because of this test : if (*this <
> Real(std::numeric_limits<T>::min()))
>         return false;
>
> Here is my problem : it seems that because of this any negative number and
> zero won't be considered fitted to be converted in double. I've conducted
> some tests and indeed the function isFits returns false for a Real equal to
> zero and several different negative numbers. And returns true for the
> opposite values. Am I doing something wrong here ? Maybe I'm not using your
> interface properly... Or there is a problem and zero and negative numbers
> are not considered by the isFits function. The solution I see is returning
> true for zero and then using the absolute value in your test, like in the
> code below:
>
>     template <typename T> bool Real::isFits () const throw() {
>         if (this->isZero)
>             return true;
>
>         if (isNumber()) {
>             if (*this < Real(0) && !std::numeric_limits<T>::is_signed)
>                 return false;
>
>             if (Abs(*this) < Real(std::numeric_limits<T>::min()))
>                 return false;
>
>             if (Abs(*this) > Real(std::numeric_limits<T>::max()))
>                 return false;
>
>             if (!std::numeric_limits<T>::is_integer) {
>                 if (getExponent().getMpExpT() <
> std::numeric_limits<T>::min_exponent) return false;
>                 if (getExponent().getMpExpT() >
> std::numeric_limits<T>::max_exponent) return false;
>             }
>         }
>         else {
>             if (isNaN() &&
>                     (!std::numeric_limits<T>::has_quiet_NaN ||
>                      !std::numeric_limits<T>::has_signaling_NaN))
>                 return false;
>             if (isInfinity() && !std::numeric_limits<T>::has_infinity)
>                 return false;
>         }
>         return true;
>     }
>
> Thank you very much for your help, I'm looking forward to reading your
> opinion on this matter.

-- 
Alexey Beshenov <address@hidden>
http://beshenov.ru/

Attachment: pgphAnkFpkPaM.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]