[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-devel
Subject: Re: [OFFTOPIC] std::numeric_limits<double>::min() question.
From: Richard Smith <richard () metafoo ! co ! uk>
Date: 2006-04-26 21:43:59
Message-ID: 200604262243.59685.richard () metafoo ! co ! uk
[Download RAW message or body]
On Wednesday 26 April 2006 22:12, Christoph Bartoschek wrote:
> Am Mittwoch, 26. April 2006 22:27 schrieb Dmitry Suzdalev:
> > On Thursday 27 April 2006 00:12, Will Entriken wrote:
> > > std::numeric_limits<double>::min() return the minimum positive value.
> >
> > Hmm... yes? :)
> > Bahhh, I should read docs more carefully, thanks! :)
> >
> > > you could try maxd = -1 if d will be non-negative. that will be pretty
> > > obvious what is going on.
> >
> > Nope, d might be negative - that's why I didn't use -1 for maxd.
> > So, then another question:
> > Maybe I'm wrong when I say that
> > "-std::numeric_limits<double>::max()" is messy-looking?
>
> If you need a value that is smaller than all numbers, you could use:
>
> maxd = - std::numeric_limits<double>::infinity();
That's all well and good, but it won't work using gcc (3.2, 3.3 and 3.4 at
least) on Linux. The key point is that
std::numeric_limits<double>::has_infinity
is false on these systems (or at least it is for me).
You could store a flag to indicate whether this is the first time through the
loop, and if so then set maxd. Or you can use this:
double minDbl = -std::numeric_limits<double>::max();
This will work for all IEEE-754 compliant systems (x86, x86_64, Solaris, etc),
and it's what I use at work (where those are the only platforms we support),
but it is a little messy. Alternatively, you can use something like this:
double makeInfinity()
{
double x = std::numeric_limits<double>::max();
while (x * x > x) x = x * x;
return x;
}
double infinity()
{
static double inf = makeInfinity();
return inf;
}
This will work on all systems which actually have an infinity (whether the C++
standard library knows about it or not). On systems which don't (if you can
find any), you'll probably get a floating point exception of some kind when
it overflows.
One final thing... on Intel-based machines, computations involving infinity or
NaN floating-point values are VERY VERY slow (hundreds of times slower than
normal floating-point operations), so you probably don't want to use infinity
after all. Best to just use -std::numeric_limits<double>::max() or a flag.
Richard
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic