Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Setting floating point variable as infinite

Reply
Thread Tools

Setting floating point variable as infinite

 
 
Rui Maciel
Guest
Posts: n/a
 
      03-23-2012
Consider a data set whose elements can be mapped to a particular floating
point number. In order to determine the smallest floating point number
computed from each element from the set, is it a good idea to initialize a
temporary float as infinite, compare the temporary float with the value
mapped from each data element and then update the temporary if the mapped
value is smaller?

More importantly, is there a better way to do this?


Thanks in advance,
Rui Maciel
 
Reply With Quote
 
 
 
 
Marc
Guest
Posts: n/a
 
      03-23-2012
Rui Maciel wrote:

> Consider a data set whose elements can be mapped to a particular floating
> point number. In order to determine the smallest floating point number
> computed from each element from the set, is it a good idea to initialize a
> temporary float as infinite, compare the temporary float with the value
> mapped from each data element and then update the temporary if the mapped
> value is smaller?


It's a very common way to do it.

> More importantly, is there a better way to do this?


You can use the first element of your data set to initialize the minimum
and only start the loop from the second element. In some cases that can
be inconvenient to do.
 
Reply With Quote
 
 
 
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      03-23-2012
Marc <(E-Mail Removed)> wrote:
> Rui Maciel wrote:


>> Consider a data set whose elements can be mapped to a particular floating
>> point number. In order to determine the smallest floating point number
>> computed from each element from the set, is it a good idea to initialize a
>> temporary float as infinite, compare the temporary float with the value
>> mapped from each data element and then update the temporary if the mapped
>> value is smaller?


(snip)
> You can use the first element of your data set to initialize the minimum
> and only start the loop from the second element. In some cases that can
> be inconvenient to do.


Specifically, it fails in the case that the data set length is zero.

-- glen
 
Reply With Quote
 
gwowen
Guest
Posts: n/a
 
      03-23-2012
On Mar 23, 12:39*pm, glen herrmannsfeldt <(E-Mail Removed)>
wrote:
> > You can use the first element of your data set to initialize the minimum
> > and only start the loop from the second element. In some cases that can
> > be inconvenient to do.

>
> Specifically, it fails in the case that the data set length is zero.


It only fails if you think you know what the right answer for the
minimum value of an empty set is.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-23-2012
On 3/23/2012 8:27 AM, Rui Maciel wrote:
> Consider a data set whose elements can be mapped to a particular floating
> point number.


I assume you mean "whose elements can be mapped to floating point
numbers (each element to its own particular FP number)".

> In order to determine the smallest floating point number
> computed from each element from the set, is it a good idea to initialize a
> temporary float as infinite, compare the temporary float with the value
> mapped from each data element and then update the temporary if the mapped
> value is smaller?
>
> More importantly, is there a better way to do this?


The usual way to find the minimum is to initialize the value from the
first element, and then start comparing from the second element. An
empty set is a special case for which the calculation of the "minimum"
should just throw an exception. A set of one element is also a special
case: there is no need to compare anything. Two elements *could* be
made into a special case by use of std::min.

As for infinity (unrelated to searching through a set of numbers), there
is 'std::numeric_limits<double>::infinity()', which you could call if
'std::numeric_limits<double>::has_infinity' is 'true'.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      03-23-2012
glen herrmannsfeldt wrote:

> Specifically, it fails in the case that the data set length is zero.


Conversely, initializing a FP value as infinity may also bring unwanted
consequences. After all, if the data set length is zero then the minimum
value is set as infinity.


Rui Maciel
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      03-23-2012
Victor Bazarov wrote:

> The usual way to find the minimum is to initialize the value from the
> first element, and then start comparing from the second element. An
> empty set is a special case for which the calculation of the "minimum"
> should just throw an exception. A set of one element is also a special
> case: there is no need to compare anything. Two elements could be
> made into a special case by use of std::min.


