Topic: Where should DBL_MAX be defined?


Author: Christopher Eltschka <celtschk@web.de>
Date: Fri, 7 Dec 2001 17:45:51 GMT
Raw View
"James Kuyper Jr." <kuyper@wizard.net> writes:

[...]

> be considered as mutually exclusive. For instance, templates are
> necessarily also either classes or functions.

No. A template is neither a class nor a function.
A class template is a template whose _instantiations_ are classes, and
a function template is a template whose _instantiations_ are functions.

If class templates were classes, you could write

  std::vector v;

but since they are not, you fist have to instantiate them on suitable
arguments, like

  std::vector<int> v;

std::vector<int> is a class, but not a template, std::vector is a
template, but not a class.

For functions, instantiation is generally done implicitly, therefore
the difference is not as easily seen, but it exists, as you can see if
you try to pass a function template to an STL algorithm.

[...]

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Nicola Musatti <objectway@divalsim.it>
Date: Tue, 30 Oct 2001 15:49:53 GMT
Raw View
Hallo,
As "DBL_MAX" has a "macro looking" name, I naively thought that by
including the <cfloat> header this and similar entities would be defined
in the global namespace.

However in the following example, the compiler I'm currently using gives
me a (not too clear) error message, which can be eliminated by
qualifying DBL_MAX with std:: .

#include <iostream>
#include <cfloat>

int main() {
  std::cout << DBL_MAX << '\n';
  return 0;
}

Reading the standard I found out that DBL_MAX is described as a "value"
in C.2/4 and table 96, and that values are defined in the std namespace
(17.4.1.1/2).

Is this the desired behaviour or is it a defect?

Cheers,
Nicola Musatti

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Ron Natalie <ron@sensor.com>
Date: Tue, 30 Oct 2001 18:17:44 GMT
Raw View

Nicola Musatti wrote:
>
> Hallo,
> As "DBL_MAX" has a "macro looking" name, I naively thought that by
> including the <cfloat> header this and similar entities would be defined
> in the global namespace.

It is a MACRO (not just look like one) and defined in cfloat.  The standard
says that the contents of cfloat shall be that of the C float.h.  The names
that are macros remain macros.

Macro's don't belong to the namespace, they're replaced with their values
before the translation of the source files get that far.

>
> However in the following example, the compiler I'm currently using gives
> me a (not too clear) error message, which can be eliminated by
> qualifying DBL_MAX with std:: .

Qualifying DBL_MAX with std:: would most certainly be wrong.  What "not
too clear" error message did you get and from what compiler?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Tue, 30 Oct 2001 18:18:39 GMT
Raw View
"Nicola Musatti" <objectway@divalsim.it> wrote in message
news:3BDE89E7.AD523F1B@divalsim.it...
> Hallo,
> As "DBL_MAX" has a "macro looking" name, I naively thought that by
> including the <cfloat> header this and similar entities would be defined
> in the global namespace.
>
> However in the following example, the compiler I'm currently using gives
> me a (not too clear) error message, which can be eliminated by
> qualifying DBL_MAX with std:: .
>
> #include <iostream>
> #include <cfloat>
>
> int main() {
>   std::cout << DBL_MAX << '\n';
>   return 0;
> }
>
> Reading the standard I found out that DBL_MAX is described as a "value"
> in C.2/4 and table 96, and that values are defined in the std namespace
> (17.4.1.1/2).
>
> Is this the desired behaviour or is it a defect?

It is an error in your implementation.

18.2.2p4:
"The contents [of <cfloat>] are the same as the Standard C library header
<float.h>."

C99 7.7 says
"The header <float.h> defines several macros that expand to various limits
and parameters of the standard floating-point types.

The macros, their meanings, and the constraints (or restrictions) on their
values are listed in 5.2.4.2.2."

where C99 5.2.4.2.2 defines the names of the macros, including DBL_MAX

Therefore IMO DBL_MAX must be a macro in C++, just as in C, so cannot be in
any namespace.

Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optoelectronics
The opinions expressed in this message are not necessarily those of my
employer



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Scott Robert Ladd" <scott@coyotegulch.com>
Date: Tue, 30 Oct 2001 18:57:19 GMT
Raw View
"Nicola Musatti" <objectway@divalsim.it> wrote
> As "DBL_MAX" has a "macro looking" name, I naively thought that by
> including the <cfloat> header this and similar entities would be defined
> in the global namespace.

Macros do not exist inside namespaces, since they are simple text
substitutions performed during preprocessing.

I strongly suggest that you replace instances of DBL_MAX with
numeric_limits<double>.max(), defined in <limits>. This would be the
Standard C++ technique for finding the maximum double value.

--
Scott Robert Ladd
Master of Complexity, Destroyer of Order and Chaos
  Visit CoyoteGulch at http://www.coyotegulch.com
    No ads -- just info, algorithms, and very free code.


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Nicola Musatti <objectway@divalsim.it>
Date: Wed, 31 Oct 2001 12:04:42 GMT
Raw View

