473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

float.h and math.h

Folks,
Well I am still dabling in the mire of math.h and getting on reasonably
well. A couple of questions.

Firstly when defining some of the extreme values in the many bits of code
seem to use "extern" definitions of various types. Is it OK to do something
like this in float.h:-

extern _ex_dbl_vals[3];
DBL_MAX _ex_dbl_vals[1];

and then have an assembler table that sets up _ex_dbl_vals, or does it have
to go via a function?

Secondly, is NAN a required value. I know GCC is not "gospel" but I see that
the IBM 370 parts assume that NAN does not exist on this architecture? And
if there is not a natural NAN would isnan() always return 0?

Dave.
May 18 '06 #1
8 2408
David Wade wrote:
Firstly when defining some of the extreme values in the many bits of code
seem to use "extern" definitions of various types. Is it OK to do something
like this in float.h:-

extern _ex_dbl_vals[3];
DBL_MAX _ex_dbl_vals[1];
I assume you know that it is the implementor's responsibility, not the
computer user to do this. For the second line, do you mean
#define DBL_MAX _ex_dbl_vals[1]
?

To answer your question, no, that is not conforming because DBL_MAX must
be a constant expression, which that is not. Specifically, the compiler
must be able to access the value.
Secondly, is NAN a required value.


Not in Standard C. Its behavior is specified, but it is optional.

--
Thad
May 20 '06 #2

"Thad Smith" <Th*******@acm.org> wrote in message
news:44*********************@auth.newsreader.octan ews.com...
David Wade wrote:
Firstly when defining some of the extreme values in the many bits of code seem to use "extern" definitions of various types. Is it OK to do something like this in float.h:-

extern _ex_dbl_vals[3];
DBL_MAX _ex_dbl_vals[1];
I assume you know that it is the implementor's responsibility, not the
computer user to do this.


Yes I have my implemtors hat on ...
For the second line, do you mean
#define DBL_MAX _ex_dbl_vals[1]
?
I think I meant

extern double ex_dbl_vals[3];
#define DBL_MAX ex_dbl_vals[1];


To answer your question, no, that is not conforming because DBL_MAX must
be a constant expression, which that is not. Specifically, the compiler
must be able to access the value.
If thats true most of the float.h files I have looked at are non-complient.
The standard says (of float.h):-

"FLT_RADIX shall be a constant expression suitable for use in #if
preprocessing directives; all other values need not be constant
expressions."
Secondly, is NAN a required value.
Not in Standard C. Its behavior is specified, but it is optional.


Thats one thing I don't need to do then....

--
Thad

May 20 '06 #3
David Wade wrote:
"Thad Smith" <Th*******@acm.org> wrote in message
news:44*********************@auth.newsreader.octan ews.com...
David Wade wrote:
Firstly when defining some of the extreme values in the many bits of code seem to use "extern" definitions of various types. Is it OK to do something like this in float.h:-

extern _ex_dbl_vals[3];
DBL_MAX _ex_dbl_vals[1];


I assume you know that it is the implementor's responsibility, not the
computer user to do this.


Yes I have my implemtors hat on ...
For the second line, do you mean
#define DBL_MAX _ex_dbl_vals[1]
?


I think I meant

extern double ex_dbl_vals[3];
#define DBL_MAX ex_dbl_vals[1];


I certainly hope not, you are using a name that is reserved for the
user and your macro definition is broken.

To answer your question, no, that is not conforming because DBL_MAX must
be a constant expression, which that is not. Specifically, the compiler
must be able to access the value.


If thats true most of the float.h files I have looked at are non-complient.
The standard says (of float.h):-

"FLT_RADIX shall be a constant expression suitable for use in #if
preprocessing directives; all other values need not be constant
expressions."


That was the C90 wording, C99 is more strict to allow values like
DBL_MAX to be used as initializers for static variables.
Secondly, is NAN a required value.


Not in Standard C. Its behavior is specified, but it is optional.


Thats one thing I don't need to do then....


