Taking minimum of three values

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

    Taking 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 some code with me
    for doing this? Thanks

    Sona

  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Taking minimum of three values

    Sona,
    Have you looked at System.Math.Min for finding the minimum of two values.
    Using the result of the min of two of the values with the third will give
    you the minimum of all three.

    Hope this helps
    Jay

    "Sona" <sona.gardner@n ospam.com> wrote in message
    news:3f5e1be5$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 some code with me
    > for doing this? Thanks
    >
    > Sona
    >[/color]


    Comment

    • Ben Houston

      #3
      Re: Taking minimum of three values

      Assuming that the three variables are a, b and c you could simply do the
      following:
      float minimumValue = Math.Min( Math.Min( a, b ), c );

      Its not horribly inefficient. Probably doing the comparisons yourself would
      be a bit faster -- maybe.

      -ben

      "Sona" <sona.gardner@n ospam.com> wrote in message
      news:3f5e1be5$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 some code with me
      > for doing this? Thanks
      >
      > Sona
      >[/color]


      Comment

      • Hilton

        #4
        Re: Taking 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 some code with me
        > for doing this? Thanks[/color]

        float min = a;

        if (b < min)
        {
        min = b;
        }

        if (c < min)
        {
        min = c;
        }

        ....is probably as efficient as you will get. Any takers on a more efficient
        way? :)

        Hilton


        Comment

        Working...