472,789 Members | 1,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 software developers and data experts.

Compile-time constant quiet NaN (double) ???

Running VC++ 6 under Win2K on i386.

I would like to assign a (compile-time) constant that resolves to a
quiet NaN (of type double)

I can assign a quiet NaN to a *variable* (of type const double, say)
by eg.:

const double qnan = fmod(1.0,0.0);

but - for efficiency reasons - what I really want is a (compile-time)
constant:

#define QNAN ???

Now I have established that (on my system at least) the type __int64
is the same size as a double and that a quiet NaN corresponds to an
__int64 with a value of -2251799813685248. However, the "obvious":

#define QNAN -2251799813685248i64

will not work, since an assignment eg.:

double x = QNAN;

will cause the QNAN to be *cast* to type double, so that x will have
the value -2251799813685248.0

Is there a way to "turn off" automatic casting, so that an assignation
as above simply copies byte-for-byte?

Any ideas much appreciated,

--
Lionel B
Jul 22 '05 #1
6 6849
Lionel B wrote:
Running VC++ 6 under Win2K on i386.

I would like to assign a (compile-time) constant that resolves to a
quiet NaN (of type double)

I can assign a quiet NaN to a *variable* (of type const double, say)
by eg.:

const double qnan = fmod(1.0,0.0);

but - for efficiency reasons - what I really want is a (compile-time)
constant:

#define QNAN ???
Don't use #define for that. Why do you believe it could be more
efficient than a constant anyway?
Now I have established that (on my system at least) the type __int64
is the same size as a double and that a quiet NaN corresponds to an
__int64 with a value of -2251799813685248. However, the "obvious":

#define QNAN -2251799813685248i64

will not work, since an assignment eg.:

double x = QNAN;

will cause the QNAN to be *cast* to type double, so that x will have
the value -2251799813685248.0
It's converted, not cast.
Is there a way to "turn off" automatic casting, so that an assignation
as above simply copies byte-for-byte?


Instead, I'd recommend to the standard C++ way to get the quiet NaN:

// #include <limits>
double x = std::numeric_limits<double>::quiet_NaN();

Jul 22 '05 #2
Lionel B wrote:

Running VC++ 6 under Win2K on i386.

I would like to assign a (compile-time) constant that resolves to a
quiet NaN (of type double)

I can assign a quiet NaN to a *variable* (of type const double, say)
by eg.:

const double qnan = fmod(1.0,0.0);

but - for efficiency reasons - what I really want is a (compile-time)
constant:
Hmm. I wonder how many nanoseconds this will take on program startup
for a global constant. Is this really an efficiency problem?

#define QNAN ???

Now I have established that (on my system at least) the type __int64
is the same size as a double and that a quiet NaN corresponds to an
__int64 with a value of -2251799813685248. However, the "obvious":

#define QNAN -2251799813685248i64

will not work, since an assignment eg.:

double x = QNAN;

will cause the QNAN to be *cast* to type double, so that x will have
the value -2251799813685248.0

Is there a way to "turn off" automatic casting, so that an assignation
as above simply copies byte-for-byte?


_int64 qnan = -2251799813685248i64;
double x = (double*)&qnan;

or hidden in a function:

static _int64 myqnan = -2251799813685248i64;
inline double qnan()
{
return *(double*)&myqnan;
}

double x = qnan();

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3
* Karl Heinz Buchegger <kb******@gascad.at> schriebt:
Hmm. I wonder how many nanoseconds this will take on program startup
for a global constant. Is this really an efficiency problem?

Is there a way to "turn off" automatic casting, so that an assignation
as above simply copies byte-for-byte?


_int64 qnan = -2251799813685248i64;
double x = (double*)&qnan;

or hidden in a function:

static _int64 myqnan = -2251799813685248i64;
inline double qnan()
{
return *(double*)&myqnan;
}

double x = qnan();


