Earn extra honor and gain new allies!
Honor is earned for each new codewarrior who joins.
Learn more
Code
Diff
  • inline float multiply(const float &number1, const float &number2 )
    {
      if (number1 == 0 || number2 == 0) return 0;
      if (number2 > 0) return number1 + multiply(number1, number2 - 1);
      if (number2 < 0) return -multiply(number1, -number2);
      return -1;
    }
    
    void shift_dec_point(float &number, int &decimal_positions)
    {
      while(number - static_cast<int>(number) > 0.1)
      {
        number = multiply(number, 10);
        decimal_positions++;
      }
    }
    
    float squared(const float &number)
    {
      float temp = number;
      if(temp < 0) temp = -temp;
    
      int decimal_positions = 0;
      shift_dec_point(temp, decimal_positions);
      float factor = temp;
      if(decimal_positions > 0) {
        temp = multiply(temp, temp);
          switch (decimal_positions){
            case 1: factor = 0.01; break;
            case 2: factor = 0.001; break;
            case 3: factor = 0.0001; break;
            case 4: factor = 0.00001;
          }
      }
      return multiply(factor,temp);
    }
  • 1-inline float multiply(float number1, float number2 )
    1+inline float multiply(const float &number1, const float &number2 )
    22 {
    3- float answer = 0;
    4- while( number2 > 0 )
    5- {
    6- answer += number1;
    7- number2--;
    8- }
    9- return answer;
    3+ if (number1 == 0 || number2 == 0) return 0;
    4+ if (number2 > 0) return number1 + multiply(number1, number2 - 1);
    5+ if (number2 < 0) return -multiply(number1, -number2);
    6+ return -1;
    1010 }
    1111
    12-
    13-float squared(float number)
    9+void shift_dec_point(float &number, int &decimal_positions)
    1414 {
    15- if( number < 0 ) number = -number;
    16-
    17- int decimal_positions = 0;
    18- while( number - int(number) > 0.1 )
    11+ while(number - static_cast<int>(number) > 0.1)
    1919 {
    2020 number = multiply(number, 10);
    2121 decimal_positions++;
    2222 }
    23-
    24- float factor = number;
    16+}
    2525
    26- if( decimal_positions > 0 ) {
    27- number = multiply(number,number);
    28- switch (decimal_positions){
    29- case 1: factor = 0.01; break;
    30- case 2: factor = 0.001; break;
    18+float squared(const float &number)
    19+{
    20+ float temp = number;
    21+ if(temp < 0) temp = -temp;
    22+
    23+ int decimal_positions = 0;
    24+ shift_dec_point(temp, decimal_positions);
    25+ float factor = temp;
    26+ if(decimal_positions > 0) {
    27+ temp = multiply(temp, temp);
    28+ switch (decimal_positions){
    29+ case 1: factor = 0.01; break;
    30+ case 2: factor = 0.001; break;