Division and fractions.

Posted by Eleander on Sat 14 Dec 2002 10:15 PM — 6 posts, 18,811 views.

Poland #0
I need to divide a few numbers to obtain a fraction number. I tried to do this on float, double, long double, but it doesn't work, smaug is rounding the number to the integer. Nevertheless when I use sqrt, it doesn't round the result.
It seems weird to me, and i would be very grateful if anyone could tell me what to do.
USA #1
I believe this has to do with how the division functions are generally written for MU*s in general, multiplication as well. The vast majority of all functions are designed to round in order to avoid fractional results in combat and similar situations since its just completely unrealistic to code a completely seperate group of math functions to handle specific spell and combat related calculations. Square roots probably didn't get included in the rounding functions because nobody ever actually expected sqrts to be used on a mud.
Poland #2
Okay but how can I solve the problem ? What should I seek ? I would be grateful for giving me some direction, even if you don't know the answer.
USA #3
Unfortunately, I suspect this would require outright recoding a vast majority of everything in your src that deals with xp, weapon damage, spell damage, coin splitting and anything else dealing with potentially fractional amounts that can't be processed properly as fractions. Having pointed that out, I quite simply have no idea where to even begin looking into such a thing but if I had to guess I'd start looking in the nanny file. I've never actually needed decimals for anything in a mud so I've never been curious enough to go hunting for this. I would also recommend getting the earlist version of pure rom or merc you can find as that would have the least extra code to sift thru to find your math functions to handle decimal rounding.
USA #4
Ok, so even I'm wrong once in a while. In this case, the problem rests with how division is handled by C and C++.

Unless otherwise noted, division is assumed to be integer values only and only integer results are returned. To change that, try using


(num_type) result = (num_type) number_1 / (num_type) number_2


If that fails you can at least retrieve the remainder by using


remainder = number_1 % number_2


(num_type) == int, long, float, etc
Amended on Sun 22 Dec 2002 11:25 AM by Meerclar
Poland #5
I've used cast operator and it's working well. Thank you very much i've realy needed that.
--sorry for possible misspeling--