Square root function

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Square root function

Jump to solution
1,417 Views
polofermin
Contributor III

Hello again,

-I am targeting with MCF51EM256 CPU

-My version of CodeWarrior is: Codewarrior IDE version 5.9.0 Build 2830

 

Now I have done a square root function, but I have 3 error, maybe I must add any library?, the code is:

 

#define MAX_INT 5;

static long Acc_temp, Acc=0;

 

  /**************************************************************************************************
 * Función:     SquareRoot
 * Argumentos:  ---
 * Resultado:   ---
 * Ambito:      Público
 * ---------------------------------------------------------------------------------------------------
 * Descripción: Función para realizar la raíz cuadrada. Parámetros de entrada de 32 bits,
 * parámetros de salida de 16 bits. (sin signo)
 *****************************************************************************************************/
unsigned short SquareRoot(unsigned long A)
 {
  short j;
  Acc = ASM_FF1(A);//Valor inicial
  for(j=0;j<MAX_INT;j++)//Número de iteraciones máximo = 5
  {
    Acc_temp = (A/Acc + Acc)/2;//Cálcular iteración
    if((abs(Acc - Acc_temp) < 0x0001)||(Acc_temp == 0))//Si el test de precisión es suficiente
    break;//Salir del bucle si se alcanza una buena precisión
    Acc = Acc_temp;//Guardar iteración previa
  }
  //Number_of_interactions[i] = (unsigned short) (j+1); // For debugging purposes
  return((unsigned short)Acc_temp);
 }

  /**************************************************************************************************
 * Función:     ASM_FF1
 * Argumentos:  ---
 * Resultado:   ---
 * Ambito:      Público
 * ---------------------------------------------------------------------------------------------------
 * Descripción: Función para obtener el primer elemento de la iteración (seed). Parámetros de entrada
 * de 32 bits, parámetros de salida de 16 bits. (sin signo)
 *****************************************************************************************************/
unsigned long ASM_FF1(unsigned long A)
 {
  asm(ff1.l d0);//Obtener el primer elemento
  A= (32-A)>>1;//Dividir por dos el primer índice
  A = (unsigned long)1<<A;//Multiplicar por dos
  return(A);
 }

 

 

and errors:

Error   : expression syntax error
ADE7753.c line 581     for(j=0;j<MAX_INT;j++)//Número de iteraciones máximo = 5 

Error   : expression syntax error
ADE7753.c line 587     } 

Error   : ';' expected
ADE7753.c line 589     return((unsigned short)Acc_temp);

 

 

Thanks!

Best regards

Fernin 

Labels (1)
0 Kudos
1 Solution
339 Views
CompilerGuru
NXP Employee
NXP Employee

Just by looking at the code, did not try it out.

The semicolon after the define seems wrong.

 

#define MAX_INT 5;

 

should be

 

#define MAX_INT 5

 

BTW: The name is a bit confusing as there is a ANSI C INT_MAX macro defined in limits.h, so I would use a different name (SQR_MAX_ITERATIONS?).

 

Daniel

Message Edited by CompilerGuru on 2010-01-14 11:30 AM

View solution in original post

0 Kudos
1 Reply
340 Views
CompilerGuru
NXP Employee
NXP Employee

Just by looking at the code, did not try it out.

The semicolon after the define seems wrong.

 

#define MAX_INT 5;

 

should be

 

#define MAX_INT 5

 

BTW: The name is a bit confusing as there is a ANSI C INT_MAX macro defined in limits.h, so I would use a different name (SQR_MAX_ITERATIONS?).

 

Daniel

Message Edited by CompilerGuru on 2010-01-14 11:30 AM
0 Kudos