Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Overloaded pow() is ambiguous... workaround?

1 view
Skip to first unread message

Matthew Schalit

unread,
Sep 25, 2002, 2:39:29 PM9/25/02
to

Folks,
I'm really new to C++, and I'm gonna need a little
help to determine if there's a workaround for this
error I get w/gmake-3.79.1 and gcc-2.95.3. I've searched
the web and usenet and can't find a resolution:


The error is this:

primitive.cxx:5050: call of overloaded `pow (double &, long int &)' is ambiguous

compiling OpenJade-1.3.1 on Unixware7.1.1. In my gcc-fixed math.h
file, I have these available overloaded pow()s:

math.h:99: candidates are: double pow(double, double)
math.h:631: double pow(double, int)
math.h:678: float pow(float, float)
math.h:679: float pow(float, int)
math.h:752: long double pow(long double, long double)
math.h:754: long double pow(long double, int)


I included the gmake output below. Sorry if it's too long, but I
wasn't sure where to snip. I'm wondering if there is a -fno flag
I can throw or if I can (cast) around the problem

pow( double, long )

into something my compiler understands like

pow( double, (float) long)

or if that even makes sense? Thanks for reading this,
and if you ever need help with your linux router, we
do great work over at http://leaf.sourceforge.net/

Matt

============================================================================
g++ -O2 -fno-implicit-templates -I. -I./../include -I./../grove
-I./../spgrove -DPACKAGE=\"openjade\" -DVERSION=\"1.3.1\"
-DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1
-DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1
-DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1
-DHAVE_UNISTD_H=1 -DHAVE_LIMITS_H=1 -DHAVE_STRUCT_STAT_ST_BLKSIZE=1
-DHAVE_ST_BLKSIZE=1 -DSIZEOF_SIZE_T=4 -DSIZEOF_UNSIGNED_INT=4
-DSP_HAVE_LOCALE=1 -DSP_HAVE_WCHAR=1 -DSIZE_T_IS_UINT=1
-DSP_HAVE_BOOL=1 -DSP_ANSI_CLASS_INST=1 -DJADE_MIF=1 -DJADE_HTML=1
-DSP_MULTI_BYTE=1 -DHAVE_DLFCN_H=1
-DDEFAULT_SCHEME_BUILTINS=\"/usr/local/gnome/share/builtins.dsl\"
-c primitive.cxx -fPIC -DPIC

primitive.cxx: In method
`class ELObj * XExptPrimitiveObj::primitiveCall(int, ELObj **,
EvalContext &, Interpreter &, const Location &)':