Robert Gamble

May 21 '06 #4

"Robert Gamble" <rg*******@gmail.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
David Wade wrote:
"Thad Smith" <Th*******@acm.org> wrote in message
news:44*********************@auth.newsreader.octan ews.com...
David Wade wrote:> > code
> seem to use "extern" definitions of various types. Is it OK to do

> Firstly when defining some of the extreme values in the many bits of
something
> like this in float.h:-
>
> extern _ex_dbl_vals[3];
> DBL_MAX _ex_dbl_vals[1];

I assume you know that it is the implementor's responsibility, not the
computer user to do this.


Yes I have my implemtors hat on ...
For the second line, do you mean
#define DBL_MAX _ex_dbl_vals[1]
?


I think I meant

extern double ex_dbl_vals[3];
#define DBL_MAX ex_dbl_vals[1];


I certainly hope not, you are using a name that is reserved for the
user and your macro definition is broken.


Ooops, I meant _EX...

To answer your question, no, that is not conforming because DBL_MAX must be a constant expression, which that is not. Specifically, the compiler must be able to access the value.


If thats true most of the float.h files I have looked at are non-complient.
The standard says (of float.h):-

"FLT_RADIX shall be a constant expression suitable for use in #if
preprocessing directives; all other values need not be constant
expressions."


That was the C90 wording, C99 is more strict to allow values like
DBL_MAX to be used as initializers for static variables.


So how do more modern ".h" files get round this?. Even if I do somthing link
this:-

typedef union {unsigned short _Shrt [4]; double _Dbl[4];}_DblShrt;
const _DblShrt _FltMax = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
#define DBL_MAX (_FltMax._Dbl)

GCC 331 won't let me use DBL_MAX to init storage.

> Secondly, is NAN a required value.

Not in Standard C. Its behavior is specified, but it is optional.


Thats one thing I don't need to do then....


Robert Gamble

May 21 '06 #5
David Wade wrote:
"Robert Gamble" <rg*******@gmail.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
David Wade wrote:
"Thad Smith" <Th*******@acm.org> wrote in message
news:44*********************@auth.newsreader.octan ews.com...
> David Wade wrote:> > code
> > seem to use "extern" definitions of various types. Is it OK to do
>
> > Firstly when defining some of the extreme values in the many bits of something
> > like this in float.h:-
> >
> > extern _ex_dbl_vals[3];
> > DBL_MAX _ex_dbl_vals[1];
>
> I assume you know that it is the implementor's responsibility, not the
> computer user to do this.

Yes I have my implemtors hat on ...

> For the second line, do you mean
> #define DBL_MAX _ex_dbl_vals[1]
> ?

I think I meant

extern double ex_dbl_vals[3];
#define DBL_MAX ex_dbl_vals[1];
I certainly hope not, you are using a name that is reserved for the
user and your macro definition is broken.


Ooops, I meant _EX...
>
> To answer your question, no, that is not conforming because DBL_MAX must > be a constant expression, which that is not. Specifically, the compiler > must be able to access the value.

If thats true most of the float.h files I have looked at are non-complient. The standard says (of float.h):-

"FLT_RADIX shall be a constant expression suitable for use in #if
preprocessing directives; all other values need not be constant
expressions."


That was the C90 wording, C99 is more strict to allow values like
DBL_MAX to be used as initializers for static variables.


So how do more modern ".h" files get round this?.


Something like:
#define DBL_MAX 1.7976931348623157e+308
or
#define DBL_MAX 0X1.fffffffffffffP1023
Even if I do somthing link this:-
typedef union {unsigned short _Shrt [4]; double _Dbl[4];}_DblShrt;
const _DblShrt _FltMax = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
#define DBL_MAX (_FltMax._Dbl)
Yuck.
GCC 331 won't let me use DBL_MAX to init storage.


That's because it's not a constant expression.

Robert Gamble

May 21 '06 #6

