TAking the minimum of three values

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

    TAking the minimum of three values

    I need to find a minimum of three float values.. what would be the most
    efficient way of doing this? Can someone please share a #define macro or
    something with me for doing this? Thanks

    Sona

  • Howard

    #2
    Re: TAking the minimum of three values


    "Sona" <sona.gardner@n ospam.com> wrote in message
    news:3f5e1b20$1 @clarion.carno. net.au...[color=blue]
    > I need to find a minimum of three float values.. what would be the most
    > efficient way of doing this? Can someone please share a #define macro or
    > something with me for doing this? Thanks
    >
    > Sona
    >[/color]

    x = min( a, min( b, c ) );

    -Howard




    Comment

    • Ron Natalie

      #3
      Re: TAking the minimum of three values

      Sona wrote:[color=blue]
      > I need to find a minimum of three float values.. what would be the most
      > efficient way of doing this? Can someone please share a #define macro or
      > something with me for doing this? Thanks
      >[/color]

      In C++

      #include <algoritm>

      double min3(double a, double b, double c) {
      return std::min(a, std::min(b, c));
      }


      Comment

      • Vincent Lascaux

        #4
        Re: TAking the minimum of three values

        > #include <algoritm>[color=blue]
        >
        > double min3(double a, double b, double c) {
        > return std::min(a, std::min(b, c));
        > }[/color]

        To keep the templated aspect of min, you could even write

        namespace
        {
        template<class T>
        T& min(T const& a, T const& b, T const& c)
        {
        return min(a, min(b,c));
        }
        }

        Like that std::min works with any type, and with 2 or 3 arguments


        Comment

        • Steve Zimmerman

          #5
          Re: TAking the minimum of three values

          Sona wrote:
          [color=blue]
          > I need to find a minimum of three float values.. what would be the most
          > efficient way of doing this? Can someone please share a #define macro or
          > something with me for doing this? Thanks
          >
          > Sona
          >[/color]

          Sona,

          Thank you for your post.

          --Steve

          #include <stdio.h>

          float min(float h, float j);

          int main()
          {
          float a, b, c;

          printf("Enter three float values, separated by spaces: ");
          scanf("%f%f%f", &a, &b, &c);
          /*
          * NOTE: I had to round the floats off to two decimal places
          * in the following printf statement, because I was getting
          * weird errors otherwise.
          */
          printf("The minimum of these three is %.2f\n", min(a, min(b, c)));

          return 0;
          }

          float min(float h, float j)
          {
          if (h < j)
          return h;
          else
          return j;
          }

          Comment

          • Irrwahn Grausewitz

            #6
            Re: TAking the minimum of three values

            [cross-posting to c.l.c++ fixed]

            Steve Zimmerman <stevetux@sonic .net> wrote:[color=blue]
            >Sona wrote:[color=green]
            >> I need to find a minimum of three float values [...][/color][/color]
            <SNIP>

            Why not make use of the standard C library?
            Code modified:

            #include <stdio.h>
            #include <math.h> /* for fminf() */

            int main( void )
            {
            float a, b, c;

            printf("Enter three float values, separated by spaces: ");
            scanf("%f%f%f", &a, &b, &c);
            printf("The minimum is: %f\n", fminf(a, fminf(b, c)) );

            return 0;
            }

            <SNIP>

            --
            Close your eyes and press escape three times.

            Comment

            • Peter Nilsson

              #7
              Re: TAking the minimum of three values

              "Howard" <alicebt@hotmai l.com> wrote in message news:<bjl7cq$rl f@dispatch.conc entric.net>...[color=blue]
              > "Sona" <sona.gardner@n ospam.com> wrote in message
              > news:3f5e1b20$1 @clarion.carno. net.au...[color=green]
              > > I need to find a minimum of three float values.. what would be the most
              > > efficient way of doing this? Can someone please share a #define macro or
              > > something with me for doing this? Thanks
              > >
              > > Sona[/color]
              >
              > x = min( a, min( b, c ) );[/color]

              Given a typical definition of min(), that will not produce an efficient result.

              #define min3(a,b,c) \
              ( (a) < (b) ? min(a,c) : min(b,c) )

              --
              Peter

              Comment

              • Martin Ambuhl

                #8
                Re: TAking the minimum of three values

                Peter Nilsson wrote:
                [color=blue]
                > "Howard" <alicebt@hotmai l.com> wrote in message news:<bjl7cq$rl f@dispatch.conc entric.net>...[/color]
                [color=blue][color=green]
                >>x = min( a, min( b, c ) );[/color]
                >
                >
                > Given a typical definition of min(), that will not produce an efficient result.
                >
                > #define min3(a,b,c) \
                > ( (a) < (b) ? min(a,c) : min(b,c) )[/color]

                Have you counted the number of evaluations of a or b this involves? There
                is a good reason for preferring functions over defines.


                --
                Martin Ambuhl

                Comment

                • Jirka Klaue

                  #9
                  Re: TAking the minimum of three values

                  Irrwahn Grausewitz wrote:[color=blue]
                  > Steve Zimmerman <stevetux@sonic .net> wrote:[color=green]
                  >>Sona wrote:
                  >>[color=darkred]
                  >>>I need to find a minimum of three float values [...][/color][/color]
                  >
                  > Why not make use of the standard C library?
                  > Code modified:
                  >
                  > #include <stdio.h>
                  > #include <math.h> /* for fminf() */[/color]

                  Is this in C89, too? If not, the standard C library provides qsort.
                  It is not *that* efficient for 3 values, but in return it's scalable.


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

                  static int cmp(const void *a, const void *b)
                  {
                  return *(float *)a < *(float *)b ? -1 : 1; /* Is 0 really required? */
                  }

                  int main( void )
                  {
                  float v[] = {2.3, 2.4, 2.299};

                  qsort(v, sizeof v / sizeof *v, sizeof *v, cmp);
                  printf("The minimum is: %f\n", *v);

                  return 0;
                  }

                  Jirka

                  Comment

                  • Irrwahn Grausewitz

                    #10
                    Re: TAking the minimum of three values

                    Martin Ambuhl <mambuhl@earthl ink.net> wrote:[color=blue]
                    >Peter Nilsson wrote:[color=green]
                    >>
                    >> #define min3(a,b,c) \
                    >> ( (a) < (b) ? min(a,c) : min(b,c) )[/color]
                    >
                    >Have you counted the number of evaluations of a or b this involves? There
                    >is a good reason for preferring functions over defines.[/color]

                    Not to mention possible side effects; consider:

                    float m3, x, y, z;
                    m3 = min3( x *= 3.142, y /= 2.718, z += 1.111 );


                    Irrwahn
                    --
                    Computer: a million morons working at the speed of light.

                    Comment

                    • Carsten Hansen

                      #11
                      Re: TAking the minimum of three values


                      "Sona" <sona.gardner@n ospam.com> wrote in message
                      news:3f5e1b20$1 @clarion.carno. net.au...[color=blue]
                      > I need to find a minimum of three float values.. what would be the most
                      > efficient way of doing this? Can someone please share a #define macro or
                      > something with me for doing this? Thanks
                      >
                      > Sona
                      >[/color]

                      You want a macro. Here you go:
                      #define min3(x, y, z) \
                      ((x) < (y)) ? (((y) < (z)) ? (y) : (((x) < (z)) ? (z) : (x))) : (((x) < (z))
                      ? (x) : (((y) < (z)) ? (z) : (y)))

                      You can't beat that for efficiency. Of course it is totally unreadable. But
                      you asked for it :-)

                      Carsten Hansen



                      Comment

                      • Jirka Klaue

                        #12
                        Re: TAking the minimum of three values

                        Carsten Hansen wrote:[color=blue]
                        > "Sona" <sona.gardner@n ospam.com> wrote in message
                        > news:3f5e1b20$1 @clarion.carno. net.au...
                        >[color=green]
                        >>I need to find a minimum of three float values.. what would be the most
                        >>efficient way of doing this? Can someone please share a #define macro or
                        >>something with me for doing this? Thanks[/color]
                        >
                        > You want a macro. Here you go:
                        > #define min3(x, y, z) \
                        > ((x) < (y)) ? (((y) < (z)) ? (y) : (((x) < (z)) ? (z) : (x))) : (((x) < (z))
                        > ? (x) : (((y) < (z)) ? (z) : (y)))
                        >
                        > You can't beat that for efficiency.[/color]

                        It is not even correct.

                        float min3(float x, float y, float z)
                        {
                        if (x < y)
                        if (x < z) return x;
                        if (y < z) return y;
                        return z;
                        }

                        Jirka

                        Comment

                        • Irrwahn Grausewitz

                          #13
                          Re: TAking the minimum of three values

                          "Carsten Hansen" <hansen.c@world net.att.net> wrote:[color=blue]
                          >
                          >You want a macro. Here you go:
                          >#define min3(x, y, z) \
                          >((x) < (y)) ? (((y) < (z)) ? (y) : (((x) < (z)) ? (z) : (x))) : (((x) < (z))
                          >? (x) : (((y) < (z)) ? (z) : (y)))
                          >[/color]

                          *urk*
                          --
                          Computer: a million morons working at the speed of light.

                          Comment

                          • Carsten Hansen

                            #14
                            Re: TAking the minimum of three values


                            "Jirka Klaue" <jklaue@ee.tu-berlin.de> wrote in message
                            news:bjlui2$b09 $1@mamenchi.zrz .TU-Berlin.DE...[color=blue]
                            > Carsten Hansen wrote:[color=green]
                            > > "Sona" <sona.gardner@n ospam.com> wrote in message
                            > > news:3f5e1b20$1 @clarion.carno. net.au...
                            > >[color=darkred]
                            > >>I need to find a minimum of three float values.. what would be the most
                            > >>efficient way of doing this? Can someone please share a #define macro or
                            > >>something with me for doing this? Thanks[/color]
                            > >
                            > > You want a macro. Here you go:
                            > > #define min3(x, y, z) \
                            > > ((x) < (y)) ? (((y) < (z)) ? (y) : (((x) < (z)) ? (z) : (x))) : (((x) <[/color][/color]
                            (z))[color=blue][color=green]
                            > > ? (x) : (((y) < (z)) ? (z) : (y)))
                            > >
                            > > You can't beat that for efficiency.[/color]
                            >
                            > It is not even correct.
                            >
                            > float min3(float x, float y, float z)
                            > {
                            > if (x < y)
                            > if (x < z) return x;
                            > if (y < z) return y;
                            > return z;
                            > }
                            >
                            > Jirka
                            >[/color]

                            Give an example where it fails.

                            Carsten Hansen


                            Comment

                            • Jeff

                              #15
                              Re: TAking the minimum of three values


                              "Ron Natalie" <ron@sensor.com > wrote in message
                              news:3f5e256a$0 $46555$9a6e19ea @news.newshosti ng.com...[color=blue]
                              > Sona wrote:[color=green]
                              > > I need to find a minimum of three float values.. what would be the most
                              > > efficient way of doing this? Can someone please share a #define macro or
                              > > something with me for doing this? Thanks
                              > >[/color]
                              >
                              > In C++
                              >
                              > #include <algoritm>
                              >
                              > double min3(double a, double b, double c) {
                              > return std::min(a, std::min(b, c));
                              > }
                              >
                              >[/color]


                              Please do not post off-topic things here. It waste the bandwidth and time.

                              --
                              Jeff



                              Comment

                              Working...