primitive.cxx:5050: call of overloaded `pow (double &, long int &)' is ambiguous

/gcc-lib/i586-unknown-sysv5/2.95.3pl1/include/math.h:99: candidates are: double pow(double, double)
/gcc-lib/i586-unknown-sysv5/2.95.3pl1/include/math.h:631: double pow(double, int)
/gcc-lib/i586-unknown-sysv5/2.95.3pl1/include/math.h:678: float pow(float, float)
/gcc-lib/i586-unknown-sysv5/2.95.3pl1/include/math.h:679: float pow(float, int)
/gcc-lib/i586-unknown-sysv5/2.95.3pl1/include/math.h:752: long double pow(long double, long double)
/gcc-lib/i586-unknown-sysv5/2.95.3pl1/include/math.h:754: long double pow(long double, int)
gmake[2]: *** [primitive.lo] Error 1
rm InterpreterMessages.cxx
gmake[2]: Leaving directory `/home/matt/Dev/openjade-1.3.1/style'
gmake[1]: *** [style] Error 2
gmake[1]: Leaving directory `/home/matt/Dev/openjade-1.3.1'
gmake: *** [all] Error 2
=============================================================================

Neil Butterworth

unread,
Sep 25, 2002, 2:48:36 PM9/25/02
to

"Matthew Schalit" <msch...@pacbell.net> wrote in message
news:3D9202C3...@pacbell.net...

>
> Folks,
> I'm really new to C++, and I'm gonna need a little
> help to determine if there's a workaround for this
> error I get w/gmake-3.79.1 and gcc-2.95.3. I've searched
> the web and usenet and can't find a resolution:

Post the code that causes the problem.

NeilB


Victor Bazarov

unread,
Sep 25, 2002, 2:48:27 PM9/25/02
to
"Matthew Schalit" <msch...@pacbell.net> wrote...

>
> Folks,
> I'm really new to C++, and I'm gonna need a little
> help to determine if there's a workaround for this
> error I get w/gmake-3.79.1 and gcc-2.95.3. I've searched
> the web and usenet and can't find a resolution:
>
>
> The error is this:
>
> primitive.cxx:5050: call of overloaded `pow (double &, long int &)'
is ambiguous
>
> compiling OpenJade-1.3.1 on Unixware7.1.1. In my gcc-fixed math.h
> file, I have these available overloaded pow()s:
>
> math.h:99: candidates are: double pow(double, double)
> math.h:631: double pow(double, int)
> math.h:678: float pow(float, float)
> math.h:679: float pow(float, int)
> math.h:752: long double pow(long double, long double)
> math.h:754: long double pow(long double, int)
>
>
>
>
> I included the gmake output below. Sorry if it's too long, but I
> wasn't sure where to snip. I'm wondering if there is a -fno flag
> I can throw or if I can (cast) around the problem
>
> pow( double, long )
>
> into something my compiler understands like
>
> pow( double, (float) long)
>
> or if that even makes sense?

Casting it to float doesn't make sense. A 'float' has fewer
digits in its mantissa than a 'long', rounding errors may ensue.
But casting it to double should be sufficient.

double d;
long l;
...
pow(d, double(l));

Victor
--
Please remove capital A's from my address when replying by mail


P.J. Plauger

unread,
Sep 25, 2002, 2:57:55 PM9/25/02
to
"Matthew Schalit" <msch...@pacbell.net> wrote in message news:3D9202C3...@pacbell.net...