"Robert Gamble" <rg*******@gmail.com> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
David Wade wrote:
"Robert Gamble" <rg*******@gmail.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
David Wade wrote:
> "Thad Smith" <Th*******@acm.org> wrote in message
> news:44*********************@auth.newsreader.octan ews.com...
> > David Wade wrote:> > code
> > > seem to use "extern" definitions of various types. Is it OK to do > >
> > > Firstly when defining some of the extreme values in the many bits of
> something
> > > like this in float.h:-
> > >
> > > extern _ex_dbl_vals[3];
> > > DBL_MAX _ex_dbl_vals[1];
> >
> > I assume you know that it is the implementor's responsibility, not
the > > computer user to do this.
>
> Yes I have my implemtors hat on ...
>
> > For the second line, do you mean
> > #define DBL_MAX _ex_dbl_vals[1]
> > ?
>
> I think I meant
>
> extern double ex_dbl_vals[3];
> #define DBL_MAX ex_dbl_vals[1];

I certainly hope not, you are using a name that is reserved for the
user and your macro definition is broken.

Ooops, I meant _EX...
> >
> > To answer your question, no, that is not conforming because

DBL_MAX must
> > be a constant expression, which that is not. Specifically, the

compiler
> > must be able to access the value.
>
> If thats true most of the float.h files I have looked at are

non-complient.
> The standard says (of float.h):-
>
> "FLT_RADIX shall be a constant expression suitable for use in #if
> preprocessing directives; all other values need not be constant
> expressions."

That was the C90 wording, C99 is more strict to allow values like
DBL_MAX to be used as initializers for static variables.


So how do more modern ".h" files get round this?.


Something like:
#define DBL_MAX 1.7976931348623157e+308
or
#define DBL_MAX 0X1.fffffffffffffP1023


The hex floats are "new" in C99 aren't they.....
Even if I do somthing link this:-
typedef union {unsigned short _Shrt [4]; double _Dbl[4];}_DblShrt;
const _DblShrt _FltMax = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
#define DBL_MAX (_FltMax._Dbl)
Yuck.
GCC 331 won't let me use DBL_MAX to init storage.


That's because it's not a constant expression.


As opposed to being a constant value? This stuff gets yukier the more I look
at it...
And as GCC 331 is still C89 I guess that the sort of fudges I am using above
will have to stay....
<< and I am stuck with 331 because I am using an obsoleted platform>>>

Robert Gamble


Thanks for your input,
Dave.
May 21 '06 #7
David Wade wrote:
"Robert Gamble" <rg*******@gmail.com> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
David Wade wrote:
"Robert Gamble" <rg*******@gmail.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
> David Wade wrote:
> > "Thad Smith" <Th*******@acm.org> wrote in message
> > news:44*********************@auth.newsreader.octan ews.com...
> > > David Wade wrote:> > code
> > > > seem to use "extern" definitions of various types. Is it OK to do > > >
> > > > Firstly when defining some of the extreme values in the many bits of
> > something
> > > > like this in float.h:-
> > > >
> > > > extern _ex_dbl_vals[3];
> > > > DBL_MAX _ex_dbl_vals[1];
> > >
> > > I assume you know that it is the implementor's responsibility, not the > > > computer user to do this.
> >
> > Yes I have my implemtors hat on ...
> >
> > > For the second line, do you mean
> > > #define DBL_MAX _ex_dbl_vals[1]
> > > ?
> >
> > I think I meant
> >
> > extern double ex_dbl_vals[3];
> > #define DBL_MAX ex_dbl_vals[1];
>
> I certainly hope not, you are using a name that is reserved for the
> user and your macro definition is broken.
>

Ooops, I meant _EX...

> > >
> > > To answer your question, no, that is not conforming because DBL_MAX must
> > > be a constant expression, which that is not. Specifically, the
compiler
> > > must be able to access the value.
> >
> > If thats true most of the float.h files I have looked at are
non-complient.
> > The standard says (of float.h):-
> >
> > "FLT_RADIX shall be a constant expression suitable for use in #if
> > preprocessing directives; all other values need not be constant
> > expressions."
>
> That was the C90 wording, C99 is more strict to allow values like
> DBL_MAX to be used as initializers for static variables.
>

