comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: How to check a Float for NaN
Date: Tue, 27 May 2014 17:35:48 -0500
Date: 2014-05-27T17:35:48-05:00	[thread overview]
Message-ID: <lm3406$g8o$1@loke.gir.dk> (raw)
In-Reply-To: 840855a9-bda8-44b7-ab53-e157b4fc1d31@googlegroups.com

"Adam Beneschan" <adambeneschan@gmail.com> wrote in message 
news:840855a9-bda8-44b7-ab53-e157b4fc1d31@googlegroups.com...
On Tuesday, May 27, 2014 5:35:26 AM UTC-7, Maurizio Tomasi wrote:
>> > That is, I see no sensible reason for NaNs or infinities -- they're 
>> > just ways of deferring detection of bugs. I would have hoped that Ada's 
>> > moved beyond that, just like it has for integers.]
>>
>>
>>
>> Being a scientist working with large chunks of data, I find NaNs useful 
>> in a number of situations.
>> I work in a domain (observational cosmology) where we need to deal with 
>> sky maps containing
>> ~10^7 pixels (you can think of a "map" as a 1D vector where pixels on the 
>> sky sphere are
>> ordered according to some rule). Not every sky direction can be sampled, 
>> because of a number
>> of problems (in the instrument, in the observational strategy, in the 
>> data reduction pipeline, etc.)
>>
>> Therefore, in my Python+NumPy codes I always mark such directions using 
>> "quiet NaNs".
>> If I have to combine two maps in order e.g. to take their average, the 
>> usual rules for
>> combining NaNs are be exactly what I want. Writing in Ada what I actually 
>> write in Python:
>>
>>   for I := 1 to N do
>>     Average_Map(I) := 0.5 * (Map1(I) + Map2(I));
>>   end loop;
>
>Well, that's actually half Pascal and half Ada, but we understand what you 
>mean.
>
>But you don't need NaN's built into the language in order to get that sort 
>of functionality.  In
>Ada (or C++ or any other language that supports operator overloading), it's 
>simple enough
>to define an "optional floating-point" record type consisting of a float 
>and a Boolean, where
>the Boolean indicates "missing data", and define operators that produce 
>"missing data" if
>either operand is missing.  So you could still write mostly the same code, 
>except that
>converting to or from a float, or from a floating-point literal, takes a 
>little extra code.

If one has
    function "+" (Right : Float) return Optional_Float;

then the "extra code" is just preceeding the float value with a "+". Hardly 
earth-shaking. (And the usual complaint about using "+" as a conversion 
operator is a non-problem here as these are numeric types).

I much prefer this sort of solution (where the missing values are explicitly 
treated) rather than using some sort of magic number (a NaN being an extreme 
version of that). The name alone tells you that it doesn't belong in a 
numeric type -- since when is something that is "not a number" belong in a 
type defining numbers?

As usual, this is mainly a case of premature optimization (preverting the 
hardware to handle something that's a rare need -- I wonder how much faster 
float hardware could be if it didn't have to mess with NaNs? I know that 
they impacted our software floating point quite a bit even though I made no 
attempt to actually do anything useful with them.)

                                 Randy.


  reply	other threads:[~2014-05-27 22:35 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-30 10:27 How to check a Float for NaN Jerry
2008-04-30 10:47 ` christoph.grein
2008-04-30 10:50   ` christoph.grein
2008-04-30 15:02     ` Adam Beneschan
2008-04-30 20:33       ` Jerry
2008-04-30 23:23         ` Adam Beneschan
2008-05-01  1:00           ` Adam Beneschan
2008-05-01 19:52             ` Keith Thompson
2008-05-01 23:57               ` Jerry
2008-04-30 23:29       ` Randy Brukardt
2008-05-01  8:04       ` Stuart
2008-05-01 14:38         ` Adam Beneschan
2008-05-01 17:14           ` Stuart
2008-05-01 19:22             ` Randy Brukardt
2008-05-02  0:04         ` Jerry
2008-04-30 20:36 ` Jerry
2008-04-30 21:53   ` Adam Beneschan
2008-05-01  1:05     ` Jerry
2014-05-22  7:27   ` jan.de.kruyf
2014-05-22  8:09     ` Dmitry A. Kazakov
2014-05-22  9:24       ` Simon Wright
2014-05-22  9:48         ` Dmitry A. Kazakov
2014-05-22 15:28           ` Adam Beneschan
2014-05-22 16:31             ` Dmitry A. Kazakov
2014-05-22 23:33               ` Adam Beneschan
2014-05-23  7:38                 ` Dmitry A. Kazakov
2014-05-23 21:39                 ` Randy Brukardt
2014-05-27  8:35                   ` Dmitry A. Kazakov
2014-05-27 12:35                   ` Maurizio Tomasi
2014-05-27 15:53                     ` Adam Beneschan
2014-05-27 22:35                       ` Randy Brukardt [this message]
2014-05-27 22:59                         ` Jeffrey Carter
2014-05-28  7:32                         ` Dmitry A. Kazakov
2014-05-28  8:40                       ` Maurizio Tomasi
2008-05-05 18:23 ` Martin Krischik
2008-05-05 20:49   ` Adam Beneschan
2008-05-06 18:09     ` Jerry
2008-05-06 18:45       ` Wiljan Derks
2008-05-06 22:18         ` Adam Beneschan
2008-05-07 22:56           ` Randy Brukardt
2008-05-07 22:56           ` Randy Brukardt
2008-05-07 23:20             ` Adam Beneschan
2008-05-09  7:24             ` Stephen Leake
2008-05-10 17:00   ` anon
2008-05-11 22:00     ` Keith Thompson
2008-05-12  2:01       ` anon
2008-05-09 19:49 ` anon
2008-05-10  2:36   ` Jerry
2008-05-10  3:53     ` anon
2008-05-10  6:24       ` christoph.grein
2008-05-10  8:05     ` Georg Bauhaus
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox