Topic: Min/Max Functions


Author: Ralf Stoffels <stoffels@faho.rwth-aachen.de>
Date: 1996/12/07
Raw View
Hi Fred,

in http://monet.uwaterloo.ca/blitz/examples/promote.h
you find a possible solution for your problem.


Ralf


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: abell@atl.mindspring.com (Andrew C. Bell)
Date: 1996/12/08
Raw View
"Fred Wasmer" <73743.1660@compuserve.com> wrote:
>What is the latest thinking on the best way to
>defined a Min or Max function that works correctly
>with the 8 standard numeric data types [...]
>I mean you can use the function with
>any two numeric parameters, and that the return type
>is the smallest type that will hold any possible result.

But there are no such functions (unless you use a union along with a
type identifier.)  Floating point numbers trade precision for dynamic
range.  So if you compare a double and a long, if you return a double
type and the long is larger, precision may be lost.  Conversely, a
long is typically not sufficient for the whole range for which a
double is capable.  Explicit casting to a common type ) is the
solution I generally use, so I can choose which sacrifice I want to
make.

Andrew Bell
abell@mindspring.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: damian@molly.cs.monash.edu.au (Damian Conway)
Date: 1996/12/09
Raw View
abell@atl.mindspring.com (Andrew C. Bell) writes:

>"Fred Wasmer" <73743.1660@compuserve.com> wrote:
>>What is the latest thinking on the best way to
>>defined a Min or Max function that works correctly
>>with the 8 standard numeric data types [...]
>>I mean you can use the function with
>>any two numeric parameters, and that the return type
>>is the smallest type that will hold any possible result.

>But there are no such functions (unless you use a union along with a
>type identifier.)  Floating point numbers trade precision for dynamic
>range.  So if you compare a double and a long, if you return a double
>type and the long is larger, precision may be lost.  Conversely, a
>long is typically not sufficient for the whole range for which a
>double is capable.  Explicit casting to a common type ) is the
>solution I generally use, so I can choose which sacrifice I want to
>make.

Further to this: take control yourself and tell the compiler what to
return in each instance:

 template <class T1, class T2, class TR>
 TR min(T1 a, T2 b, TR)
 {
  if (a<b) return static_cast<TR>(a);
  else     return static_cast<TR>(b);
 }

 main()
 {
  cout << min(1.1, 2, int()) << endl;
  cout << min(1.1, 2, float()) << endl;
 }

damian
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  who: Damian Conway                 email: damian@cs.monash.edu.au
where: Computer Science Dept.          web: http://www.cs.monash.edu.au/~damian
       Monash University             phone: +61-3-9905-5184
       Clayton 3168                    fax: +61-3-9905-5146
       AUSTRALIA                     quote: "A pessimist is never disappointed."
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Fred Wasmer" <73743.1660@compuserve.com>
Date: 1996/12/05
Raw View

What is the latest thinking on the best way to
defined a Min or Max function that works correctly
with the 8 standard numeric data types: ie,
signed/unsigned short/regular/long integers, float,
and double.

By work correctly, I mean you can use the function with
any two numeric parameters, and that the return type
is the smallest type that will hold any possible result.

I know that this has been discussed many times before,
but was wondering if there were any changes to the
automatic conversion or ambiguity resolution rules that
made this easier.


Here is what I have tried:


Use a macro

    No good: Want function-call semantics


template<class T> T Min (T x1, T x2);

    With Borland C++ 5.01, doesn't work with mixed
    types: Min ((int) 1, (float) 1).  Does this
    work with new standard?


template<class T1, class T2> ???? Min (T1 x1, T2 x2);

    What do you declare as the return type? T1 or T2?


Define a function for every possible parameter type pair.

    Works, but a pain: need 8*8 = 64 function declarations to
    cover all of the combinations.   Need even more if you also
    want to support char types.


double Min (double x1, double x2)

    Compiles, but very inefficient if, say, comparing two integers.


Take care,
Fred Wasmer



[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]