So how do more modern ".h" files get round this?.
Something like:
#define DBL_MAX 1.7976931348623157e+308
or
#define DBL_MAX 0X1.fffffffffffffP1023


The hex floats are "new" in C99 aren't they.....
Even if I do somthing link this:-
typedef union {unsigned short _Shrt [4]; double _Dbl[4];}_DblShrt;
const _DblShrt _FltMax = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
#define DBL_MAX (_FltMax._Dbl)


Yuck.
GCC 331 won't let me use DBL_MAX to init storage.


That's because it's not a constant expression.


As opposed to being a constant value?


Right, a const qualified type isn't a constant expression in C.
This stuff gets yukier the more I look
at it...
And as GCC 331 is still C89 I guess that the sort of fudges I am using above
will have to stay....
<< and I am stuck with 331 because I am using an obsoleted platform>>>


gcc 3.3.1 supports much of the C99 Standard. In gcc's float.h, DBL_MAX
expands to __DBL_MAX__ which is a built-in macro expanding to (on my
system) 1.7976931348623157e+308. The only reason for this round-about
method (AKAIK) is portability, the same float.h can be used for
multiple platforms without change. What this means though is that the
float.h provided by gcc won't work with a preprocessor that doesn't
define __DBL_MAX__ accordingly, the header is tied closely to the
compiler. What exactly are you trying to accomplish?

Robert Gamble

May 21 '06 #8

"Robert Gamble" <rg*******@gmail.com> wrote in message
news:11*********************@j55g2000cwa.googlegro ups.com...
David Wade wrote:
"Robert Gamble" <rg*******@gmail.com> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
David Wade wrote:
> "Robert Gamble" <rg*******@gmail.com> wrote in message
> news:11*********************@j33g2000cwa.googlegro ups.com...
> > David Wade wrote:
> > > "Thad Smith" <Th*******@acm.org> wrote in message
> > > news:44*********************@auth.newsreader.octan ews.com...
> > > > David Wade wrote:> > code
> > > > > seem to use "extern" definitions of various types. Is it OK to
do
> > > >
> > > > > Firstly when defining some of the extreme values in the many bits of
>
> > > something
> > > > > like this in float.h:-
> > > > >
> > > > > extern _ex_dbl_vals[3];
> > > > > DBL_MAX _ex_dbl_vals[1];
> > > >
> > > > I assume you know that it is the implementor's responsibility,
not the
> > > > computer user to do this.
> > >
> > > Yes I have my implemtors hat on ...
> > >
> > > > For the second line, do you mean
> > > > #define DBL_MAX _ex_dbl_vals[1]
> > > > ?
> > >
> > > I think I meant
> > >
> > > extern double ex_dbl_vals[3];
> > > #define DBL_MAX ex_dbl_vals[1];
> >
> > I certainly hope not, you are using a name that is reserved for
the > > user and your macro definition is broken.
> >
>
> Ooops, I meant _EX...
>
> > > >
> > > > To answer your question, no, that is not conforming because

DBL_MAX
> must
> > > > be a constant expression, which that is not. Specifically, the > compiler
> > > > must be able to access the value.
> > >
> > > If thats true most of the float.h files I have looked at are
> non-complient.
> > > The standard says (of float.h):-
> > >
> > > "FLT_RADIX shall be a constant expression suitable for use in #if > > > preprocessing directives; all other values need not be constant
> > > expressions."
> >
> > That was the C90 wording, C99 is more strict to allow values like
> > DBL_MAX to be used as initializers for static variables.
> >
>
> So how do more modern ".h" files get round this?.

Something like:
#define DBL_MAX 1.7976931348623157e+308
or
#define DBL_MAX 0X1.fffffffffffffP1023


The hex floats are "new" in C99 aren't they.....