> primitive.cxx:5050: call of overloaded `pow (double &, long int &)' is ambiguous


>
> compiling OpenJade-1.3.1 on Unixware7.1.1. In my gcc-fixed math.h
> file, I have these available overloaded pow()s:
>
> math.h:99: candidates are: double pow(double, double)
> math.h:631: double pow(double, int)
> math.h:678: float pow(float, float)
> math.h:679: float pow(float, int)
> math.h:752: long double pow(long double, long double)
> math.h:754: long double pow(long double, int)
>
> I included the gmake output below. Sorry if it's too long, but I
> wasn't sure where to snip. I'm wondering if there is a -fno flag
> I can throw or if I can (cast) around the problem
>
> pow( double, long )
>
> into something my compiler understands like
>
> pow( double, (float) long)

Welcome to the wonderful world of overloaded functions. Yes, you
can, should, and must cast away the problem. In this particular
case, what you probably want is the pow(double, int) overload,
since int and long have the same representation on modern Unix
systems. The pow(double, int) overload tends to be faster and
more accurate than pow(double, double).

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com

Matthew Schalit

unread,
Sep 26, 2002, 2:31:01 PM9/26/02
to

Thanks for the quick reply. I didn't realize. I'll try to
monitor the group today and keep up the thread. Thanks again:

Here is primative.cxx, with the offending function listed
first, the error occuring at line 5050 noted below and a
couple of addition functions for context.

Matthew

================ primative.cxx =============================================
[.snip a hundred previous DEFPRIMATIVES about Multiply, Divide, etc.]

DEFPRIMITIVE(XExpt, argc, argv, context, interp, loc)
{
long n1, n2;
double d1, d2;
int dim1, dim2;

ELObj::QuantityType q1 = argv[0]->quantityValue(n1, d1, dim1);
ELObj::QuantityType q2 = argv[0]->quantityValue(n2, d2, dim2);
if (q1 == ELObj::noQuantity)
return argError(interp, loc,
InterpreterMessages::notAQuantity, 0, argv[0]);
else if (dim1 != 0) {
if (!argv[1]->exactIntegerValue(n2))
return argError(interp, loc,
InterpreterMessages::notAnExactInteger, 1, argv[1]);
return new (interp) QuantityObj(pow(d1,n2), dim1*n2); // <<<==== Line 5050.
}
else {
if ((q2 == ELObj::noQuantity) || (dim2 != 0))
return argError(interp, loc,
InterpreterMessages::notANumber, 1, argv[1]);
double res = pow(d1, d2);
long tem;
if (argv[0]->exactIntegerValue(tem) &&
argv[1]->exactIntegerValue(tem) &&
fabs(res) < LONG_MAX)
return interp.makeInteger((long)res);
return new (interp) RealObj(res);
}
}


DEFPRIMITIVE(Expt, argc, argv, context, interp, loc)
{
double d, d2;
if (!argv[0]->realValue(d))
return argError(interp, loc,
InterpreterMessages::notANumber, 0, argv[0]);
if (!argv[1]->realValue(d2))
return argError(interp, loc,
InterpreterMessages::notANumber, 1, argv[1]);
double res = pow(d, d2);
long tem;
if (argv[0]->exactIntegerValue(tem) &&
argv[1]->exactIntegerValue(tem) &&
fabs(res) < LONG_MAX)
return interp.makeInteger((long)res);
return new (interp) RealObj(res);
}

DEFPRIMITIVE(ExactToInexact, argc, argv, context, interp, loc)
{
long n;
double d;
int dim;
switch (argv[0]->quantityValue(n, d, dim)) {
case ELObj::noQuantity:
return argError(interp, loc,
InterpreterMessages::notAQuantity, 0, argv[0]);
case ELObj::doubleQuantity:
return argv[0];
case ELObj::longQuantity:
argv[0]->realValue(d);
return new (interp) RealObj(d);
default:
CANNOT_HAPPEN();
}
}
=====================================================================

Matthew Schalit

unread,
Sep 26, 2002, 2:39:34 PM9/26/02
to
Victor Bazarov wrote:

> Casting it to float doesn't make sense. A 'float' has fewer
> digits in its mantissa than a 'long', rounding errors may ensue.
> But casting it to double should be sufficient.
>
> double d;
> long l;
> ...
> pow(d, double(l));
>
> Victor

Thanks for the reply. I just posted the primitive.cxx code,
and maybe that helps to clarify. There's a lot going on in
that file, more than just a pow(2.0,2E56) sort of thing,
though what, I'm still in the dark about :)

Matt

Karl Heinz Buchegger

unread,
Sep 26, 2002, 2:55:47 PM9/26/02
to

Matthew Schalit wrote:
>
> Victor Bazarov wrote:
>
> > Casting it to float doesn't make sense. A 'float' has fewer
> > digits in its mantissa than a 'long', rounding errors may ensue.
> > But casting it to double should be sufficient.
> >
> > double d;
> > long l;
> > ...
> > pow(d, double(l));
> >
> > Victor
>
> Thanks for the reply. I just posted the primitive.cxx code,
> and maybe that helps to clarify.

Not really. It all depends on the number range you expect for n2.

As you have seen, the compiler doesn't have a pow function, which
takes a long as it's second argument.
But it has a version which takes an int, and it has a version which
has a double. Deciding which one you want to take depends on what you
expect in n2. If you are pretty sure that n2 will never be bigger
then what fits into an int, then cast n2 to int. If you can't assume
this, then cast n2 to a double.

--
Karl Heinz Buchegger
kbuc...@gascad.at

Matthew Schalit

unread,
Sep 26, 2002, 4:03:32 PM9/26/02
to


You've been very kind,
I can't be sure what n2 will be, so I cast it to a double as in:

return new (interp) QuantityObj(pow(d1, (double)n2), dim1*n2);

And I guess the result still fits in dim1*n2, so good is good.
Have a great day,
Matt

0 new messages