01 Jul, 2008, Zeno wrote in the 1st comment:
Votes: 0
ch_printf( ch, "+%d%% (+%d)\n\r", ch->pcdata->strform,
get_curr_vig(ch)*((float)ch->pcdata->strform/100) );

get_curr_vig returns an int, strform is an int.

What did I do wrong here? Because the 2nd argument is printing out a random high number.
01 Jul, 2008, Davion wrote in the 2nd comment:
Votes: 0
Looks like it might be a problem with the %d. Try using %f.

ch_printf( ch, "+%d%% (+%f)\n\r", ch->pcdata->strform,
get_curr_vig(ch)*((float)ch->pcdata->strform/100) );
01 Jul, 2008, Zeno wrote in the 3rd comment:
Votes: 0
Yeah, %f would fix it. But %d is suppose to be able to handle floats and automatically cast them. Sprintf does it.
01 Jul, 2008, Caius wrote in the 4th comment:
Votes: 0
Won't that expression in the last argument be implicitly converted to float? Try changing the format string to: "+%d%% (+%.f)\n\r"
01 Jul, 2008, Caius wrote in the 5th comment:
Votes: 0
Wow, my answer was impressively late.
01 Jul, 2008, Davion wrote in the 6th comment:
Votes: 0
Zeno said:
Yeah, %f would fix it. But %d is suppose to be able to handle floats and automatically cast them. Sprintf does it.

Strange… I discovered the solution to the problem by trying it (with some magic numbers) with a printf call and it definitely didn't give me the right number. If you simply don't want the decimal places (the only reason I'd think you'd want to use %d over %f) do as Caius suggested and use %.f
01 Jul, 2008, Zeno wrote in the 7th comment:
Votes: 0
Davion said:
Zeno said:
Yeah, %f would fix it. But %d is suppose to be able to handle floats and automatically cast them. Sprintf does it.

Strange… I discovered the solution to the problem by trying it (with some magic numbers) with a printf call and it definitely didn't give me the right number. If you simply don't want the decimal places (the only reason I'd think you'd want to use %d over %f) do as Caius suggested and use %.f

I'd still like to use %d though (for the sake of the code staying standardized), because the number it'll be [eventually] stored into is an int. Otherwise if I come across this piece of code in the future, I may assume the number is suppose to be a float. Even if I make a comment saying it's an int, it's still somewhat of a conflict and may confuse me. :P
01 Jul, 2008, Caius wrote in the 8th comment:
Votes: 0
How about this:

ch_printf( ch, "+%d%% (+%d)\n\r", ch->pcdata->strform,
(int)(get_curr_vig(ch)*(ch->pcdata->strform/100.0) ) );
01 Jul, 2008, Zeno wrote in the 9th comment:
Votes: 0
Now this is strange.
ch_printf( ch, "+%d%% (+%s)\n\r", ch->pcdata->strform,
num_punct(get_curr_vig(ch)*((float)ch->pcdata->strform/100)) );

That works just fine.
01 Jul, 2008, Caius wrote in the 10th comment:
Votes: 0
As far as I'm aware %d does not convert floats correctly. Converting types via function arguments, however, works. So num_punct works.
01 Jul, 2008, Zeno wrote in the 11th comment:
Votes: 0
But %d does convert floats correctly.
sprintf( buf, "Test: %d\n\r", get_curr_vig(ch)*((float)ch->pcdata->strform/100) );

That works just fine.

This is why I'm confused.
01 Jul, 2008, Caius wrote in the 12th comment:
Votes: 0
#include <stdio.h>

int main()
{
float test = 45.0;
printf( "Number is: %d\n", test );
}


Output:

Number is: 0


So then perhaps sprintf does some special handling.
01 Jul, 2008, Zeno wrote in the 13th comment:
Votes: 0
Well that would satisfy some of my curiosity, but I would like to know where this is documented. *goes to check man page*
01 Jul, 2008, Caius wrote in the 14th comment:
Votes: 0
Do you have an example of sprintf converting correctly? I tried this:

#include <stdio.h>

int main()
{
float test = 45.0;
char buf[24];
sprintf( buf, "Number is: %d\n", test );
printf( buf );
}


And still got 0 as output.
01 Jul, 2008, Zeno wrote in the 15th comment:
Votes: 0
Post #11 was the example that works.
01 Jul, 2008, Caius wrote in the 16th comment:
Votes: 0
Then I guess it must depend on the compiler and/or environment. Because I got 0 also with your example:

#include <stdio.h>

int main()
{
char buf[24];
int get_curr_vig = 200;
int strform = 50;
sprintf( buf, "Test: %d\n\r", get_curr_vig*((float)strform/100) );
printf( buf );
}
01 Jul, 2008, Zeno wrote in the 17th comment:
Votes: 0
Apparently it does.
#include <stdio.h>

int get_curr_vig(int);

int get_curr_vig( int num )
{
return num+1;
}

int main()
{
char buf[24];
int strform = 50;
sprintf( buf, "Test: %d\n\r", get_curr_vig(25)*((float)strform/100) );
printf( buf );
}


My work server returns a 0.
Arthmoor (MUD host) returns -1740944344 (random)
02 Jul, 2008, Davion wrote in the 18th comment:
Votes: 0
Zeno said:
Apparently it does.
#include <stdio.h>

int get_curr_vig(int);

int get_curr_vig( int num )
{
return num+1;
}

int main()
{
char buf[24];
int strform = 50;
sprintf( buf, "Test: %d\n\r", get_curr_vig(25)*((float)strform/100) );
printf( buf );
}


My work server returns a 0.
Arthmoor (MUD host) returns -1740944344 (random)


I'm going to go out on a limb here and say it's the difference between 32 and 64 bit architect. I know Arthmoor is 64 bit, and my output is similar to that. I'll bet that your work server is 32
02 Jul, 2008, Caius wrote in the 19th comment:
Votes: 0
Davion said:
I'm going to go out on a limb here and say it's the difference between 32 and 64 bit architect. I know Arthmoor is 64 bit, and my output is similar to that. I'll bet that your work server is 32

Whatever the reason, I think we at least have established that relying on %d to convert from float is not a portable solution. You learn something new every day.
02 Jul, 2008, Zeno wrote in the 20th comment:
Votes: 0
Going back to the original code that I said worked:
sprintf( buf, "Test: %d\n\r", get_curr_vig(ch)*((float)ch->pcdata->strform/100) );
send_to_char( buf, ch );

Prints:
Quote
Test: 300


So am I missing something here? That is returning a valid number. Is there a typo in the code that I'm not seeing and thus is different from the clean version we tried out?
0.0/23