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

          • Peter Nilsson

            #6
            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

              #7
              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

              • Irrwahn Grausewitz

                #8
                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

                  #9
                  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

                    #10
                    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

                    • Carsten Hansen

                      #11
                      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

                        #12
                        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

                        • Irrwahn Grausewitz

                          #13
                          Re: TAking the minimum of three values

                          "Jeff" <nothing@notexi st.com> wrote:[color=blue]
                          >
                          >"Ron Natalie" <ron@sensor.com > wrote in message
                          >news:3f5e256a$ 0$46555$9a6e19e a@news.newshost ing.com...[color=green]
                          >> 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.[/color]

                          Erm, (almost) the whole thread is cross-posted between c.l.c and c.l.c++


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

                          Comment

                          • Jirka Klaue

                            #14
                            Re: TAking the minimum of three values

                            Carsten Hansen wrote:[color=blue]
                            >"Jirka Klaue" wrote:[color=green]
                            >>Carsten Hansen wrote:[color=darkred]
                            >>>"Sona" wrote:
                            >>>
                            >>>>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
                            >>>
                            >>>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=green][color=darkred]
                            >>>? (x) : (((y) < (z)) ? (z) : (y)))
                            >>>
                            >>>You can't beat that for efficiency.[/color]
                            >>
                            >>It is not even correct.[/color]
                            >
                            > Give an example where it fails.[/color]

                            #include <stdio.h>

                            #define MIN3(x, y, z) \
                            ((x) < (y)) ? (((y) < (z)) ? (y) : (((x) < (z)) ? (z) : (x))) : (((x) < (z)) ? (x) : (((y) < (z)) ? (z) : (y)))

                            int main(void)
                            {
                            float a = 1.6, b = 1.7, c = 1.8;
                            printf("%f\n", MIN3(a, b, c));

                            a = 1.9, b = 1.7, c = 1.8;
                            printf("%f\n", MIN3(a, b, c));

                            a = 1.6, b = 1.7, c = 1.5;
                            printf("%f\n", MIN3(a, b, c));

                            return 0;
                            }

                            1.700000
                            1.800000
                            1.600000

                            Now, give an example where it works.
                            And even when you fix it, it probably is not the most efficient method.

                            Jirka

                            Comment

                            • Steve Zimmerman

                              #15
                              Re: TAking the minimum of three values

                              Carsten Hansen wrote:
                              [color=blue]
                              > "Jirka Klaue" <jklaue@ee.tu-berlin.de> wrote in message
                              > news:bjlui2$b09 $1@mamenchi.zrz .TU-Berlin.DE...
                              >[color=green]
                              >>Carsten Hansen wrote:
                              >>[color=darkred]
                              >>>"Sona" <sona.gardner@n ospam.com> wrote in message
                              >>>news:3f5e1b2 0$1@clarion.car no.net.au...
                              >>>
                              >>>
                              >>>>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
                              >>>>
                              >>>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=green][color=darkred]
                              >>>? (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[/color]

                              Carsten,


                              Thank you for your post.

                              I don't intend this post as mean-spirited; I cannot write such
                              a macro.


                              #include <stdio.h>

                              #define min3(x, y, z) \
                              ((x) < (y)) ? (((y) < (z)) ? (y) : (((x) < (z)) ? (z) : (x))) : \
                              (((x) < (z)) ? (x) : (((y) < (z)) ? (z) : (y)))

                              int main()
                              {
                              float x, y, z;

                              printf("Enter three float values, separated by spaces: ");
                              scanf("%f%f%f", &x, &y, &z);

                              printf("The least of these is %f\n", min3(x, y, z));

                              return 0;
                              }

                              Input from keyboard: 12345 44444 55555
                              Output to screen: The least of these is 44444

                              Comment

                              Working...