variadic functions - C++ method with variable arguments (...) reporting incorrect arg values -
i'm having trouble getting variable arguments pass method - method intended select random value within weighted distribution , return index of selected result.
an example usage be:
int pickuptype = randommanager->byweights( 3, 0.60f, 0.20f, 0.20f ); switch( pickuptype ) { // ... pickuptype should 0 2, can branch on }
the function defined follows:
#include <cstdarg> int randommanager::byweights( int weightcount, ... ) { va_list arglist; // total of weights va_start( arglist, weightcount ); float weighttotal = 0; ( int = 0; < weightcount; i++ ) { weighttotal += va_arg( arglist, float ); } va_end( arglist ); // roll number in range // ... (further processing - problem occurs above) }
when run in debugger, call va_arg( arglist, float )
returning garbage values ( 2.0, 1.77499998, -1.08420217e-019 )
, rather values passed in ( 0.60f, 0.20f, 0.20f )
.
any ideas i'm doing wrong? far can tell i'm following spec exactly. i've been using http://www.cplusplus.com/reference/cstdarg/ reference.
in variadic function, float parameters converted doubles. try
weighttotal += va_arg( arglist, double );
Comments
Post a Comment