I was hoping to use a single loop. Relying on a separate initialization
block feels a bit like a crude-ish hack.


> As for infinity (unrelated to searching through a set of numbers), there
> is 'std::numeric_limits<double>::infinity()', which you could call if
> 'std::numeric_limits<double>::has_infinity' is 'true'.


Yes, I was using that, and according to the standard
std::numeric_limits<T>::has_infinity is true for T = float and double, so no
test is necessary. The only problem I have with it is that it doesn't feel
quite right to handle infinity values like this. At least I never saw this
being done anywhere else.


Rui Maciel
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-23-2012
On 3/23/2012 11:03 AM, Rui Maciel wrote:
> Victor Bazarov wrote:
>
>> The usual way to find the minimum is to initialize the value from the
>> first element, and then start comparing from the second element. An
>> empty set is a special case for which the calculation of the "minimum"
>> should just throw an exception. A set of one element is also a special
>> case: there is no need to compare anything. Two elements could be
>> made into a special case by use of std::min.

>
> I was hoping to use a single loop.


Are you concerned with less typing, and not with implementing it
correctly *logically*? Do you consider "a single loop" better or more
efficient in some way?

> Relying on a separate initialization
> block feels a bit like a crude-ish hack.


"Crude-ish"? Really? <shrug> Using an infinity value in that manner is
crudish, IMNSHO. It suggests that (a) infinity is not a valid value for
any set element to be associated with (which might be true in your
model, but doesn't necessarily sound right in all cases), and (b) that
the maximum value from the elements of an empty set is infinity, which
is a number (if you divide by it, you get 0). I'd probably use NaN for
that, although by definition of "seeking a maximum associated floating
point number" should *not* be allowed for an empty set, such search
shouldn't return a value.

>> As for infinity (unrelated to searching through a set of numbers), there
>> is 'std::numeric_limits<double>::infinity()', which you could call if
>> 'std::numeric_limits<double>::has_infinity' is 'true'.

>
> Yes, I was using that, and according to the standard
> std::numeric_limits<T>::has_infinity is true for T = float and double, so no
> test is necessary. The only problem I have with it is that it doesn't feel
> quite right to handle infinity values like this. At least I never saw this
> being done anywhere else.


<another shrug> I have. But it's still not right. You can use any
other designated value that can never be found in your set. And if you
don't have any identifiable value to use, don't. Use *logic*.
Essentially you're trying to have a mapping of yourtype values to
double/float values without

std::map<double, yourtype const*> yourmap;

.. And you're trying to figure out a hack to get
(*yourmap.rbegin()).first without checking whether the 'yourmap' is
empty or not. <third shrug>

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      03-23-2012
Victor Bazarov wrote:

> On 3/23/2012 11:03 AM, Rui Maciel wrote:
>> Victor Bazarov wrote:
>>
>>> The usual way to find the minimum is to initialize the value from the
>>> first element, and then start comparing from the second element. An
>>> empty set is a special case for which the calculation of the "minimum"
>>> should just throw an exception. A set of one element is also a special
>>> case: there is no need to compare anything. Two elements could be
>>> made into a special case by use of std::min.

>>
>> I was hoping to use a single loop.

>
> Are you concerned with less typing, and not with implementing it
> correctly *logically*? Do you consider "a single loop" better or more
> efficient in some way?


You either failed to understand what I wrote or you are intentionally trying
to misrepresenting what I said. No one claimed that it is better to use
code which is logically incorrect if it provides a way to save on typing. I
don't know where you came up with that nonsense.


> > Relying on a separate initialization
>> block feels a bit like a crude-ish hack.

>
> "Crude-ish"? Really?


Really.


> <shrug> Using an infinity value in that manner is
> crudish, IMNSHO.


What happened to logical correctness? And do you also believe that, if the
objective was to get the largest non-negative number, initializing it to
zero or even any negative number would also be crudish?


> It suggests that (a) infinity is not a valid value for
> any set element to be associated with (which might be true in your
> model, but doesn't necessarily sound right in all cases),


Zero is also not valid in a considerable number of cases, and yet variables
are still set by default as zero.


> and (b) that
> the maximum value from the elements of an empty set is infinity, which
> is a number (if you divide by it, you get 0).


As a side note, and nit-picking a bit, this isn't true. Infinity isn't a
number, and k/infinity is meaningless. The k/infinity = 0 is only valid
because it was a specific indeterminate form which is often defined as
lim{x->infinity} k/x.

Similarly, division by zero has also been defined as k/0 = infinity, but
this doesn't mean it's a good idea to hold this as true. For a start, this
would mean that infinity*0 = k.


> I'd probably use NaN for
> that, although by definition of "seeking a maximum associated floating
> point number" should *not* be allowed for an empty set, such search
> shouldn't return a value.
>
>>> As for infinity (unrelated to searching through a set of numbers), there
>>> is 'std::numeric_limits<double>::infinity()', which you could call if
>>> 'std::numeric_limits<double>::has_infinity' is 'true'.

>>
>> Yes, I was using that, and according to the standard
>> std::numeric_limits<T>::has_infinity is true for T = float and double, so
>> no
>> test is necessary. The only problem I have with it is that it doesn't
>> feel
>> quite right to handle infinity values like this. At least I never saw
>> this being done anywhere else.

>
> <another shrug> I have. But it's still not right. You can use any
> other designated value that can never be found in your set. And if you
> don't have any identifiable value to use, don't. Use *logic*.


Why is it "not right"? Is there actually a valid technical reason behind
your assertion?


> Essentially you're trying to have a mapping of yourtype values to
> double/float values without
>
> std::map<double, yourtype const*> yourmap;
>
> . And you're trying to figure out a hack to get
> (*yourmap.rbegin()).first without checking whether the 'yourmap' is
> empty or not. <third shrug>


Again, you either failed to understand what I wrote or you are intentionally
trying to misrepresent what I said. No one claimed that the set in question
could be empty, and somehow you felt the need to attribute that claim, which
you invented, to someone else.

So, to avoid any more misconceptions or any attempts to misrepresent
anything, here is a clear description of this case.

- there is a non-empty set of data.
- there is a set of operators which map each element of that set to a
floating point number.
- the objective is to evaluate which is the minimum value of the codomain of
a particular operator.

I suggested the following approach:

<pseudo-ish code>

float minimum = std::numeric_limits<float>::infinity();
for(auto element: element_list)
{
if( operator(element) < minimum)
minimum = operator(element);
}

</pseudo-ish code>

Then, I asked if it was a good idea to do this. In other words, if there
was any reason that would made it a bad idea. Until now, no reason has been
given.

I also asked if there was a better way to get the minimum value.

Simple as that.


Rui Maciel
 
Reply With Quote
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      03-23-2012
Rui Maciel <(E-Mail Removed)> wrote:

(snip, I wrote)
>> Specifically, it fails in the case that the data set length is zero.


> Conversely, initializing a FP value as infinity may also bring
> unwanted consequences. After all, if the data set length is
> zero then the minimum value is set as infinity.


The Fortran MINVAL intrinsic function returns the minimum value of
the elements of an array. As Fortran (now) allows arrays dimensioned
zero, it has to allow for that case.

For arrays with no elements, it returns the largest representable
value, which I believe even for IEEE implementations is not
the IEEE Infinity value. Most likely, the value returned by
the HUGE intrinsic function.

-- glen
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
New python module to simulate arbitrary fixed and infinite precisionbinary floating point Rob Clewley Python 2 08-11-2008 05:10 PM
floating point problem... floating indeed :( teeshift Ruby 2 12-01-2006 01:16 AM
converting floating point to fixed point H aka N VHDL 15 03-02-2006 02:26 PM
Fixed-point format for floating-point numbers Motaz Saad Java 7 11-05-2005 05:33 PM



Advertisments