Float as Infinity

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • user923005

    #31
    Re: Float as Infinity

    On Sep 26, 12:13 am, The87Boy <the87...@gmail .comwrote:
    On 25 Sep., 20:58, Mark McIntyre <markmcint...@s pamcop.netwrote :
    >
    >
    >
    >
    >
    On Tue, 25 Sep 2007 03:27:07 -0700, in comp.lang.c , The87Boy
    >
    <the87...@gmail .comwrote:
    >
    >How can I initialize valueMin and valueMax
    >
    This is NOT a C question. This is an algorithm question. If you don't
    understand the algo, you cannot safely programme it, so take time to
    understand the algo first. Here's a suggestion:
    >
    Pick ONE number.
    >
    Work out which of it is the min, and which is the max (easy....).
    What are valueMin and valueMax set to now?
    >
    Once you've solved this step, pick a second number, and compare it.
    >
    It isn't the question, but should I set it to "float
    valueMin=-999999999999999 99999999" or?- Hide quoted text -
    >
    - Show quoted text -
    Set valueMin to the largest possible value:
    DBL_MAX
    Set valueMax to the most negative possible value:
    DBL_MIN

    Then the first time you compare something, it will always get
    replaced.

    The start of your program will look like this:

    #include <float.h>
    #include <stdio.h>
    #include <stdlib.h>

    static char string[32767];

    int main(void)
    {
    double valMin = DBL_MAX;
    double valMax = DBL_MIN;

    Comment

    • Richard Heathfield

      #32
      Re: Float as Infinity

      user923005 said:

      <snip>
      Set valueMax to the most negative possible value:
      DBL_MIN
      DBL_MIN is required by the Standard to be positive.

      --
      Richard Heathfield <http://www.cpax.org.uk >
      Email: -http://www. +rjh@
      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
      "Usenet is a strange place" - dmr 29 July 1999

      Comment

      • Eric Sosman

        #33
        Re: Float as Infinity

        user923005 wrote On 09/26/07 05:10,:
        [...]
        Set valueMin to the largest possible value:
        DBL_MAX
        Set valueMax to the most negative possible value:
        DBL_MIN
        Bzzzt! DBL_MIN is a *positive* value. Use
        `-DBL_MAX' or `-HUGE_VAL' or (on systems that have
        it) `-INFINITY'.

        --
        Eric.Sosman@sun .com

        Comment

        • Charlie Gordon

          #34
          Re: Float as Infinity

          "user923005 " <dcorbit@connx. coma écrit dans le message de news:
          1190766357.4447 94.82820@22g200 0h...legrou ps.com...
          On Sep 25, 3:31 pm, pete <pfil...@mindsp ring.comwrote:
          >The87Boy wrote:
          >>
          Hey all
          >>
          I have a problem with float
          I should write a program, where you are getting some numbers from the
          command-line and try to find the maximum and minimum-values of these
          numbers.
          >>
          >The first number entered is both the min and the max.
          >>
          >Any subsequent number entered
          >which is greater than the max, is the new max.
          >>
          >Any subsequent number entered
          >which is less than the min, is the new min.
          >
          Another common way of doing it (especially when you don't have an
          array) is to set the minimum initially to be DBL_MAX and the maximum
          to be DBL_MIN as defined in <float.hfor step 1. I think that this
          is what the O.P. was getting at.
          >
          #include <float.h>
          #include <stdio.h>
          #include <stdlib.h>
          >
          char string[32767];
          int main(void)
          {
          double valMin = DBL_MAX;
          double valMax = DBL_MIN;
          /*
          read loop goes here...
          comparisons reset valMin & valMax
          */
          return 0;
          }
          There seems to be a confusion between the semantics of DBL_MIN and INT_MIN.
          INT_MIN is the negative int with the largest absolute value
          (typically -32768 or -2147483648).
          DBL_MIN is the smallest non-zero positive value that can be represented in a
          double (typically 2.2250738585072 014E-308).
          The equivalent of INT_MIN for a double is -DBL_MAX, not DBL_MIN.

          --
          Chqrlie.


          Comment

          • Eric Sosman

            #35
            Re: Float as Infinity

            Barry Schwarz wrote On 09/25/07 21:37,:
            The87Boy wrote:
            >
            >>I have a problem with float. I should write a program, where you
            >>are getting some numbers from the command-line and try to find the
            >>maximum and minimum-values of these numbers.
            >>I need to initialize 2 floats (one with minus infinity and one with
            >>plus infinity), or does anybody else have an idea?
            >
            >
            If the numbers are integers, use INT_MAX/MIN or LONG_MAX/MIN or
            LLONG_MAX/MIN as appropriate. If they are floating type, use
            FLT_MAX/MIN, DBL_MAX/MIN, or LDBL_MAX/MIN as appropriate.
            For the UINTMAX_MAX'th time, {FLT,DBL,LDBL}_ MIN
            are all *positive* values.

            The mistake is made so frequently that it must be
            easy to make, so let's not make it any easier, okay?

            --
            Eric.Sosman@sun .com

            Comment

            • Charlie Gordon

              #36
              Re: Float as Infinity

              "Eric Sosman" <Eric.Sosman@su n.coma écrit dans le message de news:
              1190818696.4159 40@news1nwk...
              Barry Schwarz wrote On 09/25/07 21:37,:
              >The87Boy wrote:
              >>
              >>>I have a problem with float. I should write a program, where you
              >>>are getting some numbers from the command-line and try to find the
              >>>maximum and minimum-values of these numbers.
              >>>I need to initialize 2 floats (one with minus infinity and one with
              >>>plus infinity), or does anybody else have an idea?
              >>
              >>
              >If the numbers are integers, use INT_MAX/MIN or LONG_MAX/MIN or
              >LLONG_MAX/MIN as appropriate. If they are floating type, use
              >FLT_MAX/MIN, DBL_MAX/MIN, or LDBL_MAX/MIN as appropriate.
              >
              For the UINTMAX_MAX'th time, {FLT,DBL,LDBL}_ MIN
              are all *positive* values.
              >
              The mistake is made so frequently that it must be
              easy to make, so let's not make it any easier, okay?
              The mistake is made so easily and so frequently that it almost qualifies as
              a defect ;-)
              The names are just *so* confusing !

              --
              chqrlie.


              Comment

              • Eric Sosman

                #37
                Re: Float as Infinity

                Charlie Gordon wrote On 09/27/07 05:36,:
                "Eric Sosman" <Eric.Sosman@su n.coma écrit dans le message de news:
                1190818696.4159 40@news1nwk...
                >>
                > For the UINTMAX_MAX'th time, {FLT,DBL,LDBL}_ MIN
                >>are all *positive* values.
                >>
                > The mistake is made so frequently that it must be
                >>easy to make, so let's not make it any easier, okay?

                The mistake is made so easily and so frequently that it almost qualifies as
                a defect ;-)
                The names are just *so* confusing !
                They are; something like DBL_SMALLEST might have
                avoided confusion. (I'm not heaping scorn on the ANSI
                committee for the unfortunate names; I think in this
                case the committee just codified "prior art.") But
                for now and probably for the life of C we're stuck with
                the _MIN names, and must learn to live with them.

                --
                Eric.Sosman@sun .com

                Comment

                • user923005

                  #38
                  Re: Float as Infinity

                  On Sep 27, 7:16 am, Eric Sosman <Eric.Sos...@su n.comwrote:
                  Charlie Gordon wrote On 09/27/07 05:36,:
                  >
                  "Eric Sosman" <Eric.Sos...@su n.coma écrit dans le message de news:
                  1190818696.4159 40@news1nwk...
                  >
                  For the UINTMAX_MAX'th time, {FLT,DBL,LDBL}_ MIN
                  >are all *positive* values.
                  >
                  The mistake is made so frequently that it must be
                  >easy to make, so let's not make it any easier, okay?
                  >
                  The mistake is made so easily and so frequently that it almost qualifies as
                  a defect ;-)
                  The names are just *so* confusing !
                  >
                  They are; something like DBL_SMALLEST might have
                  avoided confusion. (I'm not heaping scorn on the ANSI
                  committee for the unfortunate names; I think in this
                  case the committee just codified "prior art.") But
                  for now and probably for the life of C we're stuck with
                  the _MIN names, and must learn to live with them.
                  Believe it or not, I know better.

                  Anyway, here is my eventual solution (I sent a copy in to Snippets):

                  #include <stdlib.h>
                  /*
                  "Introducti on to Algorithms"
                  Cormen, Leiserson, Rivest
                  pp. 186,187
                  ISBN: 0-07-013143-0

                  Simultaneous min & max
                  using only 3*N/2 comparisons

                  Written by Dann Corbit
                  9/25/2007
                  Donated to the public domain
                  */
                  #ifdef e_type_LONG_DOU BLE
                  typedef long double e_type;
                  #elif defined(e_type_ DOUBLE)
                  typedef double e_type;
                  #elif defined(e_type_ FLOAT)
                  typedef float e_type;
                  #elif defined(e_type_ UNSIGNED_LONG_L ONG)
                  typedef unsigned long long e_type;
                  #elif defined(e_type_ LONG_LONG)
                  typedef long long e_type;
                  #elif defined(e_type_ UNSIGNED_LONG)
                  typedef unsigned long e_type;
                  #elif defined(e_type_ LONG)
                  typedef long e_type;
                  #elif defined(e_type_ UNSIGNED)
                  typedef unsigned e_type;
                  #elif defined(e_type_ INT)
                  typedef int e_type;
                  #elif defined(e_type_ SHORT)
                  typedef short e_type;
                  #elif defined(e_type_ UNSIGNED_SHORT)
                  typedef unsigned e_type;
                  #elif defined(e_type_ CHAR)
                  typedef char e_type;
                  #elif defined(e_type_ UNSIGNED_CHAR)
                  typedef unsigned char e_type;
                  #elif defined (__cplusplus)
                  template < class e_type // works with stl string class etc...
                  #endif
                  void minmax(
                  e_type * a, // input array
                  size_t arr_size, // array length
                  e_type * min_e, // smallest thing found
                  e_type * max_e // biggest thing found
                  )
                  {
                  e_type min_et;
                  e_type max_et;
                  size_t i,
                  n;
                  if (arr_size % 2) {
                  min_et = a[0];
                  max_et = a[0];
                  n = 1;
                  } else {
                  if (a[0] a[1]) {
                  max_et = a[0];
                  min_et = a[1];
                  } else {
                  min_et = a[0];
                  max_et = a[1];
                  }
                  n = 2;
                  }
                  for (i = n; i < arr_size; i += 2) {

                  if (a[i] a[i + 1]) {
                  max_et = max_et a[i] ? max_et : a[i];
                  min_et = min_et < a[i + 1] ? min_et : a[i + 1];
                  } else {
                  max_et = max_et a[i + 1] ? max_et : a[i + 1];
                  min_et = min_et < a[i] ? min_et : a[i];
                  }
                  }
                  *min_e = min_et;
                  *max_e = max_et;
                  }

                  #if defined( UNIT_TEST ) && (defined (e_type_DOUBLE) ||
                  defined( __cplusplus))
                  #include <stdio.h>

                  char string[32767];
                  double foo[32767];
                  int main(void)
                  {
                  size_t i = 0;
                  double dmin,
                  dmax;
                  while (fgets(string, sizeof string, stdin)) {
                  foo[i++] = atof(string);
                  if (i 32766)
                  break;
                  }
                  minmax(foo, i, &dmin, &dmax);
                  printf("min=%f, max=%f\n", dmin, dmax);
                  return 0;
                  }
                  #endif


                  Comment

                  Working...