A bit more general (standard C++ doesn't have an _int64 type),
STATIC_ASSERT( std::numeric_limits<double>::is_iec559 );
STATIC_ASSERT( std::numeric_limits<double>::has_quiet_NaN );
STATIC_ASSERT( sizeof(unsigned) == 4 );
STATIC_ASSERT( sizeof(double) == 8 );

static unsigned const nanData[2] = { 0x00000000, 0x7FF80000 };
extern double const quietNaN = *reinterpret_cast<double const*>(
&nanData[0] );

bool assertQuietNaNIsOk()
{
double const actualQNaN = std::numeric_limits<double>::quiet_NaN();
unsigned const* actualData = reinterpret_cast<unsigned const*>(
&actualQNaN );

assert((
"::quietNan is not the quiet NaN value",
std::equal( nanData, nanData+2, actualData )
));
return true;
}

static bool const TEMPNAME = assertQuietNaNIsOk();
to which some module initialization function must be added to ensure that the
assert at the end is executed.

It seems like a lot of complication for a once-only nanosecond-level
"improvement" compared to just
STATIC_ASSERT( std::numeric_limits<double>::has_quiet_NaN );
double const quietNaN = std::numeric_limits<double>::quiet_NaN();
Heh...

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #4
"Alf P. Steinbach" wrote:

It seems like a lot of complication for a once-only nanosecond-level
"improvement" compared to just

STATIC_ASSERT( std::numeric_limits<double>::has_quiet_NaN );
double const quietNaN = std::numeric_limits<double>::quiet_NaN();


I didn't know about the quiet_Nan in the numeric_limits.
Thanks for bringing this up.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5
Rolf Magnus <ra******@t-online.de> wrote in message news:<c7*************@news.t-online.com>...
Lionel B wrote:
Running VC++ 6 under Win2K on i386.

I would like to assign a (compile-time) constant that resolves to a
quiet NaN (of type double)

I can assign a quiet NaN to a *variable* (of type const double, say)
by eg.:

const double qnan = fmod(1.0,0.0);

but - for efficiency reasons - what I really want is a (compile-time)
constant:

#define QNAN ???
Don't use #define for that. Why do you believe it could be more
efficient than a constant anyway?


I imagined (erroneously?) that there would be an (admittedly
miniscule) overhead in fetching a constant from memory, as compared to
using a hard-coded compile-time constant.

Efficiency *is* an issue for me, because I intend to use quiet NaN as
a value for "missing" data, frequently in vast arrays of doubles (it
has the advantage that arithmetical operations where an argument is a
quiet NaN result in a quiet NaN, which is exactly how I would like
missing data to behave).

So I may frequently have to set zillions of doubles to quiet NaN:

double qnan = std::numeric_limits<double>::quiet_NaN();
...
double* data; // huge array
pend = data+(size of array)
...
for (double* p=data; p!=pend; p++) if (some condition) *p = qnan;
or
for (double* p=data; p!=pend; p++) if (*p is quiet NaN) (do
something);

Apropos of which, what is the most efficient way to test for x = a
quiet NaN...?

_isnan(x)

works (although I guess it might evaluate to true for a signalling NaN
too (probably not a problem), as does

(_fpclass(x) == _FPCLASS_QNAN)

Curiously,

(x == qnan)

appears to evaluate to true for *any* double x (???)

/snip/
Instead, I'd recommend to the standard C++ way to get the quiet NaN:

// #include <limits>
double x = std::numeric_limits<double>::quiet_NaN();


Yes, I guess that is the way to go.

Thanks,

--
Lionel B
Jul 22 '05 #6
* go****@lionelb.com (Lionel B) schriebt:
Curiously,

(x == qnan)

appears to evaluate to true for *any* double x (???)


Except for the nan; that is by definition (IEEE/IEC standard).
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: janeaustine50 | last post by:
Python's InteractiveInterpreter uses the built-in compile function. According to the ref. manual, it doesn't seem to concern about the encoding of the source string. When I hand in an unicode...
1
by: Mario T. Lanza | last post by:
I am working with Visual Studio. The solution I am developing is composed of about 8 separate projects. Some of these projects represent different tiers in the N-tiered architecture (data,...
4
by: frankg | last post by:
When I compile this file ------------------------------------------------------ #include <stdarg.h> template <class Any> void var_arg_func(int dimension_count, ...) { int dimensions;...
0
by: Jordan Willms | last post by:
My xsl stylesheet is as simple as follows: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:ims="http://www.imsglobal.org/xsd/imsmd_v1p2"...
6
by: Thomas Connolly | last post by:
I have 2 pages referencing the same codebehind file in my project. Originally the pages referenced separate code behind files. Once I changed the reference to the same file, everything worked...
4
by: tony | last post by:
Hello! My question is about calling this method CollectData below but I get a compile error that I shouldn't have because the type parameter is correct. The compile error is the following:...
1
by: brianrpsgt1 | last post by:
Newbie here.... I have been able to successful pull info from a MySQL DB, get the results and output them in an HTML format using Cheetah to the screen using IDLE. I am doing this on a Windows...
3
by: NvrBst | last post by:
Right now I have C99 code in .c extensions. I compile it in VSC++ and it complains about a lot of errors. I change the extensions to .cpp and compile in VSC++ and it succeeds. Is there a way...
2
by: Andrus | last post by:
I need compile in-memory assembly which references to other in-memory assembly. Compiling second assembly fails with error Line: 0 - Metadata file 'eed7li9m, Version=0.0.0.0, Culture=neutral,...
6
by: Ed Leafe | last post by:
I've noticed an odd behavior with compile() and code that does not contain a trailing newline: if the last line is a comment inside of any block, a syntax error is thrown, but if the last line is a...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.