Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Round down"
#1
What's the easiest way to get a program to round every value down as it calculates throughout the code? I've got my calculator, but it doesn't round down on the fly, only at the end where I've specified the function. Any easy way to do this? or will I have to create new variables and round them as the code is executed. If this is what has to be done, no wonder MS is so bloated.
Reply
#2
Have all variables in int form(or any data type that doesn't support decimals). Round down on every instance of division.

Code:
int var1 = 10, var2 = 17, var3 = var1 + var2, var4 = var1 * var2, var5 = int(var1/var2);
??
Reply
#3
Don't use typecasting to int as that does not always round down (negative numbers are rounded up - truncated). All programming languages have a "floor" function. I suggest you use that.
Reply
#4
I see what you did there, Hazzy.

Fiel, I've been using the following function to floor numbers:
function floor0(number) {
return Math.floor(number*Math.pow(10,0))/Math.pow(10,0);
}
But what do you do when you've got several places this needs to be enacted? For example, this is my Total LUK formula for MS calculations:

tLUKres = floor0((parseInt(inputbLUK)*maple_warrior) + (parseInt(inputbLUK)*(maple_warrior-1)*luk_percent));}

the way I see it, I'd have to calculate everything individually with new variables like this:

a_luk = floor0(parseInt(inputbLUK)*maple_warrior)
b_luk = floor0(parseInt(inputbLUK)*(maple_warrior-1)*luk_percent)

tLUKres = a_luk + b_luk

would that be the best way?

Thank you in advance.
Reply
#5
Do it like the executable file does it - store both in a class. When you need the float version, call the float version. In your setter, set the float and the rounded version. Then wherever you need the variables, fetch the rounded or the float version as needed.
Reply
#6
errr you're gonna have to help me... I was sick when the "classes and structs" lecture was going on and I'm afraid that's a bit beyond my knowledge ^_^;;
Reply
#7
What programming language are you using for this? I was going to guess Java, but the "function floor0(number)" syntax made me think otherwise. If it's Java, I can help you with classes. If not, I probably won't know the proper syntax. I can, however, go over the idea behind it. What I'm assuming Fiel had in mind was to make a class that will substitute for whatever type of variable you're using for number (I'm assuming an int or a float). The class will itself contain an int and a float as instance variables and will have getter/setter methods for them (e.g. class.getInt() and class.getFloat(), which will return the appropriate stored value). You could (and would probably want to) make other methods as well, such as class.add(), class.subtract(), class.multiply(), and class.divide() that perform the appropriate operation on both versions of the number (the int and the float) and then round the int. This way you'll be able to get at the unrounded float at any time, and the int should automatically round after every operation. The only real drawback is that you can't just use +, -, *, and / to do operations with it. But yeah, if you tell me what language you're using I might be able to be more specific with my help.
Reply
#8
It's javascript.
Reply
#9
it's actually Actionscript 3, the Adobe Flasher's best friend... it's ridiculously similar to Java, I just like the ability of programming in a platform that makes it easy to add in animations, videos, music and other useless entertainment/oriented garbage Smile FMI, read this for a pleasant surprise: http://www.javaworld.com/javaworld/jw-02...ript1.html
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)