Anthony Williams wrote:
[...]
> 18.2.2p4:
> "The contents [of <cfloat>] are the same as the Standard C library header
> <float.h>."

But why then the distinction between macros and values made in 17.4.1.1,
which I quote:
1 The C++ Standard Library provides definitions for the following types
  of entities: Macros, Values, Types, Templates, Classes, Functions,
  Objects.
2 All library entities except macros, operator new and operator delete
  are defined within the namespace std or namespaces nested within
  namespace std.

And in C.2/2-4 (tables omitted):

2 The C++ Standard library provides 54 standard macros from the C
  library, as shown in Table 95.
3 The header names (enclosed in < and >) indicate that the macro may be
  defined in more than one header. All such definitions are equivalent
  (3.2).
4 The C++ Standard library provides 45 standard values from the C
  library, as shown in Table 96

Note that DBL_MAX is in Table 96. By the way, what is a value in this
context?

Cheers,
Nicola Musatti

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Wed, 31 Oct 2001 16:34:45 GMT
Raw View
Nicola Musatti wrote:
>
> Hallo,
> As "DBL_MAX" has a "macro looking" name, I naively thought that by
> including the <cfloat> header this and similar entities would be defined
> in the global namespace.
>
> However in the following example, the compiler I'm currently using gives
> me a (not too clear) error message, which can be eliminated by
> qualifying DBL_MAX with std:: .

Could you show us the exact text of the error message? Possibly someone
here will be able to figure it out.

> #include <iostream>
> #include <cfloat>
>
> int main() {
>   std::cout << DBL_MAX << '\n';
>   return 0;
> }
>
> Reading the standard I found out that DBL_MAX is described as a "value"
> in C.2/4 and table 96, and that values are defined in the std namespace
> (17.4.1.1/2).
>
> Is this the desired behaviour or is it a defect?

That appears to be a defect. DBL_MAX is required to be a macro which
must expand to form a constant expression with the appropriate value.
Therefore, that code is required to work as written.

Furthermore, std::DBL_MAX is necessarily a syntax error. After macro
expansion, the only things that can appear after std:: are member names
of namespace std. If the expansion of DBL_MAX started with the name of
such a member, it wouldn't qualify as a floating point constant
expression.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Wed, 31 Oct 2001 16:35:35 GMT
Raw View
Nicola Musatti wrote:
>
> Anthony Williams wrote:
> [...]
> > 18.2.2p4:
> > "The contents [of <cfloat>] are the same as the Standard C library header
> > <float.h>."
>
> But why then the distinction between macros and values made in 17.4.1.1,
> which I quote:
> 1 The C++ Standard Library provides definitions for the following types
>   of entities: Macros, Values, Types, Templates, Classes, Functions,
>   Objects.
> 2 All library entities except macros, operator new and operator delete
>   are defined within the namespace std or namespaces nested within
>   namespace std.

I don't think you should read too much into that listing. It's a short
summary of everything in the library, the items in the list should not
be considered as mutually exclusive. For instance, templates are
necessarily also either classes or functions. Functions, and objects,
both have values. Values, templates, functions, and objects all have
types. Macros can expand into any of those things.

> And in C.2/2-4 (tables omitted):
>
> 2 The C++ Standard library provides 54 standard macros from the C
>   library, as shown in Table 95.
> 3 The header names (enclosed in < and >) indicate that the macro may be
>   defined in more than one header. All such definitions are equivalent
>   (3.2).
> 4 The C++ Standard library provides 45 standard values from the C
>   library, as shown in Table 96
>
> Note that DBL_MAX is in Table 96. By the way, what is a value in this
> context?

I can't explain the distinction being made between table 95 and table
96, except possibly by saying that table 96 describes macros that all
are required to expand to arithmetic constant expressions. However, some
of the items in table 95 are also required to be arithmetic constant
expressions, so that can't be the distinction. In any event, the forward
to the C++ standard indicates that Annexes A, B and C are purely
informative; if they contain any material that contradicts that differs
from the normative text, it it the normative text that matters.

Section 17.4.1.2p4 is normative, and requires that "... the contents of
each header _cname_ shall be the same as the corresponding header
_name_.h, as specified in ISO/IEC 9899:1990 Programming Languages C
(Clause 7), or ISO/IEC:1990 Programming Languages--C AMENDMENT 1: C
Integrity, (Clause 7) ...". The parts I've left out make some exceptions
to that requirement, but none that are relevant to this case. Therefore,
DBL_MAX must meet the C requirements, which are that it be a macro which
expands to a constant expression with the appropriate value.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Nicola Musatti <objectway@divalsim.it>
Date: Wed, 31 Oct 2001 20:19:03 GMT
Raw View

