[comp.std.c] What is permissable with HUGE_VAL

art@dinorah.wustl.edu (Arthur B. Smith) (07/15/90)

Hello,

    I hope this hasn't already been discussed to death -- my reading
of this group has been somewhat spotty.  I am hoping for a
clarification on the Standard.  I have read the relevant part in
section 4.5 (p.112), but still have these questions.

    We have an array of doubles, and need to have a "flag value" in
some of the elements.  If the element has a flag value, then we don't
do anything more with it, otherwise we do.  We had hoped to use
HUGE_VAL as this flag value but upon reading the standard I have 
some questions, best expressed in code.

#include <math.h>
#include <stdio.h>

int main ( int argc, char * argv[] )

  {
  double    flag_val, ok_val, odd_val;

  flag_val = HUGE_VAL;	    /* Question 1: Is this legal? */
  ok_val = 5.2;
  if (flag_val == HUGE_VAL)
    puts("Question 2: Is this guaranteed to print?");
  if (ok_val < HUGE_VAL)
    puts("Question 3: Is this guaranteed to print?");
  odd_val = flag_val / 2.0; /* Question 4: Is this legal? */
  if (odd_val < HUGE_VAL)
    puts("Question 5a: Is this guaranteed to print?");
  else
    puts("Question 5b: Is this guaranteed to print?");
  return(0);
  }

My guesses are:
1) Yes
2) Yes
3) Yes
4) Yes
5a) No
5b) No  (One of 5a or 5b will print, but it is not guaranteed which)

    Is this right?

I guess further that if the above example used DBL_MAX instead of
HUGE_VAL (and included float.h) the answers would be
1) Yes
2) Yes
3) Yes
4) Yes
5a) Yes
6a) No (In fact, it is guaranteed not to print)

    Is this right?

    Please send responses by e-mail, since my reading is spotty.  I
will try to watch this newsgroup, though.  Thanks in advance!

    	-art smith  (art@dinorah.wustl.edu  or
    	    	     ...!uunet!wugate!dinorah!art)

gwyn@smoke.BRL.MIL (Doug Gwyn) (07/15/90)

In article <1990Jul14.194550.14850@dinorah.wustl.edu> art@dinorah.wustl.edu (Arthur B. Smith) writes:
>  flag_val = HUGE_VAL;	    /* Question 1: Is this legal? */
>  odd_val = flag_val / 2.0; /* Question 4: Is this legal? */
>  if (odd_val < HUGE_VAL)
>    puts("Question 5a: Is this guaranteed to print?");
>My guesses are:
>5a) No

That could happen only if HUGE_VAL were defined as an "infinity".
A footnote in the standard claims that that is in fact allowed.
Unfortunately floating-point numbers are not required by the standard
to act sensibly, except in a few cases where they are constrained
very tightly.  This is inconsistent, but you can thank IEEE 754/854
for it.

rbutterworth@watmath.waterloo.edu (Ray Butterworth) (07/20/90)

If I remember the discussion about this from a year or two ago,
there was nothing in the standard indicating what HUGE_VAL needed
to be, other than positive.
i.e.  there is no reason (other than the obvious sillyness) that
an implementation can't define HUGE_VAL as 5.2.

If you think no one would be silly,
consider the BSD definition of HUGE in their <math.h>:

    #define VALUE (4.15383748682786205e34 * 4096)
    
    /* from <math.h> */
    #define HUGE    1.701411733192644270e38
    
    main() {
        auto double huge = HUGE;
        auto double value = VALUE;
    
        if (value > huge)
            printf("value is bigger than huge\n");
        if (value < huge)
            printf("value is less than huge\n");
    }

Because of the nature of floating point it wouldn't be unreasonable
for both conditions to evaluate true, but in this case only the
first prints.  And if you look at the bits in the two variables
you'll see that value really is bigger than huge.

gwyn@smoke.BRL.MIL (Doug Gwyn) (07/24/90)

In article <1990Jul19.204933.18891@watmath.waterloo.edu> rbutterworth@watmath.waterloo.edu (Ray Butterworth) writes:
>i.e.  there is no reason (other than the obvious sillyness) that
>an implementation can't define HUGE_VAL as 5.2.

Yes, HUGE_VAL is not required to be the largest representable value.
However, it should be defined as close to that as is feasible,
simply as a matter of quality of implementation.