Jump to content
  • Advertisement
Sign in to follow this  
hkBattousai

[C++, Variable Argument List] Va_Arg() Does Not Retrieve Correct Value

This topic is 2717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have this constructor of the template class PolynomialFraction:template <class T>
PolynomialFraction<T>::PolynomialFraction(uint64_t unNumNomCoef, uint64_t unNumDenCoef, T FirstCoefficient, ... )
{
std::vector<T> NominatorCoefficients;
std::vector<T> DenominatorCoefficients;
va_list ArgumentPointer;
va_start(ArgumentPointer, FirstCoefficient);
T NextCoefficient = FirstCoefficient;
if (unNumNomCoef == 0) // Every polynomial must have at least one coefficient
{
NominatorCoefficients.push_back(static_cast<T>(0));
}
else
{
NominatorCoefficients.push_back(NextCoefficient);
for (uint64_t i=1; i<unNumNomCoef; i++)
{
NextCoefficient = va_arg(ArgumentPointer, T);
NominatorCoefficients.push_back(NextCoefficient);
}
}
if (unNumNomCoef == 0)
{
DenominatorCoefficients.push_back(static_cast<T>(1));
}
else
{
for (uint64_t i=0; i<unNumDenCoef; i++)
{
NextCoefficient = va_arg(ArgumentPointer, T);
DenominatorCoefficients.push_back(NextCoefficient);
}
}
m_NominatorPolynomial = Polynomial<long double>(NominatorCoefficients);
m_DenominatorPolynomial = Polynomial<long double>(DenominatorCoefficients);
}


As its name suggests, this class is supposed to hold nominator and denominator polynomials of a polynomial fraction.

Constructor parameters are:
First: Number of coefficients in nominator
Second: Number of coefficients in denominator
Third: First coefficient
The rest: The other coefficients if any.

I'm calling this constructor like below:PolynomialFraction<long double> pf1(2, 3, 1, 2, 1, 2, 3);
std::cout << pf1.ToString() << std::endl;


Its output is:
(-9.25596e+061x + 1)/(-9.25596e+061x^2 - 9.25596e+061x - 9.25596e+061)[/quote](It is not a bug in ToString() method, I checked it.)

But it should have been:
(2x + 1)/(3x^2 + 2x + 1)[/quote]

In other words, I don't receive any coefficient except for the very first one.

However, the code below which is a very similar one is working flawlessly:template <class T>
Polynomial<T>::Polynomial(uint64_t NumberOfCoefficients, T FirstCoefficient, ...)
{
if (NumberOfCoefficients == 0)
{
m_Coefficients.push_back(static_cast<T>(0));
return;
}
va_list ArgumentPointer;
va_start(ArgumentPointer, FirstCoefficient);
T NextCoefficient = FirstCoefficient;
std::vector<T> Coefficients;
Coefficients.push_back(NextCoefficient);
for (uint64_t i=1; i<NumberOfCoefficients; i++)
{
NextCoefficient = va_arg(ArgumentPointer, T);
Coefficients.push_back(NextCoefficient);
}
m_Coefficients = Coefficients;
}


What am I doing wrong in my code?

Share this post


Link to post
Share on other sites
Advertisement
Your parameters are integers, not doubles. The compiler cannot tell that the arguments passed via the ellipsis should undergo conversion; try passing them explicitly as double literals (that is, 1.0, 2.0, 3.0, et cetera).

Share this post


Link to post
Share on other sites
You are passing integers to PolynomialFraction constructor (2, 3, 1, ...), but you are expecting long double (second T argument to va_arg macro). That won't work. Pass the correct type.

Share this post


Link to post
Share on other sites
and the reason you're not just simply passing in an array, or an [font="'Courier New"]std::vector<double>[/font] directly? Instead of playing around with the type-unsafe and quite undefined behavior prone ellipses operator?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!