Ron Natalie wrote:
>
> Nicola Musatti wrote:
[...
> > However in the following example, the compiler I'm currently using gives
> > me a (not too clear) error message, which can be eliminated by
> > qualifying DBL_MAX with std:: .
>
> Qualifying DBL_MAX with std:: would most certainly be wrong.  What "not
> too clear" error message did you get and from what compiler?

The message reports an undefined implementation specific variable and is
caused by the fact that this variable is defined within the std
namespace, while DBL_MAX is a macro defined to the unqualified variable
name.

Cheers,
Nicola Musatti

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Wed, 31 Oct 2001 20:20:14 GMT
Raw View
"Nicola Musatti" <objectway@divalsim.it> wrote in message
news:3BDFE47C.B4AF7EDF@divalsim.it...
>
>
> Anthony Williams wrote:
> [...]
> > 18.2.2p4:
> > "The contents [of <cfloat>] are the same as the Standard C library
header
> > <float.h>."
>
> But why then the distinction between macros and values made in 17.4.1.1,
> which I quote:
> 1 The C++ Standard Library provides definitions for the following types
>   of entities: Macros, Values, Types, Templates, Classes, Functions,
>   Objects.
> 2 All library entities except macros, operator new and operator delete
>   are defined within the namespace std or namespaces nested within
>   namespace std.
>
> And in C.2/2-4 (tables omitted):
>
> 2 The C++ Standard library provides 54 standard macros from the C
>   library, as shown in Table 95.
> 3 The header names (enclosed in < and >) indicate that the macro may be
>   defined in more than one header. All such definitions are equivalent
>   (3.2).
> 4 The C++ Standard library provides 45 standard values from the C
>   library, as shown in Table 96
>
> Note that DBL_MAX is in Table 96. By the way, what is a value in this
> context?

I think that the description of DBL_MAX as a value comes from the C
standard, where it is described as a value (though float.h is said to define
macros).

The only distinction I can see is that some macros (such as DBL_MAX) are
described as values, and must be constant expressions, whereas others are
described as object-like macros (such as WCHAR_MAX) and yet others are just
described as macros (such as errno, or va_start), though they may also be
constant expressions (such as ERANGE).

I see that the distinction is largely that macros which describe constants,
the value of which may matter to a program, are "values", and are defined in
"limits.h" or "float.h", whereas constants that have no meaning other than
that given to them by the runtime library are just "macros". WCHAR_MAX is an
exception, but it lives in "stdint.h" (at least it does in C99)

As to what "values" are being referred to by 17.4.1.1, I am not sure -
things like std::cout are objects, and things like std::string::npos are
class members. Enumerations are values, but they must exist in the namespace
in which the enum type is defined, and Types must be in namespace std.
Values of bitmask types, such as "binary" and "app" for
std::ios_base::openmode might be considered "values" if the corresponding
type was an integral type, rather than an enumeration. However, in such a
case it is unclear to me what scope they should be defined in (std::, or
std::ios_base::). For that matter, it is also unclear what scope an
enumeration should be declared in, provided there is a suitable typedef for
std::ios_base::openmode.

DBL_MAX may evaluate to a reference to a library-defined constant object,
rather than a floating-point constant. I imagine that this constant object
may count as a value that must live in namespace std, though I am not sure.

Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optical Components Ltd
The opinions expressed in this message are not necessarily those of my
employer



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Thu, 1 Nov 2001 12:02:01 GMT
Raw View
Nicola Musatti wrote:
>
> Ron Natalie wrote:
> >
> > Nicola Musatti wrote:
> [...
> > > However in the following example, the compiler I'm currently using gives
> > > me a (not too clear) error message, which can be eliminated by
> > > qualifying DBL_MAX with std:: .
> >
> > Qualifying DBL_MAX with std:: would most certainly be wrong.  What "not
> > too clear" error message did you get and from what compiler?
>
> The message reports an undefined implementation specific variable and is
> caused by the fact that this variable is defined within the std
> namespace, while DBL_MAX is a macro defined to the unqualified variable
> name.

That isn't a conforming implmentation of DBL_MAX. The following code is
required to compile (as a complete translation unit, though not as a
complete program) under any conforming implementation of C++:

 #include <cfloat>
 double d=DBL_MAX;

What does this compiler do with it?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: comeau@panix.com (Greg Comeau)
Date: Thu, 1 Nov 2001 15:10:57 GMT
Raw View
In article <kICD7.464678$aZ.89681464@typhoon.tampabay.rr.com>,
Scott Robert Ladd <scott@coyotegulch.com> wrote:
>"Nicola Musatti" <objectway@divalsim.it> wrote
>> As "DBL_MAX" has a "macro looking" name, I naively thought that by
>> including the <cfloat> header this and similar entities would be defined
>> in the global namespace.
>
>Macros do not exist inside namespaces, since they are simple text
>substitutions performed during preprocessing.

Macros exist inside their own name space, of course, but I know
that's not what you mean (you mean within a namespace { ... })

>I strongly suggest that you replace instances of DBL_MAX with
>numeric_limits<double>.max(), defined in <limits>. This would be the
>Standard C++ technique for finding the maximum double value.

I don't see the code in question, but be careful here,
as that's not always possible since inline functions are
(in part unfortunately) not constant expressions.
--
Greg Comeau         export ETA: December     See our Oct 31st special
Comeau C/C++ ONLINE ==>     http://www.comeaucomputing.com/tryitout
World Class Compilers:  Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]