[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  SMAUG
. -> [Folder]  SMAUG coding
. . -> [Subject]  Problem Compiling SWRFUSS after putting in the bank intrest code
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Problem Compiling SWRFUSS after putting in the bank intrest code

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Jason   (109 posts)  [Biography] bio
Date Thu 31 May 2012 02:21 AM (UTC)
Message
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.
[Go to top] top

Posted by Jason   (109 posts)  [Biography] bio
Date Reply #1 on Sun 03 Jun 2012 01:03 AM (UTC)
Message
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.
[Go to top] top

Posted by Worstje   Netherlands  (899 posts)  [Biography] bio
Date Reply #2 on Sun 03 Jun 2012 02:47 AM (UTC)
Message
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.
[Go to top] top

Posted by Jason   (109 posts)  [Biography] bio
Date Reply #3 on Sun 03 Jun 2012 04:18 AM (UTC)
Message
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?
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


3,535 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]