Rational& operator /=(const Rational& x) { Rational res = divide(rhs); swap(res); return *this; }
static CYTHON_INLINE struct ZZX* ZZX_div(struct ZZX* x, struct ZZX* y, int* divisible) { struct ZZX* z = new ZZX(); *divisible = divide(*z, *x, *y); return z; }
ll sort(T *a1,T *b1,int n1){ a=a1;b=b1;n=n1; num=0; divide(0,n-1); return num; }
vec3& vec3::operator/=(const vec3& other){ return divide(other); }
int main(void) { struct { ULONG a; UWORD b; ULONG r; } div[] = { {0,1,0x00000000}, {1,1,0x00000001}, {1,2,0x00010000}, {1,3,0x00010000}, {1,4,0x00010000}, {1,1000,0x00010000}, {1,6000,0x00010000}, {10000,666,0x000a000f}, {80000,666,0x01de0015}, {80000,1,0x00003880}, }; const div_numof = sizeof(div) / sizeof(div[0]); int i; UBYTE tmp[256], *p; UBYTE t2[64]; printf("\ndivide:\n"); for (i = 0; i< div_numof; i++) { printf("%d / %d = 0x%08x\n", div[i].a, div[i].b, divide(div[i].a, div[i].b)); if (divide(div[i].a, div[i].b) != div[i].r) { printf("failed!\n"); return 5; } } printf("\nctodstr:\n"); ctodstr("", tmp); if (tmp[0] != 0 || memcmp(tmp+1, "", 1)) { printf("failed \"\"!\n"); return 5; } ctodstr("foobar", tmp); if (tmp[0] != 6 || memcmp(tmp+1, "foobar", 6)) { printf("failed \"foobar\"!\n"); return 5; } printf("\nstcu_d:\n"); stcu_d(tmp, 0); if (strcmp(tmp, "0")) { printf("failed 0\n"); return 5; } stcu_d(tmp, 123456); if (strcmp(tmp, "123456")) { printf("failed 123456\n"); return 5; } stcu_d(tmp, 10000000); if (strcmp(tmp, "10000000")) { printf("failed 10000000\n"); return 5; } printf("\nstpcpy:\n"); p = stpcpy(tmp, ""); if (p != tmp || strcmp(tmp, "")) { printf("failed \"\"\n"); return 5; } p = stpcpy(tmp, "foobar"); if (p != tmp + 6 || strcmp(tmp, "foobar")) { printf("failed \"foobar\"\n"); return 5; } printf("\nintltoupper:\n"); ctodstr("", tmp); intltoupper(tmp); if (tmp[0] != 0) { printf("failed \"\"\n"); return 5; } ctodstr("fooBarφδείπ!@£", tmp); intltoupper(tmp); if (tmp[0] != 14 || memcmp(tmp + 1, "FOOBARΦΔΕίΠ!@£", 14)) { printf("failed \"fooBarφδείπ!@£\"\n"); return 5; } printf("\nintlcmp:\n"); ctodstr("foo", tmp); intltoupper(tmp); if (intlcmp(tmp, tmp) != 1) { printf("failed \"foo\" selfcompare\n"); return 5; } ctodstr("foo", tmp); intltoupper(tmp); ctodstr("foo", t2); if (intlcmp(tmp, t2) != 1) { printf("failed \"FOO\" vs \"foo\"\n"); return 5; } ctodstr("fooBarφδείπ!@£", tmp); intltoupper(tmp); if (intlcmp(tmp, tmp) != 1) { printf("failed \"fooBarφδείπ!@£\" selfcompare\n"); return 5; } ctodstr("fooBar", t2); intltoupper(t2); if (intlcmp(tmp, t2) != 0) { printf("failed \"fooBarφδείπ!@£\" vs \"fooBar\"\n"); return 5; } ctodstr("fooBarφδείπ!@£xxx", t2); intltoupper(t2); if (intlcmp(tmp, t2) != 0) { printf("failed \"fooBarφδείπ!@£\" vs \"fooBarφδείπ!@£xxx\"\n"); return 5; } ctodstr("fooBaxφδείπ!@£", t2); if (intlcmp(tmp, t2) != 0) { printf("failed \"fooBarφδείπ!@£\" vs \"fooBaxφδείπ!@£\"\n"); return 5; } ctodstr("fooBar", t2); if (intlcmp(tmp, t2) != 0) { printf("failed \"fooBarφδείπ!@£\" vs \"fooBar\"\n"); return 5; } ctodstr("fooBarφδείπ!@£xxx", t2); if (intlcmp(tmp, t2) != 0) { printf("failed \"fooBarφδείπ!@£\" vs \"fooBarφδείπ!@£xxx\"\n"); return 5; } ctodstr("fooBaxφδείπ!@£", t2); if (intlcmp(tmp, t2) != 0) { printf("failed \"fooBarφδείπ!@£\" vs \"fooBaxφδείπ!@£\"\n"); return 5; } printf("\nintlcdcmp:\n"); ctodstr("", tmp); intltoupper(tmp); if (intlcdcmp("", tmp) != 1) { printf("failed \"\"\n"); return 5; } ctodstr("A", tmp); if (intlcdcmp("A", tmp) != 1) { printf("failed \"A\" vs \"A\"\n"); return 5; } ctodstr("a", tmp); if (intlcdcmp("A", tmp) != 1) { printf("failed \"A\" vs \"a\"\n"); return 5; } ctodstr("abcDEF", tmp); if (intlcdcmp("ABCDEF", tmp) != 1) { printf("failed \"ABCDEF\" vs \"abcDEF\"\n"); return 5; } ctodstr("fooBarφδείπ!@£", tmp); if (intlcdcmp("FOOBARΦΔΕίΠ!@£", tmp) != 1) { printf("failed \"FOOBARΦΔΕίΠ!@£\" vs \"fooBarφδείπ!@£\"n"); return 5; } ctodstr("fooBarφδείπ!@£", tmp); if (intlcdcmp("FOOBARΦΔΕίΠ!@£", tmp) != 1) { printf("failed \"FOOBARΦΔΕίΠ!@£\" vs \"fooBarφδείπ!@£\"\n"); return 5; } ctodstr("fooBarφδείπ!@£", tmp); if (intlcdcmp("FOOBAXΦΔΕίΠ!@£", tmp) != 0) { printf("failed \"FOOBAXΦΔΕίΠ!@£\" vs \"fooBarφδείπ!@£\"\n"); return 5; } ctodstr("fooBarφδείπ!@£", tmp); if (intlcdcmp("FOOBAR", tmp) != 0) { printf("failed \"FOOBAR\" vs \"fooBarφδείπ!@£\"\n"); return 5; } ctodstr("fooBarφδείπ!@£", tmp); if (intlcdcmp("FOOBARΦΔΕίΠ!@£XXX", tmp) != 0) { printf("failed \"FOOBARΦΔΕίΠ!@£XXX\" vs \"fooBarφδείπ!@£\"\n"); return 5; } return 0; }
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message){ case WM_DESTROY:{ PostQuitMessage(0); break;} case WM_CREATE:{ infoEdit = CreateWindow("edit","0",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_RIGHT|ES_READONLY, 10, 30, 265, 20, hWnd,(HMENU)201,hInst,0); buttons[0] = CreateWindow("button","7", WS_CHILD|WS_VISIBLE, 10,70,65,65, hWnd, (HMENU)107, hInst, 0); buttons[1] = CreateWindow("button","8", WS_CHILD|WS_VISIBLE, 75,70,65,65, hWnd, (HMENU)108, hInst, 0); buttons[2] = CreateWindow("button","9", WS_CHILD|WS_VISIBLE, 140,70,65,65, hWnd, (HMENU)109, hInst, 0); buttons[3] = CreateWindow("button","+", WS_CHILD|WS_VISIBLE, 205,70,65,65, hWnd, (HMENU)110, hInst, 0); buttons[4] = CreateWindow("button","4", WS_CHILD|WS_VISIBLE, 10,135,65,65, hWnd, (HMENU)104, hInst, 0); buttons[5] = CreateWindow("button","5", WS_CHILD|WS_VISIBLE, 75,135,65,65, hWnd, (HMENU)105, hInst, 0); buttons[6] = CreateWindow("button","6", WS_CHILD|WS_VISIBLE, 140,135,65,65, hWnd, (HMENU)106, hInst, 0); buttons[7] = CreateWindow("button","-", WS_CHILD|WS_VISIBLE, 205,135,65,65, hWnd, (HMENU)111, hInst, 0); buttons[8] = CreateWindow("button","1", WS_CHILD|WS_VISIBLE, 10,200,65,65, hWnd, (HMENU)101, hInst, 0); buttons[9] = CreateWindow("button","2", WS_CHILD|WS_VISIBLE, 75,200,65,65, hWnd, (HMENU)102, hInst, 0); buttons[10] = CreateWindow("button","3", WS_CHILD|WS_VISIBLE, 140,200,65,65, hWnd, (HMENU)103, hInst, 0); buttons[11] = CreateWindow("button","*", WS_CHILD|WS_VISIBLE, 205,200,65,65, hWnd, (HMENU)112, hInst, 0); buttons[12] = CreateWindow("button","C", WS_CHILD|WS_VISIBLE, 10,265,65,65, hWnd, (HMENU)113, hInst, 0); buttons[13] = CreateWindow("button","0", WS_CHILD|WS_VISIBLE, 75,265,65,65, hWnd, (HMENU)100, hInst, 0); buttons[14] = CreateWindow("button","=", WS_CHILD|WS_VISIBLE, 140,265,65,65, hWnd, (HMENU)114, hInst, 0); buttons[15] = CreateWindow("button","/", WS_CHILD|WS_VISIBLE, 205,265,65,65, hWnd, (HMENU)115, hInst, 0); break; } case WM_COMMAND:{ if(HIWORD(wParam)==BN_CLICKED){ switch (LOWORD(wParam)){ case 100: case 101: case 102: case 103: case 104: case 105: case 106: case 107: case 108: case 109:{ if(count<9&&second<214748364){ if(znak==0) second=0; second=second*10+LOWORD(wParam)-100; itoa(second, buff, 10); SetWindowText(infoEdit, buff); count++; } break; } case 110: case 111: case 112: case 115:{ switch(znak){ case 0: result=second; break; case 1: { if(plus(result, second)) result=plus(result, second); else { result=0; count=0; znak=1; strcpy(buff, "!Error!"); SetWindowText(infoEdit, buff); } break; } case 2: { if(minus(result, second)) result=minus(result, second); else { result=0; count=0; znak=1; strcpy(buff, "!Error!"); SetWindowText(infoEdit, buff); } break; } case 3: { if(multiply(result, second)) result=multiply(result, second); else { result=0; count=0; znak=1; strcpy(buff, "!Error!"); SetWindowText(infoEdit, buff); } break; } case 4: { if(second){ if(divide(result, second)) result=divide(result, second); else { result=0; count=0; znak=1; strcpy(buff, "!Error!"); SetWindowText(infoEdit, buff); } } else{ result=0; count=0; znak=1; strcpy(buff, "!Dev by 0!"); SetWindowText(infoEdit, buff); } break; } default: break; }; if(strcmp(buff, "!Dev by 0!")&&strcmp(buff, "!Error!")){ sprintf (buff, "%f", result); /*MessageBox(NULL, buff, "Title", MB_OK);*/ while(strrchr(buff, '0')&&(strrchr(buff, '0'))>(strchr(buff, '.'))) buff[strlen(buff)-1]='\0'; if(buff[strlen(buff)-1]=='.') buff[strlen(buff)-1]='\0'; SetWindowText(infoEdit, buff); strcpy(buff, "\0"); count=0; second=0; if(LOWORD(wParam)==110) znak=1; if(LOWORD(wParam)==111) znak=2; if(LOWORD(wParam)==112) znak=3; if(LOWORD(wParam)==115) znak=4; } break; } case 113:{ result=0; second=0; count=0; sprintf (buff, "%f", result); while(strrchr(buff, '0')&&(strrchr(buff, '0'))>(strchr(buff, '.'))) buff[strlen(buff)-1]='\0'; if(buff[strlen(buff)-1]=='.') buff[strlen(buff)-1]='\0'; SetWindowText(infoEdit, buff); znak=1; break; } case 114:{ switch(znak){ case 0: result=second; break; case 1: { if(plus(result, second)) result=plus(result, second); else { result=0; count=0; znak=1; strcpy(buff, "!Error!"); SetWindowText(infoEdit, buff); } break; } case 2: { if(minus(result, second)) result=minus(result, second); else { result=0; count=0; znak=1; strcpy(buff, "!Error!"); SetWindowText(infoEdit, buff); } break; } case 3: { if(multiply(result, second)) result=multiply(result, second); else { result=0; count=0; znak=1; strcpy(buff, "!Error!"); SetWindowText(infoEdit, buff); } break; } case 4: { if(second){ if(divide(result, second)) result=divide(result, second); else { result=0; count=0; znak=1; strcpy(buff, "!Error!"); SetWindowText(infoEdit, buff); } } else{ result=0; count=0; znak=1; strcpy(buff, "!Dev by 0!"); SetWindowText(infoEdit, buff); } break; } default: break; }; if(strcmp(buff, "!Dev by 0!")&&strcmp(buff, "!Error!")){ sprintf (buff, "%f", result); while(strrchr(buff, '0')&&(strrchr(buff, '0'))>(strchr(buff, '.'))) buff[strlen(buff)-1]='\0'; if(buff[strlen(buff)-1]=='.') buff[strlen(buff)-1]='\0'; SetWindowText(infoEdit, buff); count=0; second=result; znak=0; } break; } default: break; } } break; } default: break; }; return DefWindowProc(hWnd, message, wParam, lParam); }
static int euclid(_MIPD_ big x,int num) { /* outputs next c.f. quotient from gcd(w5,w6) */ mr_small sr,m; #ifdef MR_FP mr_small dres; #endif mr_small lr,lq; big t; #ifndef MR_GENERIC_MT miracl *mr_mip=get_mip(); #endif if (num==0) { mr_mip->oldn=(-1); mr_mip->carryon=FALSE; mr_mip->last=FALSE; if (compare(mr_mip->w6,mr_mip->w5)>0) { /* ensure w5>w6 */ t=mr_mip->w5,mr_mip->w5=mr_mip->w6,mr_mip->w6=t; return (mr_mip->q=0); } } else if (num==mr_mip->oldn || mr_mip->q<0) return mr_mip->q; mr_mip->oldn=num; if (mr_mip->carryon) goto middle; start: if (size(mr_mip->w6)==0) return (mr_mip->q=(-1)); mr_mip->ndig=(int)mr_mip->w5->len; mr_mip->carryon=TRUE; mr_mip->a=1; mr_mip->b=0; mr_mip->c=0; mr_mip->d=1; if (mr_mip->ndig==1) { mr_mip->last=TRUE; mr_mip->u=mr_mip->w5->w[0]; mr_mip->v=mr_mip->w6->w[0]; } else { m=mr_mip->w5->w[mr_mip->ndig-1]+1; if (mr_mip->base==0) { #ifndef MR_NOFULLWIDTH if (m==0) { mr_mip->u=mr_mip->w5->w[mr_mip->ndig-1]; mr_mip->v=mr_mip->w6->w[mr_mip->ndig-1]; } else { mr_mip->u=muldvm(mr_mip->w5->w[mr_mip->ndig-1],mr_mip->w5->w[mr_mip->ndig-2],m,&sr); mr_mip->v=muldvm(mr_mip->w6->w[mr_mip->ndig-1],mr_mip->w6->w[mr_mip->ndig-2],m,&sr); } #endif } else { mr_mip->u=muldiv(mr_mip->w5->w[mr_mip->ndig-1],mr_mip->base,mr_mip->w5->w[mr_mip->ndig-2],m,&sr); mr_mip->v=muldiv(mr_mip->w6->w[mr_mip->ndig-1],mr_mip->base,mr_mip->w6->w[mr_mip->ndig-2],m,&sr); } } mr_mip->ku=mr_mip->u; mr_mip->kv=mr_mip->v; middle: forever { /* work only with most significant piece */ if (mr_mip->last) { if (mr_mip->v==0) return (mr_mip->q=(-1)); lq=MR_DIV(mr_mip->u,mr_mip->v); } else { if (((mr_mip->v+mr_mip->c)==0) || ((mr_mip->v+mr_mip->d)==0)) break; lq=MR_DIV((mr_mip->u+mr_mip->a),(mr_mip->v+mr_mip->c)); if (lq!=MR_DIV((mr_mip->u+mr_mip->b),(mr_mip->v+mr_mip->d))) break; } if (lq>=(mr_small)(MR_TOOBIG/mr_abs(mr_mip->d))) break; mr_mip->q=(int)lq; mr_mip->r=mr_mip->a-mr_mip->q*mr_mip->c; mr_mip->a=mr_mip->c; mr_mip->c=mr_mip->r; mr_mip->r=mr_mip->b-mr_mip->q*mr_mip->d; mr_mip->b=mr_mip->d; mr_mip->d=mr_mip->r; lr=mr_mip->u-lq*mr_mip->v; mr_mip->u=mr_mip->v; mr_mip->v=lr; return mr_mip->q; } mr_mip->carryon=FALSE; if (mr_mip->b==0) { /* update w5 and w6 */ mr_mip->check=OFF; divide(_MIPP_ mr_mip->w5,mr_mip->w6,mr_mip->w7); mr_mip->check=ON; if (mr_lent(mr_mip->w7)>mr_mip->nib) return (mr_mip->q=(-2)); t=mr_mip->w5,mr_mip->w5=mr_mip->w6,mr_mip->w6=t; /* swap(w5,w6) */ copy(mr_mip->w7,x); return (mr_mip->q=size(x)); } else { mr_mip->check=OFF; premult(_MIPP_ mr_mip->w5,mr_mip->c,mr_mip->w7); premult(_MIPP_ mr_mip->w5,mr_mip->a,mr_mip->w5); premult(_MIPP_ mr_mip->w6,mr_mip->b,mr_mip->w0); premult(_MIPP_ mr_mip->w6,mr_mip->d,mr_mip->w6); add(_MIPP_ mr_mip->w5,mr_mip->w0,mr_mip->w5); add(_MIPP_ mr_mip->w6,mr_mip->w7,mr_mip->w6); mr_mip->check=ON; } goto start; }
int divide(struct NUMBER *a,struct NUMBER *b,struct NUMBER *c,struct NUMBER *d) { int k,res = 0; struct NUMBER tmp_a,n,m,l,abs_a,abs_b; copyNumber(a,&tmp_a);//aが破壊されないようにtmp_aにコピー if(isZero(b) == 0)//isZeroは0か-1が返るので return(-1); clearByZero(c); clearByZero(d);//c,dを0でクリア clearByZero(&n); clearByZero(&m); clearByZero(&l);//作業用変数n,m,lを0でクリア setInt(&n,1);//適当な変数に1を突っ込む if(numComp(b,&n) == 0)//除数が1ならば { copyNumber(a,c);//c = a; return(0); } getAbs(a,&abs_a); getAbs(b,&abs_b);//a,bの絶対値をとる if((getSign(a) == 1) && (getSign(b) == 1)) { while(1)//xから何回yを引けるか調べる { if(numComp(&tmp_a,b) == -1)//a < bならば break; copyNumber(b,&n);//bを作業用変数nに代入 setInt(&m,1);//作業用変数mに1を代入 while(1) { copyNumber(&n,&l); mulBy10(&l,&n); copyNumber(&m,&l); mulBy10(&l,&m);//mとnを10倍 if(numComp(&tmp_a,&n) == -1)//a < nならば { copyNumber(&n,&l); divBy10(&l,&n); copyNumber(&m,&l); divBy10(&l,&m);//10倍しちゃったので10で割る。いい方法を模索中、一時的に保存する? break; } } while(1) { copyNumber(&tmp_a,&l);//lに現在のaを代入 sub(&l,&n,&tmp_a);//a = l -n; すなわち a -= n; copyNumber(c,&l);//lにcを代入 add(&l,&m,c);//c = l + m;すわなち c += m; if(numComp(&tmp_a,&n) == -1) break; } } copyNumber(&tmp_a,d);//残ったtmp_aがすなわち剰余 } if((getSign(a) == 1) && (getSign(b) == -1)) { res = divide(a,&abs_b,c,d); setSign(c,-1);//+ / -は解が負であるため } if((getSign(a) == -1) && (getSign(b) == 1)) { res = divide(&abs_a,b,c,d); setSign(c,-1);//- / +は解が負であるため setSign(d,-1);//- / +は剰余が負であるため } if((getSign(a) == -1)&& (getSign(b) == -1)) { res = divide(&abs_a,&abs_b,c,d);//-x / -yは解が正であるためなにもしない setSign(d,-1);//- / -は剰余が負であるため } return(res); }
void point::normalize() { divide(length()); }
Quat Quat::operator / ( const Real& scalar) { Quat r; divide(r, *this, scalar); return r; }
void Quat::operator /=( const Real& scalar) { Quat r; divide(r, *this, scalar); set(r); }
void Quat::operator /=( const Quat& other) { Quat r; divide(r, *this, other); set(r); }
Quat Quat::operator / ( const Quat& other) { Quat r; divide(r, *this, other); return r; }
constexpr Rational operator /(const Rational& rhs) const { return divide(rhs); }
Polynom& Polynom::operator %= (const Polynom& _another) { Polynom result = divide(_another).second; a = result.a; return *this; }
int main(int argc, char* argv[]){ DynArr *dyn; dyn = createDynArr(2); double a,b,c,d,e,f,g,h; a = 3; b = 4; c = 10; d = 6; e = 5; f = 20; printf("\n\nTesting addDynArr...\n"); addDynArr(dyn, a); addDynArr(dyn, b); addDynArr(dyn, c); addDynArr(dyn, d); addDynArr(dyn, e); printf("The array's content: [3,4,10,6,5]\n"); assertTrue(EQ(getDynArr(dyn, 0), a), "Test 1st element == 3"); assertTrue(EQ(getDynArr(dyn, 1), b), "Test 2nd element == 4"); assertTrue(EQ(getDynArr(dyn, 2), c), "Test 3rd element == 10"); assertTrue(EQ(getDynArr(dyn, 3), d), "Test 4th element == 5"); assertTrue(EQ(getDynArr(dyn, 4), e), "Test 5th element == 6"); assertTrue(sizeDynArr(dyn) == 5, "Test size = 5"); printf("\n\nTesting add...\nCalling addDynArr(dyn)\n"); add(dyn); printf("The array's content: [3,4,7,6,5]\n"); assertTrue(EQ(getDynArr(dyn, 3), (double)11), "Test 3rd element == 11"); assertTrue(sizeDynArr(dyn) == 4, "Test size = 4"); //removing result of add test and restoring array removeDynArr(dyn, 3); addDynArr(dyn, d); addDynArr(dyn, e); printf("\n\nTesting sub..."); subtract(dyn); printf("The array's content: [3,4,7,6,5]\n"); assertTrue(EQ(getDynArr(dyn, 3), (double)1), "Test 3rd element == 1"); assertTrue(sizeDynArr(dyn) == 4, "Test size = 4"); //printf("%d \n", sizeDynArr(dyn)); printf("Top: %f \n", topDynArr(dyn)); //removing result of add test and restoring array popDynArr(dyn); printf("Top: %f \n", topDynArr(dyn)); pushDynArr(dyn, f); pushDynArr(dyn, e); printf("Top: %f \n", topDynArr(dyn)); printf("\n\nTesting divide..."); divide(dyn); //printf("The array's content: [3,4,10,20,5]\n"); //assertTrue(EQ(getDynArr(dyn, 3),(double)4), "Test 3rd element == 4"); //assertTrue(sizeDynArr(dyn) == 4, "Test size = 4"); /* printf("At 4: %f \n", topDynArr(dyn)); removeDynArr(dyn,4); printf("At 3: %f \n", topDynArr(dyn)); removeDynArr(dyn,3); printf("At 2: %f \n", topDynArr(dyn)); removeDynArr(dyn,2); printf("At 1: %f \n", topDynArr(dyn)); printf("%d \n",sizeDynArr(dyn)); */ //removing result of add test and restoring array printf("\nTop: %f \n", topDynArr(dyn)); popDynArr(dyn); printf("Top: %f \n", topDynArr(dyn)); pushDynArr(dyn, f); pushDynArr(dyn, e); printf("Top: %f \n", topDynArr(dyn)); printf("\n\nTesting multiply...\nCalling addDynArr(dyn)\n"); multiply(dyn); //printf("The array's content: [3,4,10,6,5]\n"); //assertTrue(EQ(getDynArr(dyn, 3),(float)30), "Test 3rd element == 30"); //assertTrue(sizeDynArr(dyn) == (float)4, "Test size = 4"); printf("Before pop Top: %f \n", topDynArr(dyn)); //removing result of add test and restoring array popDynArr(dyn); printf("After pop Top: %f \n", topDynArr(dyn)); pushDynArr(dyn,(double) 2); pushDynArr(dyn,(double) 1); printf("After 2 push Top: %f \n", topDynArr(dyn)); printf("\n\nTesting power of...n"); powerOf(dyn); //printf("The array's content: [3,4,10,6,5]\n"); //assertTrue(EQ(getDynArr(dyn, 3),(float)2), "Test 3rd element == 2"); //assertTrue(sizeDynArr(dyn) == (float)4, "Test size = 4"); }
void cosine_of_angle(void) { int n; double d; if (car(p1) == symbol(ARCCOS)) { push(cadr(p1)); return; } if (isdouble(p1)) { d = cos(p1->u.d); if (fabs(d) < 1e-10) d = 0.0; push_double(d); return; } // cosine function is symmetric, cos(-x) = cos(x) if (isnegative(p1)) { push(p1); negate(); p1 = pop(); } // cos(arctan(x)) = 1 / sqrt(1 + x^2) // see p. 173 of the CRC Handbook of Mathematical Sciences if (car(p1) == symbol(ARCTAN)) { push_integer(1); push(cadr(p1)); push_integer(2); power(); add(); push_rational(-1, 2); power(); return; } // multiply by 180/pi push(p1); push_integer(180); multiply(); push_symbol(PI); divide(); n = pop_integer(); if (n < 0) { push(symbol(COS)); push(p1); list(2); return; } switch (n % 360) { case 90: case 270: push_integer(0); break; case 60: case 300: push_rational(1, 2); break; case 120: case 240: push_rational(-1, 2); break; case 45: case 315: push_rational(1, 2); push_integer(2); push_rational(1, 2); power(); multiply(); break; case 135: case 225: push_rational(-1, 2); push_integer(2); push_rational(1, 2); power(); multiply(); break; case 30: case 330: push_rational(1, 2); push_integer(3); push_rational(1, 2); power(); multiply(); break; case 150: case 210: push_rational(-1, 2); push_integer(3); push_rational(1, 2); power(); multiply(); break; case 0: push_integer(1); break; case 180: push_integer(-1); break; default: push(symbol(COS)); push(p1); list(2); break; } }
// ---------------------------------------------------------------------------- // parses the next command from m_instrStream // ---------------------------------------------------------------------------- void CRPNCalc::parse() { double number = 0; while (m_buffer.length() != 0) { bool delDecimal = false; //erase the spaces at the beginning of the string while(m_buffer[0] == ' ') m_buffer.erase(m_buffer.begin()); number = atof(m_buffer.c_str()); //if it is a number if((number != 0 && m_buffer[0] != '+') || m_buffer[0] == '0') { if(m_buffer[0] == '-') m_buffer.erase(m_buffer.begin()); while((m_buffer[0] >= '0' && m_buffer[0] <= '9') || m_buffer[0] == '.') { if(m_buffer[0] == '.') if(delDecimal == false) delDecimal = true; else break; m_buffer.erase(m_buffer.begin()); if(m_buffer.length() == 0) break; } m_stack.push_front(number); } else { string token; //if the beginning is a character if(m_buffer.length() >= 2) { //special situation with CE if(toupper(m_buffer[0]) == 'C' && toupper(m_buffer[1]) == 'E') { m_buffer.erase(m_buffer.begin(),m_buffer.begin() + 2); clearAll(); continue; } //special situation with -0 else if(m_buffer[0] == '-' && m_buffer[1] == '0') { m_buffer.erase(m_buffer.begin()); while(m_buffer[0] == '0') { m_buffer.erase(m_buffer.begin()); if(m_buffer.length() == 0) break; } m_stack.push_front(number); neg(); continue; } //special situation with S0-9 else if(toupper(m_buffer[0]) == 'S' && m_buffer[1] >= '0' && m_buffer[1] <= '9') { m_buffer.erase(m_buffer.begin()); // delete the 'S' to get the number char index = m_buffer[0]; setReg(static_cast<int>(index) - ZEROINASCII); m_buffer.erase(m_buffer.begin()); // delete the number continue; } //special situation with G0-9 else if(toupper(m_buffer[0]) == 'G' && m_buffer[1] >= '0' && m_buffer[1] <= '9') { m_buffer.erase(m_buffer.begin()); // delete the 'G' to get the number char index = m_buffer[0]; getReg(static_cast<int>(index) - ZEROINASCII); m_buffer.erase(m_buffer.begin()); // delete the number continue; } } if (m_buffer.length() != 0) { token = m_buffer[0]; if (0 == token.compare("+")) add(); else if (0 == token.compare("-")) subtract(); else if (0 == token.compare("*")) multiply(); else if (0 == token.compare("/")) divide(); else if (0 == token.compare("^")) exp(); else if (0 == token.compare("%")) mod(); else if (0 == token.compare("c") || 0 == token.compare("C")) clearEntry(); else if (0 == token.compare("d") || 0 == token.compare("D")) rotateDown(); else if (0 == token.compare("f") || 0 == token.compare("F")) saveToFile(); else if (0 == token.compare("h") || 0 == token.compare("H")) m_helpOn = !m_helpOn; else if (0 == token.compare("l") || 0 == token.compare("L")) loadProgram(); else if (0 == token.compare("m") || 0 == token.compare("M")) neg(); else if (0 == token.compare("p") || 0 == token.compare("P")) recordProgram(); else if (0 == token.compare("r") || 0 == token.compare("R")) { //the application only do this method and ignore other methods //if they are inputed at the same line runProgram(); return; } else if (0 == token.compare("u") || 0 == token.compare("U")) rotateUp(); else if (0 == token.compare("x") || 0 == token.compare("X")) m_on = false; else m_error = true; m_buffer.erase(m_buffer.begin()); } } } }
int main() { /* factoring program using Williams (p+1) method */ int k,phase,m,nt,iv,pos,btch; long i,p,pa,interval; big b,q,n,fp,fvw,fd,fn,t; static big fu[1+MULT/2]; static BOOL cp[1+MULT/2]; mip=mirsys(30,0); b=mirvar(0); q=mirvar(0); n=mirvar(0); t=mirvar(0); fp=mirvar(0); fvw=mirvar(0); fd=mirvar(0); fn=mirvar(0); gprime(LIMIT1); for (m=1;m<=MULT/2;m+=2) if (igcd(MULT,m)==1) { fu[m]=mirvar(0); cp[m]=TRUE; } else cp[m]=FALSE; printf("input number to be factored\n"); cinnum(n,stdin); if (isprime(n)) { printf("this number is prime!\n"); return 0; } for (nt=0,k=3;k<10;k++) { /* try more than once for p+1 condition (may be p-1) */ convert(k,b); /* try b=3,4,5.. */ convert((k*k-4),t); if (egcd(t,n,t)!=1) continue; /* check (b*b-4,n)!=0 */ nt++; phase=1; p=0; btch=50; i=0; printf("phase 1 - trying all primes less than %d\n",LIMIT1); printf("prime= %8ld",p); forever { /* main loop */ if (phase==1) { /* looking for all factors of p+1 < LIMIT1 */ p=mip->PRIMES[i]; if (mip->PRIMES[i+1]==0) { /* now change gear */ phase=2; printf("\nphase 2 - trying last prime less than %ld\n" ,LIMIT2); printf("prime= %8ld",p); copy(b,fu[1]); copy(b,fp); mad(b,b,b,n,n,fd); decr(fd,2,fd); negify(b,t); mad(fd,b,t,n,n,fn); for (m=5;m<=MULT/2;m+=2) { /* store fu[m] = Vm(b) */ negify(fp,t); mad(fn,fd,t,n,n,t); copy(fn,fp); copy(t,fn); if (!cp[m]) continue; copy(t,fu[m]); } convert(MULT,t); lucas(b,t,n,fp,fd); iv=(int)(p/MULT); if (p%MULT>MULT/2) iv++; interval=(long)iv*MULT; p=interval+1; convert(iv,t); lucas(fd,t,n,fp,fvw); negify(fp,fp); subtract(fvw,fu[p%MULT],q); marks(interval); btch*=100; i++; continue; } pa=p; while ((LIMIT1/p) > pa) pa*=p; convert((int)pa,t); lucas(b,t,n,fp,q); copy(q,b); decr(q,2,q); } else { /* phase 2 - looking for last large prime factor of (p+1) */ p+=2; pos=(int)(p%MULT); if (pos>MULT/2) { /* increment giant step */ iv++; interval=(long)iv*MULT; p=interval+1; marks(interval); pos=1; copy(fvw,t); mad(fvw,fd,fp,n,n,fvw); negify(t,fp); } if (!cp[pos]) continue; /* if neither interval+/-pos is prime, don't bother */ if (!plus[pos] && !minus[pos]) continue; subtract(fvw,fu[pos],t); mad(q,t,t,n,n,q); /* batching gcds */ } if (i++%btch==0) { /* try for a solution */ printf("\b\b\b\b\b\b\b\b%8ld",p); fflush(stdout); egcd(q,n,t); if (size(t)==1) { if (p>LIMIT2) break; else continue; } if (compare(t,n)==0) { printf("\ndegenerate case"); break; } printf("\nfactors are\n"); if (isprime(t)) printf("prime factor "); else printf("composite factor "); cotnum(t,stdout); divide(n,t,n); if (isprime(n)) printf("prime factor "); else printf("composite factor "); cotnum(n,stdout); return 0; } } if (nt>=NTRYS) break; printf("\ntrying again\n"); } printf("\nfailed to factor\n"); return 0; }
int main() { char char_response = 0; unsigned int first_operand = 0; unsigned int second_operand = 0; int end_of_operand_flag = 0; int operand_success = 0; int operation_success = 0; char operation = 0; printf("Let's test some simple expressions. Type \'N\' at any time to exit.\nPlease input an expression: "); while ((char_response = getchar()) != EOF) { putchar(char_response); if (char_response == 'N') { printf("\nThanks for playing. Goodbye.\n"); break; } if (isNumChecker(char_response)) { if (operand_success == 0) { first_operand = makeOperand(char_response, first_operand); // printf("\nfirst_operand = %d\n", first_operand); } if (operand_success == 1) { second_operand = makeOperand(char_response, second_operand); // printf("\nsecond_operand = %d\n", second_operand); } end_of_operand_flag = 1; } if (!isNumChecker(char_response) && end_of_operand_flag == 1) { operand_success++; end_of_operand_flag = 0; } if (isOperator(char_response)) { if (operation_success == 0 && operand_success == 1) operation = char_response; // printf ("\noperator = %c\n", operation); operation_success++; } if (char_response == '\n') { // printf("Successful operands = %d\nSuccessful operations = %d\n", operand_success, operation_success); if (operation == 0 || operation_success > 1) printf("Invalid expression.\n"); if (operand_success == 2 && operation_success == 1) { if (operation == '+') add(first_operand, second_operand); if (operation == '-') subtract(first_operand, second_operand); if (operation == '*') multiply(first_operand, second_operand); if (operation == '/') divide(first_operand, second_operand); if (operation == '%') modulo(first_operand, second_operand); } printf("Please input a valid expression: "); /* Reset all variables */ first_operand = 0; second_operand = 0; end_of_operand_flag = 0; operand_success = 0; operation_success = 0; operation = 0; } } return 0; }
Bignum* modulus(Bignum *n1, Bignum *n2, Bignum *r) { Bignum aux1, aux2; return subtract(n1,multiply(divide(n1,n2,&aux1),n2,&aux2),r); }
Complex Complex::operator/(Complex c){ return divide(c); }
CompositeWeight<W1, W2, W3> divide(CompositeWeight<W1, W2, W3> const& x, CompositeWeight<W1, W2, W3> const& y) { W1 w1(divide(x.weight1(), y.weight1())); W2 w2(divide(x.weight2(), y.weight2())); W3 w3(divide(x.weight3(), y.weight3())); return CompositeWeight<W1, W2, W3>(w1, w2, w3); }
int main() { int a[] = {120, 140, 20, 30 ,40 , 50 ,60 ,70 , 800 , 1000, 10, 5}; divide(a, sizeof(a)/ sizeof(int)); }
vec2& vec2::operator/=(const vec2& other){ return divide(other); }
Polynom Polynom::operator / (const Polynom& _another) const { return divide(_another).first; }
bignum modulo (bignum x, bignum y) { return minus(x, multiply(y, divide(x, y))); }
Polynom Polynom::operator % (const Polynom& _another) const { return divide(_another).second; }
//TODO remove pModel CEvaluationNode* CDerive::deriveBranch(const CEvaluationNode* node, const CCopasiObject * pObject, std::vector<const CEvaluationNode*>& env, //std::vector<const CCopasiObject*>& objenv, const CEvaluationTree* pTree, bool simplify) { CEvaluationNode * newNode = NULL; const CEvaluationNodeOperator * pENO = dynamic_cast<const CEvaluationNodeOperator*>(node); if (pENO) { if (!pENO->getLeft() || !pENO->getRight()) return NULL; CEvaluationNode * pLeftDeriv = deriveBranch(pENO->getLeft(), pObject, env, pTree, simplify); if (!pLeftDeriv) return NULL; CEvaluationNode * pRightDeriv = deriveBranch(pENO->getRight(), pObject, env, pTree, simplify); if (!pRightDeriv) {delete pLeftDeriv; return NULL;} // we now know that derivations of the left and right branch exist switch (pENO->subType()) { case CEvaluationNode::S_MULTIPLY: { CEvaluationNode * pLeftCopy = copyBranch_var2obj(pENO->getLeft(), env); CEvaluationNode * pRightCopy = copyBranch_var2obj(pENO->getRight(), env); CEvaluationNode * tmpNode1 = multiply(pRightCopy, pLeftDeriv, simplify); CEvaluationNode * tmpNode2 = multiply(pRightDeriv, pLeftCopy, simplify); return add(tmpNode1, tmpNode2, simplify); } break; case CEvaluationNode::S_DIVIDE: { CEvaluationNode * pLeftCopy = copyBranch_var2obj(pENO->getLeft(), env); CEvaluationNode * pRightCopy = copyBranch_var2obj(pENO->getRight(), env); //numerator CEvaluationNode * tmpNode1 = multiply(pRightCopy, pLeftDeriv, simplify); CEvaluationNode * tmpNode2 = multiply(pRightDeriv, pLeftCopy, simplify); CEvaluationNode * minusNode = subtract(tmpNode1, tmpNode2, simplify); minusNode->compile(NULL); //denominator CEvaluationNode * powerNode = power(copyBranch_var2obj(pENO->getRight(), env), new CEvaluationNodeNumber(CEvaluationNode::S_INTEGER, "2"), simplify); return divide(minusNode, powerNode, simplify); } break; case CEvaluationNode::S_PLUS: return add(pLeftDeriv, pRightDeriv, simplify); break; case CEvaluationNode::S_MINUS: return subtract(pLeftDeriv, pRightDeriv, simplify); break; case CEvaluationNode::S_POWER: { // b-1 CEvaluationNode * tmpNode = subtract(copyBranch_var2obj(pENO->getRight(), env), new CEvaluationNodeNumber(CEvaluationNode::S_INTEGER, "1"), simplify); // a^(b-1) CEvaluationNode * powerNode = power(copyBranch_var2obj(pENO->getLeft(), env), tmpNode, simplify); // b*a' tmpNode = multiply(copyBranch_var2obj(pENO->getRight(), env), pLeftDeriv, simplify); // ln a CEvaluationNodeFunction * funcNode = new CEvaluationNodeFunction(CEvaluationNode::S_LOG, "ln"); funcNode->addChild(copyBranch_var2obj(pENO->getLeft(), env)); // add a // a * b' * ln a CEvaluationNode * tmpNode2 = multiply(copyBranch_var2obj(pENO->getLeft(), env), multiply(pRightDeriv, funcNode, simplify), simplify); // b*a + a*b * ln a CEvaluationNode * plusNode = add(tmpNode, tmpNode2, simplify); // a^(b-1)*(b*a + a*b * ln a) return multiply(powerNode, plusNode, simplify); } break; default: break; } } const CEvaluationNodeVariable * pENV = dynamic_cast<const CEvaluationNodeVariable*>(node); if (pENV) { if (!env[pENV->getIndex()]) return NULL; //basically just expand the tree. return deriveBranch(env[pENV->getIndex()], pObject, env, pTree, simplify); } const CEvaluationNodeNumber * pENN = dynamic_cast<const CEvaluationNodeNumber*>(node); if (pENN) { newNode = new CEvaluationNodeNumber(CEvaluationNode::S_INTEGER, "0"); return newNode; } const CEvaluationNodeObject *pENObj = dynamic_cast<const CEvaluationNodeObject*>(node); if (pENObj) { //first check whether the object is the derivation variable if (pObject->getCN() == pENObj->getObjectCN()) { //std::cout << "*"; return new CEvaluationNodeNumber(CEvaluationNode::S_INTEGER, "1"); } //now we need to check if we know something about the object so that it needs to be expanded const CCopasiObject * tmpObj = (const CCopasiObject *)pENObj->getObjectInterfacePtr(); if (!tmpObj) return NULL; //object is a concentration? if (tmpObj->getObjectName() == "Concentration") { //std::cout << "Concentration found" << std::endl; //In this context, the concentration is expanded as "amount of substance/volume" std::string tmpstr = tmpObj->getObjectParent() ? "<" + tmpObj->getObjectParent()->getCN() + ",Reference=ParticleNumber>" : "<>"; CEvaluationNodeObject* amount = new CEvaluationNodeObject(CEvaluationNode::S_CN, tmpstr); amount->compile(pTree); tmpstr = tmpObj->getObjectAncestor("Compartment") ? "<" + tmpObj->getObjectAncestor("Compartment")->getCN() + ",Reference=Volume>" : "<>"; CEvaluationNodeObject* volume = new CEvaluationNodeObject(CEvaluationNode::S_CN, tmpstr); volume->compile(pTree); CEvaluationNodeObject* volume2 = new CEvaluationNodeObject(CEvaluationNode::S_CN, tmpstr); //we need this node twice volume2->compile(pTree); CEvaluationNode* damount = deriveBranch(amount, pObject, env, pTree, simplify); CEvaluationNode* dvolume = deriveBranch(volume, pObject, env, pTree, simplify); // A / V - A*V /V^2 return subtract(divide(damount, volume, simplify), divide(multiply(amount, dvolume, simplify), power(volume2, new CEvaluationNodeNumber(CEvaluationNode::S_INTEGER, "2"), simplify), simplify), simplify); } //TODO: //object is an object with an assignment //object is dependent species //object is a reaction rate // otherwise return 0. return new CEvaluationNodeNumber(CEvaluationNode::S_INTEGER, "0"); } const CEvaluationNodeCall *pENCall = dynamic_cast<const CEvaluationNodeCall*>(node); if (pENCall) { //is it a function? const CFunction * tmpFunction = dynamic_cast<const CFunction*>(pENCall->getCalledTree()); // const std::vector<CEvaluationNode *> getListOfChildNodes() const {return mCallNodes;} //create call environment for the called function std::vector<const CEvaluationNode*> subenv; size_t i, imax = pENCall->getListOfChildNodes().size(); subenv.resize(imax); for (i = 0; i < imax; ++i) { CEvaluationNode* tmpnode = copyBranch_var2obj(pENCall->getListOfChildNodes()[i], env); compileTree(tmpnode, pTree); subenv[i] = tmpnode; } return deriveBranch(pENCall->getCalledTree()->getRoot(), pObject, subenv, pTree, simplify); } return newNode; }
Number* Operations::subtract(Number* firstNum, Number* secondNum) { //Subtract two Integers if (firstNum->getType() == INTEGER_TYPE && secondNum->getType() == INTEGER_TYPE) { Integer* int1 = (Integer*) firstNum; Integer* int2 = (Integer*) secondNum; return new Integer(int1->value - int2->value); } //Subtract two Fractions else if (firstNum->getType() == FRACTION_TYPE && secondNum->getType() == FRACTION_TYPE) { Fraction* f1 = (Fraction*) firstNum; Fraction* f2 = (Fraction*) secondNum; int num = f1->num * f2->den - f2->num * f1->den; int den = f1->den * f2->den; return (new Fraction(num,den))->simplify(); } //Subtract one Fraction and one Integer else if (firstNum->getType() == INTEGER_TYPE && secondNum->getType() == FRACTION_TYPE){ Integer* int1 = (Integer*) firstNum; Fraction* f2 = (Fraction*) secondNum; int newNum = f2-> num - f2->den * int1->value; return (new Fraction(newNum, f2->den))->simplify(); } else if (firstNum->getType() == FRACTION_TYPE && secondNum->getType() == INTEGER_TYPE){ Fraction* f1 = (Fraction*) firstNum; Integer* int2 = (Integer*) secondNum; int newNum = f1->num - f1->den * int2->value; return (new Fraction(newNum, f1->den))->simplify(); } //Subtract two Logs else if (firstNum->getType() == LOG_TYPE && secondNum->getType() == LOG_TYPE) { Log* log1 = (Log*) firstNum; Log* log2 = (Log*) secondNum; if ((log1->base)->equals(log2->base)) { Number* newPower = divide(log1->power,log2->power); return (new Log(log1->base,newPower))->simplify(); } else { return new Polynomial(log1,'+',log2);// OR Change-of-base? } } //Subtract two Irrationals of same type else if (firstNum->getType() == IRRATIONAL_TYPE && secondNum->getType() == IRRATIONAL_TYPE) { Irrational* ir1 = (Irrational*) firstNum; Irrational* ir2 = (Irrational*) secondNum; if(ir1->name == ir2->name) return new Integer(0); else { return new Polynomial(firstNum, '-', secondNum); } } else if (firstNum->getType() == POLYNOMIAL_TYPE && secondNum->getType() == POLYNOMIAL_TYPE) { Polynomial* p1 = (Polynomial*) firstNum; Polynomial* p2 = (Polynomial*) secondNum; return p1->join(p2,'-'); } else if(firstNum->getType() == POLYNOMIAL_TYPE) { Polynomial* p1 = (Polynomial*) firstNum; return p1->push_back(secondNum,'-'); } else if(secondNum->getType() == POLYNOMIAL_TYPE) { Polynomial* p2 = (Polynomial*) secondNum; return p2->push_back(firstNum,'-'); } //For any other case create Polynomial else { return new Polynomial(firstNum,'-', secondNum); } //TODO For debugging throw runtime_error("Number type not supported"); return new Integer(-1); }