Skip to content
Browse files

test cases added

  • Loading branch information...
1 parent ee26b0c commit e759bfb463a0a07dac574210eacb89a6302e0eb2 @asanza committed Apr 1, 2012
Showing with 89 additions and 0 deletions.
  1. +2 −0 maketest.sh
  2. +87 −0 testsqrt.c
View
2 maketest.sh
@@ -0,0 +1,2 @@
+#!/usr/bin/env bash
+gcc testsqrt.c -o testsqrt -lm -g
View
87 testsqrt.c
@@ -0,0 +1,87 @@
+#include <stdio.h>
+#include <math.h>
+typedef short fractional;
+typedef int _Q31Fractional;
+
+fractional _Q15sqrt(fractional data);
+double Fract2Float(fractional data);
+fractional Float2Fract(double data);
+double _Q31Fract2Float(_Q31Fractional data);
+fractional sqrt3(int data);
+int main()
+{
+ int i;
+ double maxerr = 0;
+ for(i = 0; i < 2147483647; i++)
+ {
+ //i = 32768;
+ double d = sqrt(_Q31Fract2Float(i));
+ fractional fr = sqrt3(i);
+ double t = Fract2Float(fr);
+ double error;
+ if ( i == 0)
+ error = 0;
+ else
+ error = fabs(t-d)*100/d;
+ //printf("%d, %.10f, %.10f, %f\n", i, d, t, error);
+ //if(error != 100)
+ {
+ if(error > 30)
+ {
+ maxerr = error;
+ printf("%d, %.10f, %.10f, %f\n", i, d, t, error);
+ }
+ //printf("%d, %.10f, %.10f, %f\n", i, d, t, error);
+ //printf("ERROR TOO BIG, STOPING!\n");
+ //break;
+ }
+ }
+ printf("Max. Error: %f\n", maxerr);
+ return 1;
+}
+
+fractional sqrt3(int data)
+{
+ unsigned short* t = (unsigned short*) &data;
+ //int p = *(t+1);
+ if(*(t+1)>0)
+ return _Q15sqrt(*(t+1));
+ //p = *(t);
+ short a = *(t) >> 2;
+ /*if(a & 0x4000)
+ {
+ a >>= 1;
+ a = 0x3FFF;
+ a = _Q15sqrt(a);
+ return a >> 7;
+ }*/
+ a = a&0x3FFF;
+ a = _Q15sqrt(a);//>>7;
+ a = a >> 7;
+ return a;
+}
+
+fractional _Q15sqrt(fractional data)
+{
+ unsigned long int t = (unsigned long int) data;
+ t = t << 17;
+ unsigned long int d = sqrt(t);
+ d = d >> 1;
+ return (short)d;
+}
+
+double Fract2Float(fractional data)
+{
+ return data*3.051757812e-5;
+}
+
+fractional Float2Fract(double data)
+{
+ return (short) data/3.051757812e-5;
+}
+
+double _Q31Fract2Float(_Q31Fractional data)
+{
+ return (int) data*4.656612873e-10;
+}
+

0 comments on commit e759bfb

Please sign in to comment.
Something went wrong with that request. Please try again.