Topic: why not logb()?
Author: "Bill Hallahan" <Bill_Hallahan_no_spam@37.com>
Date: Mon, 15 Apr 2002 12:14:22 GMT Raw View
I wish the standard supplied this. This is slightly off topic, but will
solve the problem.
You can use the following formula which uses the base "e" logarithm to find
the logarithm to any other base B.
log X = ( ln X ) / ( ln B )
B
where X > 0 and B > 0
OR
Alternatively, you can subsitute a logarithm for any other base for the base
e logarithm, such as the log base 10.
log X = ( log X ) / ( log B )
B 10 10
"Robb" <ArchonTreborus@netscape.net> wrote in message
news:6ee7d287.0203301820.442a4dee@posting.google.com...
> Why is there not a function in the math library that will do logrithms at
any base.
> It could be prototyped like this:
>
> template<typename T> T logb(T num, int base);
>
> It could use the change of bases formula then simply call log10() or
log().
>
> --------------------------------------------------------------------------
-
>
> -Robb
>
> ---
> [ 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 ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
>
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Wed, 3 Apr 2002 20:16:14 GMT Raw View
Christopher Eltschka wrote:
....
> Otherwise you'll have to explain why there is no function to calculate
> the quite common expression a*x+b.
Funny you should mention that - a few languages DO have such a function;
including C99. It's got three functions: fma(), fmaf(), and fmal(), to
handle (and return) double, float, and long double values respectively.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Scott Robert Ladd" <scott@coyotegulch.com>
Date: Tue, 2 Apr 2002 15:28:58 GMT Raw View
"Robb" <ArchonTreborus@netscape.net> wrote...
> Why is there not a function in the math library that will do logrithms at
any base.
Because numerical programming has been sorely under-represented in the C++
Standards process... ;)
Standard C (ISO/IEC 9899:1999) already defines a "logb" function, but it
returns the signed exponent of its argument. There does not appear to be a
generic "logarithm to any base" function.
Nor does the GNU Scientific Library include such a function. It's probably
just as easy to write one of your own.
--
Scott Robert Ladd
http://www.coyotegulch.com
No ads -- just info, algorithms, and very free code.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: Tue, 2 Apr 2002 22:08:26 GMT Raw View
Robb wrote:
>
> Why is there not a function in the math library that will do logrithms at any base.
> It could be prototyped like this:
>
> template<typename T> T logb(T num, int base);
>
> It could use the change of bases formula then simply call log10() or log().
Not as you have written it. The base of log() is not an integer.
double logbase(double num, double base) {
return log(num) / log(base);
}
Optimizing it for log10 and log (base e) cases is left as an excerise to the
student.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Christopher Eltschka <celtschk@web.de>
Date: Tue, 2 Apr 2002 22:08:33 GMT Raw View
ArchonTreborus@netscape.net (Robb) writes:
> Why is there not a function in the math library that will do logrithms at any base.
Maybe because it's easy enough to write log(x)/log(b) yourself?
Otherwise you'll have to explain why there is no function to calculate
the quite common expression a*x+b.
> It could be prototyped like this:
>
> template<typename T> T logb(T num, int base);
It could even be prototyped as
template<class T> T log(T num, T base)
because the ordinary log function takes only one argument.
>
> It could use the change of bases formula then simply call log10() or log().
And the a*x+b function could be prototyped as
template<class T> T muladd(T a, T x, T b);
and simply do a multiplication and an addition (although it also might
take measures against numeric extinction - which would make it
actually more useful than the arbitrary-base logarithm where the
simple formula is numerically stable anyway).
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "P.J. Plauger" <pjp@dinkumware.com>
Date: Tue, 2 Apr 2002 22:09:04 GMT Raw View
"Scott Robert Ladd" <scott@coyotegulch.com> wrote in message news:4Zjq8.56551$K52.9662776@typhoon.tampabay.rr.com...
> "Robb" <ArchonTreborus@netscape.net> wrote...
> > Why is there not a function in the math library that will do logrithms at
> any base.
>
> Because numerical programming has been sorely under-represented in the C++
> Standards process... ;)
>
> Standard C (ISO/IEC 9899:1999) already defines a "logb" function, but it
> returns the signed exponent of its argument. There does not appear to be a
> generic "logarithm to any base" function.
>
> Nor does the GNU Scientific Library include such a function. It's probably
> just as easy to write one of your own.
C99 does offer log, log10, and log2 (in all three precisions). In principle,
they differ only by a final multiplicative constant, so you can whomp up
an arbitrary base logarithm just by changing the constant. In practice,
however, it's not easy to preserve all the bits in a sensitive function
such as log without some very careful crafting for each distinct base.
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: ArchonTreborus@netscape.net (Robb)
Date: Sun, 31 Mar 2002 16:08:15 GMT Raw View
Why is there not a function in the math library that will do logrithms at any base.
It could be prototyped like this:
template<typename T> T logb(T num, int base);
It could use the change of bases formula then simply call log10() or log().
---------------------------------------------------------------------------
-Robb
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]