// given the extent of the altitude and the difference beween the collinear // base intercept and the altitude's base intercept, we can use // Pythagorean's theorem to compute the length of the collinear line. byte Location::collinearHeight(byte i) { // use i altitude height and base; i collinear base return( d->Cb[i] >= d->Ab[i] ? // unsigned, so careful of order squareRoot( squared(d->Ah[i]) + squared(d->Cb[i]-d->Ab[i]) ) : squareRoot( squared(d->Ah[i]) + squared(d->Ab[i]-d->Cb[i]) ) ); }
TEST(SquareRootTest, PositiveNos) { ASSERT_EQ(6, squareRoot(36.0)); ASSERT_EQ(18.0, squareRoot(324.0)); ASSERT_EQ(25.4, squareRoot(645.16)); ASSERT_EQ(0, squareRoot(0.0)); }
int main(void) { printf("squareRoot (2.0) = %f\n", squareRoot(2.0)); printf("squareRoot (144.0) = %f\n", squareRoot(144.0)); printf("squareRoot (17.5) = %f\n", squareRoot(17.5)); return 0; }
int main(void) { printf("square root (18.0,0.002) = %f \n",squareRoot(18,0.002)); printf("square root (18.0,0.000002) = %f \n",squareRoot(18,0.0002)); printf("square root (18.0,0.0000002) = %f \n",squareRoot(18,0.000002)); return 0; }
TEST(climbertest, SetName) { ASSERT_EQ(6, squareRoot(36.0)); ASSERT_EQ(18.0, squareRoot(324.0)); ASSERT_EQ(25.4, squareRoot(645.16)); ASSERT_EQ(0, squareRoot(0.0)); }
TEST(SquareRootTest, NegativeNos) { //ASSERT_EQ执行失败,当前函数返回,该assert后面的语句不执行 ASSERT_EQ(1.0, squareRoot(-15.0)); //ASSERT_EQ(-1.0, squareRoot(-15.0)); ASSERT_EQ(-1.0, squareRoot(-0.2)); }
int main(void) { printf("squareRoot(2.0, .01) = %f\n", squareRoot(2.0, 0.01)); printf("squareRoot(2.0, .001) = %f\n", squareRoot(2.0, 0.001)); printf("squareRoot(2.0, .0001) = %f\n", squareRoot(2.0, 0.0001)); return 0; }
int main() { system("color B"); printf ("squareRoot (2.0) = %.2f\n", squareRoot (2.0)); printf ("squareRoot (144.0) = %.2f\n", squareRoot (144.0)); printf ("squareRoot (17.5) = %.2f\n", squareRoot (17.5)); return 0; }
float quadratic (int a, int b, int c) { float d; float squareRoot (float x); d = ((b * b) - (4 * a * c)); gX1 = (-b + squareRoot(d)) / (2 * a); gX2 = (-b - squareRoot(d)) / (2 * a); }
bool SquareRoot::test() { for (int i = 0; i != 17; ++i) { if (squareRoot(i) != (int)sqrt(i)) { cout << i << " root should be " << (int)sqrt(i) << endl; cout << "Result " << squareRoot(i) << endl; return false; } } return true; }
// given the altitude's distance along the side length and the distance, // use Pythagorean's theorem to calculte the altitude byte Location::altitudeHeight(byte i) { // use right-i and left-i distance if( d->D[left(i)] + d->D[right(i)] < SL ) return( 0 ); unsigned long s = semiPerimeter(i); unsigned long part1 = squareRoot(s*(s - d->D[left(i)])); unsigned long part2 = squareRoot((s-SL)*(s - d->D[right(i)])); // Serial << F("i=") << i << F("\ts=") << s << F("\tpart1=") << part1 << F("\tpart2=") << part2 << endl; return( (2UL * part1 * part2) / SL // this big divide operator is slow. bummer. ); }
int main() { int tc; int caseNum; caseNum = 1; scanf("%d", &tc); while(tc--) { int fs = 0; std::string lower; std::getline(std::cin, lower); std::string upper; std::getline(std::cin, upper); while(std::strcmp(lower, upper) != 0) { bool square = squareRoot(lower); bool fair = palidrone(lower); if(lower < 10) { square = false; fair = false; if(lower == 1 || lower == 4 || lower == 9) { square = true; fair = true; } } else { if(palidrone(lower) && squareRoot(lower)) { int result = (int) sqrt(lower); if(palidrone(result)) fair == true; else fair = false; } } if(fair && square) { //printf("%d\n", lower); fs++; } lower++; } printf("Case #%d: %d\n", caseNum, fs); caseNum++; } }
// given two distances and a known side length, we can calculate the // the altitude's distance along the side length of the altitude. // from https://en.wikipedia.org/wiki/Heron%27s_formula#Algebraic_proof_using_the_Pythagorean_theorem byte Location::altitudeBase(byte i) { if( d->D[left(i)] < d->Ah[i] ) return(0); // use left-i distance and ith height return( squareRoot( squared(d->D[left(i)]) - squared(d->Ah[i]) ) ); }
//Calculates the square roots of the given quadratic equation void roots(int a,int b,int c) { float disc; disc=discriminant(a,b,c); float root1,root2; if(disc<0) printf("it has imaginary square roots \n"); else if(disc==0) { printf("The roots are \n%.2f\n%.2f\n",((float)-b/(2*a)),((float)-b/(2*a))); } else{ root1=(-b+squareRoot(disc))/(2*a); root2=(-b-squareRoot(disc))/(2*a); printf("the square roots are : \n"); printf("%.2f\n%.2f \n",root1,root2); } }
int main (int argc, char *argv []) { int i, mode = NONE; if(argc > 1 && argv[1][0] != '>') for(i=1; i<argc; i++) if(argv[i][0] == '-' && argv[i][1] != '\0') switch(argv[i][1]){ case 'p': mode = PRIME; break; case 'g': mode = GCD; break; case 'r': mode = ROOT; break; default: return badArg(); } switch(mode){ case PRIME: if(argc >= 3 && argv[2][0] != '>' && (atoi(argv[2])) > 0){ int c = atoi(argv[2]); printf("Checking if %d is prime...\n", c); if(isPrime(c)) printf("%d is a prime number.\n" , c); else printf("%d is not a prime number.\n" , c); } else notEnoughInput(); break; case GCD: if(argc >= 4 && argv[2][0] != '>' && (atoi(argv[2])) > 0 && argv[3][0] != '>' && (atoi(argv[3])) > 0){ int a = atoi(argv[2]), b = atoi(argv[3]); printf("Calculating the greatest common divisor of %d and %d...\n", a, b); printf("gcd(%d,%d)=%d\n", a, b, gcd(a,b)); } else notEnoughInput(); break; case ROOT: if(argc >= 3 && argv[2][0] != '>' && (atof(argv[2])) > 0){ double a = atof(argv[2]); printf("Calculating the square root of %f...\n", a); printf("sqrt(%f)=%f\n", a, squareRoot(a)); } else notEnoughInput(); break; default: badArg(); } return 0; }
int main(void) { double num; printf("Enter number: "); scanf("%lf", &num); printf("%lf\n", squareRoot(num)); return 0; }
void addRecordToDataset(Dataset* dataset, Record record) { float priceRelative, oldMean; dataset->sampleSize++; priceRelative = (float)record.x/record.y; oldMean = dataset->mean; dataset->mean = oldMean + (priceRelative - oldMean) / dataset->sampleSize; dataset->Q = dataset->Q + (priceRelative - oldMean) * (priceRelative - dataset->mean); dataset->variance = dataset->Q / dataset->sampleSize; dataset->stdDev = squareRoot(dataset->variance); }
int prime (n) { if ( n == 2 ) { return 1; } else if ( n > 2 ) { bool isPrime = true; int i; for ( i = 2; i <= squareRoot ((float) n); i += (i > 2) ? 2 : 1 ) if ( n % i == 0 ) return 0; return 1; } else { return 0; } }
int main () { time_t end; double seconds; unsigned int i; unsigned int j; time_t start = time(NULL); for (i=0;i<65535;++i) { for (j=0;j<3600;j++) { //R0 is velocityDenominator for now R1 = area; R0 = density; R0 *= R1; //denominator at bp -16-24 = -40, so shift right to -24 R0>>=16; R1 = coefficient; R0*=R1; //denominator at bp -24-16=-40, so shift right to -24 R0>>=16; //move out 2 from calculation as part of binary point shifting //terminalVelocity = weight/velocityDenominator; R3= weight; R4 = R0; R0= R3/R4; //terminal velocity bp now at 1-32+24= -7 R0>>=1; squareRoot(); //R1 is dynamicPressure; //1/2 factored out into shifting bp R2=density; R2*=R0; R2*=R0; dynamicPressure=R2; //terminalVelocity bp at -3 from sqrt, so shift right to 0 R0>>=3; //dynamicPressure bp =-1-16-3-3=-23, so shift right to 0 R0 = dynamicPressure; R0>>=23; } } end = time(NULL); seconds = difftime(end,start); printf("time it took %f seconds\n",seconds); return 0; }
int main () { /* bp -32*/ unsigned int weight = 0x19eb851e; /* bp -16*/ unsigned int coefficient = 0x8000; unsigned int density = 0x9b; //bp = 2^-22/4=2^-24 //so area = (0.14*0.14*PI*2^(bp-2)) unsigned int area = 258265; unsigned int terminalVelocity; unsigned int velocityDenominator; unsigned int dynamicPressure; time_t end; double seconds; unsigned int i; unsigned int j; time_t start = time(NULL); for (i=0;i<65535;++i) { for (j=0;j<3600;j++) { velocityDenominator = density*area; //denominator at bp -16-24 = -40, so shift right to -24 velocityDenominator>>=16; velocityDenominator*=coefficient; //denominator at bp -24-16=-40, so shift right to -24 velocityDenominator>>=16; //move out 2 from calculation as part of binary point shifting terminalVelocity = weight/velocityDenominator; //terminal velocity bp now at 1-32+24= -7 terminalVelocity>>=1; terminalVelocity = squareRoot(terminalVelocity); //1/2 factored out into shifting bp dynamicPressure=density*terminalVelocity*terminalVelocity; //terminalVelocity bp at -3 from sqrt, so shift right to 0 terminalVelocity>>=3; //dynamicPressure bp =-1-16-3-3=-23, so shift right to 0 dynamicPressure>>=23; } } end = time(NULL); seconds = difftime(end,start); printf("time it took %f seconds\n",seconds); return 0; }
void testRoot(){ printf("\nRoot finding and minimisation\n"); golden(functionTest3,0,2); golden(functionTest1,3,7); golden(functionTest2,5,7); golden(poly,-0.5,1.0); brute (poly,-1.0,1.0, 0.0001); brute (functionTest1,3,7, 0.0001); secant(poly,0.0,1.0); newton(poly, dpoly, 0.0); regulaFalsi(poly,-1.0,1.0); bisect(poly,-1.0,1.0); fixedPointIteration(cosine,1.0); squareRoot(20); }
// The Cholesky decomposition // Uses a symmetric, positive-definite matrix // to calculate a lower triangular matrix void choldc(int n, uint32_t *a, float *l_test){ for(int i = 0; i < n; ++i) { for(int j = 0; j < (i + 1); ++j) { float sum = a[i * n + j]; for(int k = 0; k < j; ++k){ sum -= l_test[i * n + k] * l_test[j * n + k]; } if (i == j) { if (sum <= 0.0) { sum *= -1; } l_test[i * n + i] = squareRoot(sum); } else{ l_test[i * n + j] = 1.0 / l_test[j * n + j] * sum; } } } }
int main(int argc,char * argv[]){ int i; long double r = 123456789012345678; long long int MAX_ITER; if(argc != 2){ printf("ErroR..."); } else{ MAX_ITER = atoi(argv[1]); //r = MAX_ITER; time_t t1,t2; t1 = (double) time(NULL); for(i=0;i <MAX_ITER;i++){ squareRoot(r);//Call Function r--; } t2 = (double) time(NULL); printf("\nThe Total time = %f \n",difftime(t2,t1)); } return 0; }
int main(int argc, char** argv) { FILE* fp; if(argc != 2) { printf("USAGE: MatrixRotation <fileContainingTestVectors>\n"); return 1; } fp = fopen(argv[1], "r"); if(fp == NULL) { printf("Failed to open the input file '%s' for reading!\n", argv[1]); return 2; } while(!feof(fp)) { int len = readMatrix(fp); if(len <= 0) { continue; } int dim = squareRoot(len); rotateMatrix(dim); printMatrix(len); } fclose(fp); return 0; }
int main(void) { printf("%f",squareRoot(125348, 0.000000001)); return 0; }
void printSqrt(double n) { log("sqrt(" + n + ") = " + squareRoot(n)); }
TEST(SquareRootTest, NegativeNos) { ASSERT_EQ(-1.0, squareRoot(-15.0)); ASSERT_EQ(-1.0, squareRoot(-0.2)); }
int main() { _Bool C1,C2; unsigned int i , j , k, l , mi , alpha ; double totald1[n1] , totald2[n2] = {} , irDist1[n1] = {}, irDist2[n2] = {}, costd1[n1] = {} , costd2[n2] = {}; double totalCost1[n1] , totalCost2[n2]; int source1, source2, dest1 , dest2; unsigned int edgeCount = 0; double distance , totaldistance1 = 0, totaldistance2 = 0 , finalDistance = 0 , de1 = 0 , de2 = 0, del[n1] = {}, de2l[n2] = {}; struct EdgeBag cab1[n1]; // pass the argument to this as the total number of customers there struct EdgeBag cab2[n2]; // same struct Start s; // create a starting point pair struct Start d; // create a destination pair /* Long Input */ cab1[0].x = 2; cab1[1].x = 1; cab1[2].x = 1; cab1[3].x = 1; cab1[0].y = 2; cab1[1].y = 1; cab1[2].y = 0; cab1[3].y = -1; cab2[0].x = 3; cab2[0].x = 3; cab2[0].x = 3; cab2[0].y = 1; cab2[1].y = 0; cab2[2].y = -1; d.x = 0; d.y = 0; /* Optimal Cost * *----------------------------------------- * * */ // For the cab 1 for ( i = 0; i < n1 ; i++) { if (i != n1 ) { distance = squareRoot((cab1[i].x - cab1[i+1].x) * (cab1[i].x - cab1[i+1].x) + (cab1[i].y - cab1[i].y) *(cab1[i].y - cab1[i + 1].y)); totald1[i] = distance / (i + 1); } else { distance = squareRoot((cab1[i].x - d.x) * (cab1[i].x - d.x) + (cab1[i].y - d.y) * (cab1[i].y - d.y)); totald1[i] = distance / (i + 1); } } for ( i = 0; i < n1 ; i++) { costd1[i] += totald1[i]; for ( j = i + 1; j < n1 ; j++) { costd1[i] += totald1[j]; } costd1[i] *= alpha; } // For the cab 2 for ( i = 0; i < n2 ; i++) { if (i != (n2 - 1)) { distance = squareRoot((cab2[i].x - cab2[i+1].x) * (cab2[i].x - cab2[i+1].x) + (cab2[i].y - cab2[i].y) *(cab2[i].y - cab2[i + 1].y)); totald2[i] = distance / (i + 1); } else { distance = squareRoot((cab2[i].x - d.x) * (cab2[i].x - d.x) + (cab2[i].y - d.y) * (cab2[i].y - d.y)); totald2[i] = distance / (i + 1); } } for ( i = 0; i < n1 ; i++) { costd2[i] += totald2[i]; for ( j = i + 1; j < n1 ; j++) { costd2[i] += totald2[j]; } costd2[i] *= alpha; } /* Optimal Cost Ends */ irDist1[0] = 0; irDist2[0] = 0; for ( i = 0; i < (n1 - 1); i++) { de1 = squareRoot ((cab1[i].x - cab1[i+1].x) * (cab1[i].x - cab1[i+1].x) + (cab1[i].y - cab1[i].y) *(cab1[i].y - cab1[i + 1].y)); de2 = squareRoot((cab1[i + 1].x - d.x) * (cab1[i + 1].x - d.x) + (cab1[i + 1].y - d.y) * (cab1[i + 1].y - d.y)); del[i] = squareRoot((cab1[i].x - d.x) * (cab1[i].x - d.x) + (cab1[i].y - d.y) * (cab1[i].y - d.y)); irDist1[i + 1] = alpha * (de1 + de2 - del[i]); } for ( i = 0; i < (n2 - 1); i++) { de1 = squareRoot((cab2[i].x - cab2[i+1].x) * (cab2[i].x - cab2[i+1].x) + (cab2[i].y - cab2[i].y) *(cab2[i].y - cab2[i + 1].y)); de2 = squareRoot((cab2[i + 1].x - d.x) * (cab2[i + 1].x - d.x) + (cab2[i + 1].y - d.y) * (cab2[i + 1].y - d.y)); de2l[i] = squareRoot((cab2[i].x - d.x) * (cab2[i].x - d.x) + (cab2[i].y - d.y) * (cab2[i].y - d.y)); irDist2[i] = alpha * (de1 + de2 - de2l[i]); } /* IR completes */ for (i = 0; i < n1; i++) { if (i == 0) { totalCost1[i] = costd1[i]; } else { totalCost1[i] = costd1[i] + ( (i) * irDist1[i]); } } for (i = 0; i < n2 ; i++) { if (i == 0) { totalCost2[i] = costd2[i]; } else { totalCost2[i] = costd2[i] + ( (i) * irDist2[i]); } } C1 = 1; // Sir Constrints for ( i=0;i<n1;i++) { C1 = (C1 && (del[i] >= costd1[i])); } for ( i=0;i<n1;i++) { C2 = (C2 && (de2l[i] >= costd2[i])) ; } __CPROVER_assert(!(C1 && C2) , "Cost distribution exists for the current path"); }
void testInputOutputCorrelation() { QTime t; t.start(); struct Nugget { mpz_class a, b, c, agx; }; QList<Nugget> nuggets; mpz_class agx = 0; mpz_class c = 6; mpz_class b = 1; mpz_class a = 1; mpz_class addA = 19; nuggets << Nugget{a, b, c, agx}; int i = 1; while (i <= 30) { a += addA; b += 1; c += 2; agx += 1; addA += 10; if (isSquare(a)) { const auto& n = nuggets.last(); qDebug() << int (t.elapsed() / 1000.0) << "s" << i << a << b << c << " AG(x) = " << agx << " sqrt(a)" << squareRoot(a) << "| diff:" << (mpz_class) (a-n.a) << (mpz_class) (b-n.b) << (mpz_class) (c-n.c) << (mpz_class) (agx-n.agx); nuggets << Nugget{a, b, c, agx}; if (i == 1) { assert(agx == 2); } else if (i == 2) { assert(agx == 5); } else if (i == 3) { assert(agx == 21); } else if (i == 4) { assert(agx == 42); } else if (i == 5) { assert(agx == 152); } else if (i == 20) { assert(agx == 211345365); } i += 1; } } }
double calculate(int numInputTokens, char **inputString) { int i; double result = 0.0; char *s; struct DynArr *stack; //for keeping track of # of operands and operators int numOfOperands = 0; int numOfOperators = 0; int check = 0; //set up the stack stack = createDynArr(20); // start at 1 to skip the name of the calculator calc for(i=1;i < numInputTokens;i++) { s = inputString[i]; if(strcmp(s, "+") == 0) { numOfOperators++; //increase for binary operators add(stack); } else if(strcmp(s,"-") == 0) { numOfOperators++; subtract(stack); } else if(strcmp(s, "/") == 0) { numOfOperators++; divide(stack); } else if(strcmp(s, "x") == 0) { numOfOperators++; multiply(stack); } else if(strcmp(s, "^") == 0) { numOfOperators = numOfOperators; //for unary operators don't increase power(stack); } else if(strcmp(s, "^2") == 0) { numOfOperators = numOfOperators; square(stack); } else if(strcmp(s, "^3") == 0) { numOfOperators = numOfOperators; cube(stack); } else if(strcmp(s, "abs") == 0) { numOfOperators = numOfOperators; absoluteValue(stack); } else if(strcmp(s, "sqrt") == 0) { numOfOperators = numOfOperators; squareRoot(stack); } else if(strcmp(s, "exp") == 0) { numOfOperators = numOfOperators; exponential(stack); } else if(strcmp(s, "ln") == 0) { numOfOperators = numOfOperators; naturalLog(stack); } else if(strcmp(s, "log") == 0) { numOfOperators = numOfOperators; logTen(stack); } else { //check if its a number (or pi or e) if(isNumber(s, &result)) { numOfOperands++; //increase count of operands pushDynArr(stack, result); //if it is push onto stack } else { printf("You entered bad characters, exiting!\n"); //else print error message exit(0); } } } //end for //check for correct balance of operands and operators //must be one more operand than operators in sequence check = numOfOperands - numOfOperators; //this must be equal to 1 for valid input if (check > 1) { printf("Illegal Input, too many operands or too few operators, exiting\n"); exit(0); } else if (check < 1) { printf("Illegal Input, too few operands or too many operators, exiting\n"); exit(0); } //if passed check store final result and return result = topDynArr(stack); return result; }