> Even if I do somthing link this:-
> typedef union {unsigned short _Shrt [4]; double _Dbl[4];}_DblShrt;
> const _DblShrt _FltMax = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
> #define DBL_MAX (_FltMax._Dbl)

Yuck.

> GCC 331 won't let me use DBL_MAX to init storage.

That's because it's not a constant expression.


As opposed to being a constant value?


Right, a const qualified type isn't a constant expression in C.
This stuff gets yukier the more I look
at it...
And as GCC 331 is still C89 I guess that the sort of fudges I am using

above will have to stay....
<< and I am stuck with 331 because I am using an obsoleted platform>>>


gcc 3.3.1 supports much of the C99 Standard. In gcc's float.h, DBL_MAX
expands to __DBL_MAX__ which is a built-in macro expanding to (on my
system) 1.7976931348623157e+308. The only reason for this round-about
method (AKAIK) is portability, the same float.h can be used for
multiple platforms without change. What this means though is that the
float.h provided by gcc won't work with a preprocessor that doesn't
define __DBL_MAX__ accordingly, the header is tied closely to the
compiler. What exactly are you trying to accomplish?

/* you may want to skip this */
What I REALLY want to acomplish is to provide a REXX interpreter on the
VM/370 (old free version of IBM Mainframe OS)under the Hercules emulator, to
make it more like modern versions which as an individual I can't license.
There are a couple free REXX interpreters, but of course they are written in
"C"....
/* end of epehemera */

So I need a "C" compiler that will, at least cross compile to the I370 (not
370/XA, ESA, I390 or X/Series) architecture and old (pre 4.0) GCC's will do
this. However there were some issues with floating point in the original
port, so it is rather lacking in the FP area. In particular math.h and
float.h are somewhat minimal. I was using some one elses library and
became a sad bunny when I looked in float.h and found it was basically the
minimum spec from the ANSI standard.As I370 uses HEX floating point its was
somewhat broken. I have fixed most of it but I am having problems with
DBL_MIN and DBL_EPSILON. GCC seems to underflow and puts "0.0e0" in the
assember output when compiling the decimal equivalent. I don't feel strong
enough to delve into GCCs "real.c" again....

I do have a copy of "The Standard "C" Library" but in some ways that makes
things worse because I don't want to infringe copyright. (also Hastings and
Cody & Waite)....
Robert Gamble


Thanks for your interest,
Dave.
May 21 '06 #9

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

Similar topics

6
by: Dave win | last post by:
Hi all: I'm confused with the expression "(float *())". Book says that this is a cast. But, I have no idea of this expr. why could this expr ignore the variable??? Thanx!!!
6
by: James Thurley | last post by:
According to the docs, floats are 32 bit and doubles are 64 bit. So using floats should be faster than using doubles on a 32 bit processor, and my tests confirm this. However, most of the Math...
3
by: Neils Christoffersen | last post by:
Hey all, I wrote on Friday asking for some help moving a common subclass field up to the base class (original post and followup included below). This entails storing whole numbers inside float...
17
by: kiplring | last post by:
float sum = (float)Math.Sqrt( floatA*floatA + floatB*floatB); I'm using DirectX with c#. But the Math class in .net framework has a problem. It is "double" base! So I'm doing type casting...
3
by: Terje Barman | last post by:
Hi! I have sumarized float values and I'd like to round the sum to a certain precision. Say I have 1.234 and want it rounded to 1.23. Which method can I use? Terje
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
7
by: shellon | last post by:
Hi all: I want to convert the float number to sortable integer, like the function float2rawInt() in java, but I don't know the internal expression of float, appreciate your help!
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
5
by: zunbeltz | last post by:
Hi, For compatibility reasons with an old program I have to format string in exponential format with the following format 0.xxxxxE-yy This means that the number start always by 0 and after...
3
by: Arnie | last post by:
Folks, We ran into a pretty significant performance penalty when casting floats. We've identified a code workaround that we wanted to pass along but also was wondering if others had experience...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.