Problem Compiling SWRFUSS after putting in the bank intrest code

Posted by Jason on Thu 31 May 2012 02:21 AM — 4 posts, 19,463 views.

#0
ok I put the bank intrest snippet for swr into my swrfuss code, and i get the following errors.

update.c: In function `void bank_update()':
update.c:2814: warning: assignment to `int' from `double'
update.c:2814: warning: argument to `int' from `double'




void bank_update()
{
    CHAR_DATA *ch;
    int value1, value2;  
    char buf[MAX_INPUT_LENGTH];  

/*    day     = time_info.day + 1;
    if ( day % 7 !=  6 )         */         /* Uncomment to have once a MUD week updates *//*
    return;*/

    for ( ch = last_char; ch; ch = gch_prev )
    {
	if ( ch == first_char && ch->prev )
	{
	    bug( "char_update: first_char->prev != NULL... fixed", 0 );
	    ch->prev = NULL;
	}
	gch_prev = ch->prev;
	set_cur_char( ch );
	if ( gch_prev && gch_prev->next != ch )
	{
	    bug( "char_update: ch->prev->next != ch", 0 );
	    return;
	}


    	if ( !IS_NPC( ch ) )
	{
           value1 = ch->pcdata->bank;
           value2 = (value1 * .001);<---- this is the error line
           ch->pcdata->bank += value2;
           sprintf(buf, "&C[Monthly Bank Update] %s, you made: %d credits this month.&W\n\r", ch->name, value2);
           send_to_char(buf, ch);
    	}
    }
}



I'm confused why this works with swr but not swrfuss

any help would be apprieciated.
#1
I've gone over and gone over this code and I have not found any reason why it isn't working. I've rewitten that part of the code quite a few times, and yet it comes up the same every time. I hope someone can figure this out, cause it has me completely stumped.
Netherlands #2
First: it is a warning, not an error. But if you treat warnings like errors like a good coder should ;) then I commend you good sir.

Let's look at this line:

value2 = (value1 * .001);


value1 is declared as an int.
value2 is declared as an int.
.001 is a floating point number; a double to be precise.

An int (integer) is a whole number, like 1, 2, -5 or 1000. Fractional numbers like 1.1, 3.14 100.0000001 are not integers.

Computers and floating point math have gotchas. For example, take the number represented by (1/3). In our base-10 system, the closest we can get is 0.333333 with an eternal amount of 3s behind it. The computer has the same sort of problem, but it thinks in base-2; meaning the problem is the same but the practical cases are different. Depending on the sort of floating point and its accuracy, it is very well possible that a float cannot properly represent an integer despite it being in its 'range'!

In practice, this difference is miniscule and won't affect you, but it is a very nasty hole, which is the reason the warning exists. And now you know why you get an error here.

There will be values passed for which the result of the calculation cannot be converted to an integer without losing the fractional part.

In your case, there is probably no harm in rounding down like a proper bank would no doubt do. As such, your solution is:

value2 = (int)(value1 * .001);


This tells the compiler that you knowingly throw away the fractional part and that you really want an int out of the double that was given to you.
#3
That worked great thank you... The SWRFUSS code is so different from regular swr it is a bit confusing. Is there a site i can go to help learn this codebase better?