What's new
  • Visit Rebornbuddy
  • Visit Panda Profiles
  • Visit LLamamMagic
  • Visit Resources
  • Visit Downloads
  • Visit Portal

Question

Mahlesseh

New Member
Joined
Mar 18, 2011
Messages
78
Reaction score
0
I want to do this

Code:
        #region Ratio_Calculation

        private List<WoWUnit> ratio_calc()
	{
               float sum;

	       sum = Me.CurrentMana / Me.CurrentTarget.CurrentHealth;
		    
               return sum;
	 }

        #endregion

I read some stuff and this is what i can come up with. I hope someone can explain to me why this isn't working and how i can get this to work. Any help or pointers to guides are much apreciated.

Thanks in advance!

-Edit- It probably looks like crap but i want to get the variable of "Me.CurrentMana / Me.CurrentTarget.CurrentHealth" and use it to tell the bot when to start spamming Arcane Blast. (Arcane Mage)
 
Last edited:
I want to do this

Code:
        #region Ratio_Calculation

        private List<WoWUnit> ratio_calc()
	{
               float sum;

	       sum = Me.CurrentMana / Me.CurrentTarget.CurrentHealth;
		    
               return sum;
	 }

        #endregion

I read some stuff and this is what i can come up with. I hope someone can explain to me why this isn't working and how i can get this to work. Any help or pointers to guides are much apreciated.

Thanks in advance!

-Edit- It probably looks like crap but i want to get the variable of "Me.CurrentMana / Me.CurrentTarget.CurrentHealth" and use it to tell the bot when to start spamming Arcane Blast. (Arcane Mage)


K, lets clean this up incrementally.

First, you're calculating a ratio not a sum, so your temporary variable should be named 'ratio' not 'sum'. Naming things correctly (methods and variables) is easily the hardest thing about programming. Its important to do the best job that you can, because it aids the maintainer further down the road. Even if you are the maintainer, you won't remember what the code does a month from now, and will have to read it to figure it out.

Second, you don't have to do it, but C# naming conventions uses camelcase for method names. Using camelcase keeps everything nice and consistent in other places. If your program grows larger, you'll learn to appreciate the small things like this.

So, our first pass cleanup yields this...
HTML:
private List     RatioCalc()
{
    float ratio;

    ratio = Me.CurrentMana / Me.CurrentTarget.CurrentHealth;
		    
    return ratio;
}


Second pass, we notice that the return type for the method ("List") does not agree with the type we should be returning ("float"). Also, the name of the function implies we will be returning a scalar, and not a container. So, we need to make the return type of the method agree with the purpose.
HTML:
private float   RatioCalc()
{
    float ratio;

    ratio = Me.CurrentMana / Me.CurrentTarget.CurrentHealth;
		    
    return ratio;
}

Third pass, we notice we're just returning the temporary, so we don't need the temporary at all, and our method reduces to a simple 'return' statement. The C# compiler should make this optimization for you internally, but more lines of code to read means more time trying to comprehend what it does. Brevity aids code maintainability, so we simplify it...
HTML:
private float   RatioCalc()
{
     return (Me.CurrentMana / Me.CurrentTarget.CurrentHealth);
}


Lastly, is an issue of performance.

The idea of 'float' was created back in the dinosaur era when CPUs were powered by steam generated from lava pits. Back then, calculating additional bits of precision required exponential time--thus, 'float's were more than twice as fast as 'double's.

In present day, all modern CPUs calculate floating-point quantities to a very high precision, then truncate the result to either a 'float' quantity or a 'double' quantity.

What this means is that 'float' will save you storage space (which is largely not a consideration on modern computers), but they don't save you in performance (which is why most developers wrongly think they are doing good by using them).

So basically, there is no reason to use 'float'--always use a 'double' instead, so your precision will be maintained. Thus, our final result is...
HTML:
private double   RatioCalc()
{
     return (Me.CurrentMana / Me.CurrentTarget.CurrentHealth);
}

A bit more than you asked, but was in a pedagogical mood. :D


cheers,
chinajade
 
Last edited:
Dude... This was way more than i asked for.... Thanks for the small guide :D

So, i wasn't far off after all! I need to read on the basics some more. But thanks for clearing this up :)
 
Back
Top