Update of /cvsroot/easycalc/PPCport/core/mlib In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv14503 Modified Files: calcDB.cpp display.cpp display.h fp.cpp funcs.cpp konvert.cpp stack.cpp Added Files: defuns.h guess.cpp Log Message: Subset of the code, compiling, but not yet linking Index: display.h =================================================================== RCS file: /cvsroot/easycalc/PPCport/core/mlib/display.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** display.h 24 Jun 2009 21:39:59 -0000 1.2 --- display.h 24 Sep 2009 21:16:50 -0000 1.3 *************** *** 54,59 **** TCHAR * display_complex(Complex number) MLIB; TCHAR * display_integer(UInt32 number,Tbase mode) MLIB; ! TCHAR * display_default(Skin *skin, Trpn rpn,Boolean complete) MLIB; ! TCHAR * display_list(Skin *skin, List *list) MLIB; extern TdispPrefs dispPrefs; --- 54,59 ---- TCHAR * display_complex(Complex number) MLIB; TCHAR * display_integer(UInt32 number,Tbase mode) MLIB; ! TCHAR * display_default(Trpn rpn,Boolean complete) MLIB; ! TCHAR * display_list(List *list) MLIB; extern TdispPrefs dispPrefs; Index: display.cpp =================================================================== RCS file: /cvsroot/easycalc/PPCport/core/mlib/display.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** display.cpp 5 Jul 2009 20:53:37 -0000 1.3 --- display.cpp 24 Sep 2009 21:16:50 -0000 1.4 *************** *** 27,36 **** #include "compat/PalmOS.h" #include "konvert.h" #include "Skin.h" #include "mathem.h" #include "display.h" #include "defuns.h" #include "funcs.h" ! #include "prefs.h" #include "fp.h" #include "stack.h" --- 27,38 ---- #include "compat/PalmOS.h" #include "konvert.h" + #include "core/core_display.h" #include "Skin.h" + #include "Easycalc.h" #include "mathem.h" #include "display.h" #include "defuns.h" #include "funcs.h" ! #include "core/prefs.h" #include "fp.h" #include "stack.h" *************** *** 123,127 **** /* for decimal display signed number */ if ((signednum = number) < 0) { ! number = -number; *result = _T('-'); result1 = result + 1; --- 125,129 ---- /* for decimal display signed number */ if ((signednum = number) < 0) { ! number = -signednum; *result = _T('-'); result1 = result + 1; *************** *** 152,156 **** TCHAR * ! display_list(Skin *skin, List *list) { TCHAR *result,*tmp; --- 154,158 ---- TCHAR * ! display_list(List *list) { TCHAR *result,*tmp; *************** *** 159,163 **** result = (TCHAR *) MemPtrNew((UInt32)(dispPrefs.decPoints*2+10)*list->size + 10); if (!result) { ! skin->alertErrorMessage(c_memory); return NULL; } --- 161,165 ---- result = (TCHAR *) MemPtrNew((UInt32)(dispPrefs.decPoints*2+10)*list->size + 10); if (!result) { ! alertErrorMessage(c_memory); return NULL; } *************** *** 240,244 **** ***********************************************************************/ TCHAR * ! display_default(Skin *skin, Trpn rpn, Boolean complete) { TCHAR *result; --- 242,246 ---- ***********************************************************************/ TCHAR * ! display_default(Trpn rpn, Boolean complete) { TCHAR *result; *************** *** 272,276 **** case list: if (complete) ! result = display_list(skin, rpn.u.listval); else { result = (TCHAR *) MemPtrNew(20); --- 274,278 ---- case list: if (complete) ! result = display_list(rpn.u.listval); else { result = (TCHAR *) MemPtrNew(20); Index: funcs.cpp =================================================================== RCS file: /cvsroot/easycalc/PPCport/core/mlib/funcs.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** funcs.cpp 5 Jul 2009 20:53:37 -0000 1.3 --- funcs.cpp 24 Sep 2009 21:16:50 -0000 1.4 *************** *** 42,46 **** #include "complex.h" #include "history.h" ! #include "prefs.h" #include "fp.h" #include "slist.h" --- 42,46 ---- #include "complex.h" #include "history.h" ! #include "core/prefs.h" #include "fp.h" #include "slist.h" *************** *** 487,491 **** else for(i=0;i<lst2->size;i++){ lst->item[row-1]=lst2->item[i]; ! row+=sgn(dif); } list_delete(lst2); --- 487,491 ---- else for(i=0;i<lst2->size;i++){ lst->item[row-1]=lst2->item[i]; ! row+=(Int16)sgn(dif); } list_delete(lst2); *************** *** 869,919 **** convert_real_to_string(Functype *func,CodeStack *stack) { ! double arg; ! TCHAR *res,*tmp; ! CError err; ! err=stack_get_val(stack,&arg,real); ! if (err) ! return err; ! if (!finite(arg)) ! return c_badarg; ! res = (TCHAR *) MemPtrNew(MAX_FP_NUMBER+20); ! tmp = res; ! if (func->num==FUNC_RADIAN) { ! fp_print_double(res,arg/M_PIl); ! StrCat(res,_T("pi")); } ! else if (func->num==FUNC_DEGREE || func->num == FUNC_DEGREE2) { ! if (arg<0) { ! *tmp++=_T('-'); ! arg=-arg; ! } ! /* The numbers must be added 1E-12 to avoid ! cases like 1+5/60 -> 1o04' ! */ ! fp_print_double(tmp,trunc(arg+1E-12)); tmp+=StrLen(tmp); ! *tmp++=_T('°'); ! if (arg-trunc(arg)<1E-15) ! StrCopy(tmp,_T("0\'")); ! else { ! if (func->num == FUNC_DEGREE) { ! fp_print_double(tmp,trunc(60*(arg-trunc(arg+1E-12))+1E-12)); ! tmp+=StrLen(tmp); ! *tmp++=_T('\''); ! fp_print_double(tmp,60*(arg*60-trunc(arg*60+1E-12))); ! } else { /* FUNC_DEGREE2 */ ! fp_print_double(tmp,60*(arg-trunc(arg+1E-12))+1E-12); ! StrCat(tmp,_T("'")); ! } ! } } ! err = stack_add_val(stack,res,string); ! MemPtrFree(res); ! return err; } --- 869,919 ---- convert_real_to_string(Functype *func,CodeStack *stack) { ! double arg; ! TCHAR *res,*tmp; ! CError err; ! err=stack_get_val(stack,&arg,real); ! if (err) ! return err; ! if (!finite(arg)) ! return c_badarg; ! res = (TCHAR *) MemPtrNew(MAX_FP_NUMBER+20); ! tmp = res; ! if (func->num==FUNC_RADIAN) { ! fp_print_double(res,arg/M_PIl); ! StrCat(res,_T("pi")); ! } ! else if (func->num==FUNC_DEGREE || func->num == FUNC_DEGREE2) { ! if (arg<0) { ! *tmp++=_T('-'); ! arg=-arg; } ! /* The numbers must be added 1E-12 to avoid ! cases like 1+5/60 -> 1o04' ! */ ! fp_print_double(tmp,trunc(arg+1E-12)); ! tmp+=StrLen(tmp); ! *tmp++=_T('°'); ! if (arg-trunc(arg)<1E-15) ! StrCopy(tmp,_T("0\'")); ! else { ! if (func->num == FUNC_DEGREE) { ! fp_print_double(tmp,trunc(60*(arg-trunc(arg+1E-12))+1E-12)); tmp+=StrLen(tmp); ! *tmp++=_T('\''); ! fp_print_double(tmp,60*(arg*60-trunc(arg*60+1E-12))); ! } else { /* FUNC_DEGREE2 */ ! fp_print_double(tmp,60*(arg-trunc(arg+1E-12))+1E-12); ! StrCat(tmp,_T("'")); ! } } + } ! err = stack_add_val(stack,res,string); ! MemPtrFree(res); ! return err; } *************** *** 933,960 **** * ***********************************************************************/ ! Boolean ! varfunc_name_ok(TCHAR *name,rpntype type) ! { ! Int16 i; ! if (StrLen(name)==0 || StrLen(name)>MAX_FUNCNAME) ! return false; ! if (!IS_FUNC_1LETTER(name[0])) ! return false; ! for (i=0;name[i];i++) ! if (!IS_FUNC_LETTER(name[i])) ! return false; if (StrCompare(name,parameter_name)==0) ! return false; if (type==variable && is_constant(name)) ! return false; else if (type==function) { ! for (i=0;defined_funcs[i].name[0];i++) ! if (StrCompare(name,defined_funcs[i].name)==0) ! return false; } ! return true; } --- 933,958 ---- * ***********************************************************************/ ! Boolean varfunc_name_ok(TCHAR *name,rpntype type) { ! Int16 i; ! if (StrLen(name)==0 || StrLen(name)>MAX_FUNCNAME) ! return false; ! if (!IS_FUNC_1LETTER(name[0])) ! return false; ! for (i=0;name[i];i++) ! if (!IS_FUNC_LETTER(name[i])) ! return false; if (StrCompare(name,parameter_name)==0) ! return false; if (type==variable && is_constant(name)) ! return false; else if (type==function) { ! for (i=0;defined_funcs[i].name[0];i++) ! if (StrCompare(name,defined_funcs[i].name)==0) ! return false; } ! return true; } *************** *** 989,1029 **** CodeStack *addparams) { ! Trpn argument; ! CodeStack *stack; ! CError err; ! Boolean oldinput; ! oldinput = DisableInput; ! DisableInput = true; ! stack = stack_new(origstack->size); ! stack_copy(stack,origstack); ! argument.type = real; ! argument.allocsize = 0; ! argument.u.realval = param; ! if (addparams == NULL) { ! set_func_argument(&argument,1); ! } else { ! stack_push(addparams,argument); ! set_func_argument(addparams->stack,addparams->size); ! } ! err = stack_compute(stack); ! decr_func_argument(); ! if (addparams != NULL) { ! /* Return the addparams stack to the original state */ ! stack_pop(addparams); ! } ! if (!err) ! err = stack_get_val(stack,result,real); ! if (err) ! *result = NaN; ! stack_delete(stack); ! DisableInput = oldinput; ! return err; } --- 987,1027 ---- CodeStack *addparams) { ! Trpn argument; ! CodeStack *stack; ! CError err; ! Boolean oldinput; ! oldinput = DisableInput; ! DisableInput = true; ! stack = stack_new(origstack->size); ! stack_copy(stack,origstack); ! argument.type = real; ! argument.allocsize = 0; ! argument.u.realval = param; ! if (addparams == NULL) { ! set_func_argument(&argument,1); ! } else { ! stack_push(addparams,argument); ! set_func_argument(addparams->stack,addparams->size); ! } ! err = stack_compute(stack); ! decr_func_argument(); ! if (addparams != NULL) { ! /* Return the addparams stack to the original state */ ! stack_pop(addparams); ! } ! if (!err) ! err = stack_get_val(stack,result,real); ! if (err) ! *result = NaN; ! stack_delete(stack); ! DisableInput = oldinput; ! return err; } *************** *** 1048,1087 **** Complex *result) { ! CodeStack *stack; ! CError err = c_noerror; ! CodeStack *paramstack; ! UInt16 i; ! stack = stack_new(origstack->size); ! if (!stack) ! return c_memory; ! stack_copy(stack,origstack); ! paramstack = stack_new(paramcount); ! if (!paramstack) { ! stack_delete(stack); ! return c_memory; ! } ! for(i=0; i < paramcount && err == c_noerror; i++) { ! err = stack_add_val(paramstack, ¶ms[i], complex); ! } ! if (!err) { ! set_func_argument(paramstack->stack,paramstack->size); ! err = stack_compute(stack); ! decr_func_argument(); ! } ! if (!err) ! err = stack_get_val(stack,result,complex); ! if (err) { ! result->real = NaN; ! result->imag = NaN; ! } ! stack_delete(stack); ! stack_delete(paramstack); ! return err; } --- 1046,1085 ---- Complex *result) { ! CodeStack *stack; ! CError err = c_noerror; ! CodeStack *paramstack; ! UInt16 i; ! stack = stack_new(origstack->size); ! if (!stack) ! return c_memory; ! stack_copy(stack,origstack); ! paramstack = stack_new(paramcount); ! if (!paramstack) { ! stack_delete(stack); ! return c_memory; ! } ! for(i=0; i < paramcount && err == c_noerror; i++) { ! err = stack_add_val(paramstack, ¶ms[i], complex); ! } ! if (!err) { ! set_func_argument(paramstack->stack,paramstack->size); ! err = stack_compute(stack); ! decr_func_argument(); ! } ! if (!err) ! err = stack_get_val(stack,result,complex); ! if (err) { ! result->real = NaN; ! result->imag = NaN; ! } ! stack_delete(stack); ! stack_delete(paramstack); ! return err; } *************** *** 1090,1142 **** func_item(Functype *func, CodeStack *stack) { ! rpntype type1,type2; ! CError err; ! err = stack_item_type(stack,&type2,(func->paramcount-1)); ! if (err) ! return err; ! if (func->paramcount==2 && type2==list) ! err = stack_item_type_nr(stack,&type1,1); ! else if (func->paramcount==3) ! err = stack_item_type_nr(stack,&type1,2); ! else ! err = c_badargcount; ! if (err) ! return err; ! if (type1 == variable) { ! UInt32 arg1=0,arg2=0; ! Trpn item,ritem; ! err = stack_get_val(stack,&arg2,integer); ! if (err) ! return err; ! if (func->paramcount == 3) { ! err = stack_get_val(stack,&arg1,integer); ! if (err) ! return err; ! }else ! arg1=arg2; ! item = stack_pop(stack); ! ritem.allocsize = 0; ! ritem.type = litem; ! StrCopy(ritem.u.litemval.name,item.u.varname); ! ritem.u.litemval.row = arg1; ! ritem.u.litemval.col = arg2; ! stack_push(stack,ritem); ! rpn_delete(item); } else switch (type2){ ! case list: ! return list_item(func,stack); ! case matrix: ! return matrix_item(func,stack); ! case cmatrix: ! return cmatrix_item(func,stack); ! default: ! return c_badarg; } ! return c_noerror; } --- 1088,1140 ---- func_item(Functype *func, CodeStack *stack) { ! rpntype type1,type2; ! CError err; ! err = stack_item_type(stack,&type2,(func->paramcount-1)); ! if (err) ! return err; ! if (func->paramcount==2 && type2==list) ! err = stack_item_type_nr(stack,&type1,1); ! else if (func->paramcount==3) ! err = stack_item_type_nr(stack,&type1,2); ! else ! err = c_badargcount; ! if (err) ! return err; ! if (type1 == variable) { ! UInt32 arg1=0,arg2=0; ! Trpn item,ritem; ! err = stack_get_val(stack,&arg2,integer); ! if (err) ! return err; ! if (func->paramcount == 3) { ! err = stack_get_val(stack,&arg1,integer); ! if (err) ! return err; ! }else ! arg1=arg2; ! item = stack_pop(stack); ! ritem.allocsize = 0; ! ritem.type = litem; ! StrCopy(ritem.u.litemval.name,item.u.varname); ! ritem.u.litemval.row = arg1; ! ritem.u.litemval.col = arg2; ! stack_push(stack,ritem); ! rpn_delete(item); } else switch (type2){ ! case list: ! return list_item(func,stack); ! case matrix: ! return matrix_item(func,stack); ! case cmatrix: ! return cmatrix_item(func,stack); ! default: ! return c_badarg; } ! return c_noerror; } --- NEW FILE: guess.cpp --- /* * $Id: guess.cpp,v 1.1 2009/09/24 21:16:50 mapibid Exp $ * * Scientific Calculator for Palms. * Copyright (C) 1999,2000 Ondrej Palkovsky * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * * You can contact me at 'on...@pe...'. */ #include "stdafx.h" #include "compat/PalmOS.h" //#include <StringMgr.h> #include "segment.h" #include "compat/MathLib.h" #include "stack.h" #include "funcs.h" #include "konvert.h" #include "complex.h" #include "display.h" #include "defuns.h" #include "mathem.h" #define MAX_DIV_F 1024 #define MAX_FORM_F 128 #define MAX_DIV_CONST 4 #define MAX_DIV_CONST_E 12 #define GUESS_MAX 131072 #define GUESS_FACTORS_MAX 1000000000 #define M_PI 3.14159265358979323846 #define M_E 2.7182818284590452354 static Boolean is_int(double t1) MLIB; static Boolean is_int(double t1) { if (IS_ZERO(t1-round(t1))) return true; return false; } static Boolean guess_pot(double t1,Int32 *cte_lo,Int32 *cte_up) MLIB; static Boolean guess_pot(double t1,Int32 *cte_lo,Int32 *cte_up) { Int16 i,j; double tmp; if (t1<=0) t1=-t1; for (i=2;i<MAX_FORM_F;i++) { tmp=t1/i; for (j=2;i<=tmp;j++) { tmp/=i; if (tmp==1) { if (t1<=0) *cte_lo=-i; else *cte_lo=i; *cte_up=j; return true; } } } return false; } static Boolean guess_div(double t1,Int32 *upper,Int32 *lower,Int16 maxlev) MLIB; static Boolean guess_div(double t1,Int32 *upper,Int32 *lower,Int16 maxlev) { Int16 i; double tmp; for (i=1;i<maxlev;i++) { tmp=i*t1; if (is_int(tmp)) { *upper=(Int32)round(tmp); *lower=i; return true; } } return false; } static Boolean guess_e_x(double t1,Int32 *upper,Int32 *lower,Int32 *cte_up,Int32 *cte_lo) MLIB; static Boolean guess_e_x(double t1,Int32 *upper,Int32 *lower,Int32 *cte_up,Int32 *cte_lo) { Int16 i; double tmp; if (t1<=0) t1=-t1; /* 1*e^(x/y) */ tmp=log(t1); if (guess_div(tmp,upper,lower,MAX_FORM_F)){ *cte_up=1; *cte_lo=1; return true; } /* x*e^(-y) */ tmp=t1; for (i=1;i<MAX_DIV_CONST_E;i++) { tmp*=M_E; if (guess_div(tmp,cte_up,cte_lo,MAX_FORM_F)) { *upper=-i; *lower=1; return true; } } /* x*e^(y) */ tmp=t1; for (i=1;i<MAX_DIV_CONST_E;i++) { tmp/=M_E; if (guess_div(tmp,cte_up,cte_lo,MAX_FORM_F)) { *upper=i; *lower=1; if(*cte_up==0) return false; return true; } } return false; } static Boolean guess_mult_pi(double t1,Int32 *upper,Int32 *lower,Int16 *power) MLIB; static Boolean guess_mult_pi(double t1,Int32 *upper,Int32 *lower,Int16 *power) { Int16 i; double tmp; /* First try dividing it */ tmp=t1; for (i=1;i<MAX_DIV_CONST;i++) { tmp/=M_PI; if (guess_div(tmp,upper,lower,MAX_FORM_F)) { *power=i; if(*upper==0) return false; return true; } } /* multiplying it */ tmp=t1; for (i=1;i<MAX_DIV_CONST;i++) { tmp*=M_PI; if (guess_div(tmp,upper,lower,MAX_FORM_F)) { *power=-i; return true; } } return false; } static Boolean guess_log_x(double t1,Int32 *upper,Int32 *lower) MLIB; static Boolean guess_log_x(double t1,Int32 *upper,Int32 *lower) { if(t1>26) return false; t1=exp(t1); if (guess_div(t1,upper,lower,MAX_FORM_F) && *upper!=0) return true; return false; } static Boolean guess_sqrt(double t1,Int32 *cte_up,Int32 *cte_lo,Int32 *power) MLIB; static Boolean guess_sqrt(double t1,Int32 *cte_up,Int32 *cte_lo,Int32 *power) { Int16 i; double tmp; if (t1<=0.0) t1=-t1; for (i=2;i<4;i++) { tmp=t1/sqrt(i); if (guess_div(tmp,cte_up,cte_lo,MAX_FORM_F)) { *power=i; return true; } } return false; } static Boolean guess_pow1(double t1,Int32 *upper,Int32 *lower,Int16 *power) MLIB; static Boolean guess_pow1(double t1,Int32 *upper,Int32 *lower,Int16 *power) { Int16 i; double tmp; if (t1<=0.0) t1=-t1; tmp=t1; for (i=2;i<MAX_DIV_CONST_E;i++) { tmp*=t1; if (guess_div(tmp,upper,lower,MAX_FORM_F)) { *power=i; if (*upper>GUESS_MAX) return false; return true; } } return false; } static Boolean guess_real(double t1,TCHAR *result) MLIB; static Boolean guess_real(double t1,TCHAR *result) { Int32 upper,lower,cte_up,cte_lo; Int16 power; if (!finite(t1)){ if (isnan(t1)){ StrPrintF(result,_T("NaN")); return true; } if(t1<0.0) StrCopy(result++,_T("-")); else StrCopy(result++,_T("+")); StrPrintF(result,_T("Inf")); return true; } if (IS_ZERO(t1)) { StrCopy(result,_T("0")); return true; } if (is_int(t1)) if (guess_pot(round(t1),&cte_lo,&cte_up)){ if(t1<0.0) StrCopy(result++,_T("-")); StrPrintF(result,_T("%ld^%ld"),cte_lo,cte_up); return true; }; if (fabs(t1)>GUESS_FACTORS_MAX) return false; if (is_int(t1)) { // print factors Int32 value = (Int32)round(t1); Int32 factors[32]; UInt8 fcount, i,k=0; TCHAR* tmp = result; fcount = factorize(value, factors); for(i=0; i<fcount; i++) { if (i < fcount - 1 && factors[i] == factors[i+1]) k++; else { StrPrintF(tmp,_T("%ld*"),factors[i]); while(*tmp) tmp++; if (k) StrPrintF(--tmp,_T("^%d*"),k+1); while(*tmp) tmp++; k=0; } } *--tmp = 0; return true; } if (fabs(t1)>GUESS_MAX) return false; if (guess_div(t1,&upper,&lower,MAX_DIV_F)) { StrPrintF(result,_T("%ld/%ld"),upper,lower); return true; } if (is_int(1/t1)) if (guess_pot(round(1/t1),&cte_lo,&cte_up)){ if(t1<0.0) StrCopy(result++,_T("-")); StrPrintF(result,_T("%ld^-%ld"),cte_lo,cte_up); return true; }; if (guess_sqrt(t1,&cte_up,&cte_lo,&upper)) { if(t1<0.0) StrCopy(result++,_T("-")); if (cte_up!=1){ StrPrintF(result,_T("%ld"),cte_up); result=result+StrLen(result); } if (cte_lo==1) StrPrintF(result,_T("sqrt(%ld)"),upper); else StrPrintF(result,_T("sqrt(%ld)/%ld"),upper,cte_lo); return true; } if (guess_mult_pi(t1,&upper,&lower,&power)) { if (upper<0){ StrCopy(result++,_T("-")); upper=-upper;} if (power>0) { if (upper>1){ StrPrintF(result,_T("%ld"),upper); result+=StrLen(result);} if (power==1) StrPrintF(result,_T("pi")); else StrPrintF(result,_T("pi^%d"),power); result+=StrLen(result); if (lower>1) StrPrintF(result,_T("/%ld"),lower); }else{ StrPrintF(result,_T("%ld/"),upper); result+=StrLen(result); if (lower>1){ StrPrintF(result,_T("%ld"),lower); result+=StrLen(result);} if (power<-1) StrPrintF(result,_T("pi^%d"),-power); else StrCat(result,_T("pi")); } return true; } if (guess_e_x(t1,&upper,&lower,&cte_up,&cte_lo)) { if(t1<0.0) StrCopy(result++,_T("-")); if(cte_lo!=1){ StrPrintF(result,_T("%ld/%ld*"),cte_up,cte_lo); result=result+StrLen(result); } else if(cte_up!=1){ StrPrintF(result,_T("%ld"),cte_up); result=result+StrLen(result); } if (lower==1 && upper>0) StrPrintF(result,_T("e^%ld"),upper); else if (lower==1) StrPrintF(result,_T("e^%ld"),upper); else StrPrintF(result,_T("e^(%ld/%ld)"),upper,lower); return true; } if (guess_log_x(t1,&upper,&lower)) { if (lower==1) StrPrintF(result,_T("ln(%ld)"),upper); else StrPrintF(result,_T("ln(%ld/%ld)"),upper,lower); return true; } if (guess_pow1(t1,&upper,&lower,&power)) { if (upper && lower) { if(t1<0.0) StrCopy(result++,_T("-")); if (power==2 && lower==1) StrPrintF(result,_T("sqrt(%ld)"),upper); else if (power==2) StrPrintF(result,_T("sqrt(%ld/%ld)"),upper,lower); else if (lower==1) StrPrintF(result,_T("%ld^(1/%d)"),upper,power); else StrPrintF(result,_T("(%ld/%ld)^(1/%d)"),upper,lower,power); return true; } } return false; } static Boolean guess_complex(Complex arg,TCHAR *result) MLIB; static Boolean guess_complex(Complex arg,TCHAR *result) { Complex_gon arg_gon; Int32 upper,lower,rad; Int16 i; double tmp; if (!finite(arg.real) || !finite(arg.imag)) return false; if (fabs(arg.real)>100000 || fabs(arg.imag)>100000) return false; arg_gon = cplx_to_gon(arg); if (!guess_real(arg_gon.r,result)) StrCopy(result,display_real(arg_gon.r)); if (is_int(arg_gon.r) && round(arg_gon.r)==1) StrCopy(result,_T("e^(")); else StrCat(result,_T("*e^(")); result=result+StrLen(result); tmp = arg_gon.angle/M_PI; if (is_int(tmp)){ StrPrintF(result,_T("%ldpi*i)"),(Int32)round(tmp)); return true; } if (guess_div(tmp,&upper,&lower,MAX_FORM_F)){ if (upper<0){ StrPrintF(result++,_T("-")); upper=-upper;} if (upper==1) StrPrintF(result,_T("pi/%ld*i)"),lower); else StrPrintF(result,_T("%ldpi/%ld*i)"),upper,lower); return true; } tmp=arg_gon.angle-MAX_DIV_CONST*2*M_PI; for(i=0;i<2*MAX_DIV_CONST;i++){ tmp+=2*M_PI; if (is_int(tmp)){ if (tmp<0){ StrPrintF(result++,_T("-")); tmp=-tmp;} if ((Int32)round(tmp)==1) StrPrintF(result,_T("i)")); else StrPrintF(result,_T("%ldi)"),(Int32)round(tmp)); return true; } if (guess_div(tmp,&upper,&lower,MAX_FORM_F)){ if (upper<0){ StrPrintF(result++,_T("-")); upper=-upper;} if (upper==1) StrPrintF(result,_T("i/%ld)"),lower); else StrPrintF(result,_T("%ldi/%ld)"),upper,lower); return true; } if (guess_sqrt(tmp,&upper,&lower,&rad)) { if(tmp<0.0) StrPrintF(result++,_T("-")); if(upper!=1){ StrPrintF(result,_T("%ld"),upper); result=result+StrLen(result); } if(lower==1) StrPrintF(result,_T("sqrt(%ld)*i)"),rad); else StrPrintF(result,_T("sqrt(%ld)/%ld*i)"),rad,lower); return true; } } return false; } TCHAR * guess(Trpn item) MLIB; TCHAR * guess(Trpn item) { TCHAR *result; Boolean guessed; if (item.type == real) { result = (TCHAR *) MemPtrNew(MAX_FP_NUMBER+1); guessed = guess_real(item.u.realval,result); if (!guessed) { MemPtrFree(result); return NULL; } return result; } else if (item.type == complex) { result = (TCHAR *) MemPtrNew(MAX_FP_NUMBER*3); if (IS_ZERO(item.u.cplxval->imag)){ if (guess_real(item.u.cplxval->real,result)) return result; else { MemPtrFree(result); return NULL;} } if (IS_ZERO(item.u.cplxval->real)) if (guess_real(item.u.cplxval->imag,result)){ StrPrintF(result+StrLen(result),_T("*i")); return result; }; guessed = guess_complex(*item.u.cplxval,result); if (!guessed) { MemPtrFree(result); return NULL; } return result; } return NULL; } --- NEW FILE: defuns.h --- /* * $Id: defuns.h,v 1.1 2009/09/24 21:16:50 mapibid Exp $ * * Scientific Calculator for Palms. * Copyright (C) 1999 Ondrej Palkovsky * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * * You can contact me at 'on...@pe...'. */ #ifndef _DEFUNS_H_ #define _DEFUNS_H_ #include "segment.h" #include "konvert.h" #define MAX_FP_NUMBER 64 /* Application specific things */ #define APP_ID 'OpCl' #define LIB_ID 'OpCl' #define DBNAME _T("CalcDB-OpCl") #define HISTORYDBNAME _T("CalcDB-H-OpCl") #define SOLVERDBNAME _T("CalcDB-S-OpCl") #define DBTYPE 'Data' #define SOLVERDBTYPE 'Slvr' #define CARDNO 0 #define DBVERSION 5 #define HIST_DB_VERSION 6 #define SOLVER_DB_VERSION 1 #define PREF_DEFAULT 0 #define PREF_RESULT 1 #define PREF_GRAPH 3 #define PREF_LSTEDIT 4 #define PREF_MTXEDIT 5 #define PREF_DIA 6 /* Change this if you are changing contents of the preferences structure */ #define PREF_VERSION 44 #define PREF_RES_VERSION 2 #define PREF_GRAPH_VERSION 5 #define PREF_LSTEDIT_VERSION 1 #define PREF_MTXEDIT_VERSION 1 #define PREF_DIA_VERSION 1 /* Maximum length of normal strings copied from tSTR */ #define MAX_RSCLEN 30 #define MAX_RESULT 17 /* Rounding offset for reduce precision */ #define ROUND_OFFSET 13 #define PRECISION_TO_ZERO 1E-12 /* konvert.c constants */ #define MAX_RPNSIZE 105 #define MAX_OSTACK 45 //#define NaN ((double)(3-3)/(3-3)) TCHAR * guess(Trpn item) MLIB; //TCHAR * print_error(CError err); #endif Index: stack.cpp =================================================================== RCS file: /cvsroot/easycalc/PPCport/core/mlib/stack.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** stack.cpp 5 Jul 2009 20:53:37 -0000 1.3 --- stack.cpp 24 Sep 2009 21:16:50 -0000 1.4 *************** *** 37,41 **** #include "cmatrix.h" #include "compat/MathLib.h" ! #include "prefs.h" /*********************************************************************** --- 37,41 ---- #include "cmatrix.h" #include "compat/MathLib.h" ! #include "core/prefs.h" /*********************************************************************** *************** *** 210,215 **** Int16 i; if (dest->allocated < orig->size) ! ErrFatalDisplayIf(1,"Trying to copy larger stack into smaller."); ! for (i=0;i<orig->size;i++) rpn_duplicate(&dest->stack[i],orig->stack[i]); dest->size = orig->size; --- 210,215 ---- Int16 i; if (dest->allocated < orig->size) ! ErrFatalDisplayIf(1, _T("Trying to copy larger stack into smaller.")); ! for (i=0 ; i<(orig->size) ; i++) rpn_duplicate(&dest->stack[i],orig->stack[i]); dest->size = orig->size; *************** *** 378,382 **** realarg = tmpitem.u.realval; if (-2147483648.0 <= realarg && realarg <= 2147483647.0) { ! *((Int32 *)arg1) = tmpitem.u.realval; } else --- 378,382 ---- realarg = tmpitem.u.realval; if (-2147483648.0 <= realarg && realarg <= 2147483647.0) { ! *((Int32 *)arg1) = ((Int32) tmpitem.u.realval); } else *************** *** 908,912 **** stack_pop(CodeStack *stack) { ! ErrFatalDisplayIf(stack->size==0,"Null stack size"); stack->size--; --- 908,912 ---- stack_pop(CodeStack *stack) { ! ErrFatalDisplayIf(stack->size==0, _T("Null stack size")); stack->size--; *************** *** 958,962 **** { ErrFatalDisplayIf(stack->size==stack->allocated, ! "RPN Stack overflow"); stack->stack[stack->size]=rpn; stack->size++; --- 958,962 ---- { ErrFatalDisplayIf(stack->size==stack->allocated, ! _T("RPN Stack overflow")); stack->stack[stack->size]=rpn; stack->size++; Index: konvert.cpp =================================================================== RCS file: /cvsroot/easycalc/PPCport/core/mlib/konvert.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** konvert.cpp 5 Jul 2009 20:53:37 -0000 1.3 --- konvert.cpp 24 Sep 2009 21:16:50 -0000 1.4 *************** *** 36,40 **** #include "funcs.h" #include "stack.h" ! #include "prefs.h" #include "history.h" #include "integ.h" --- 36,40 ---- #include "funcs.h" #include "stack.h" ! #include "core/prefs.h" #include "history.h" #include "integ.h" Index: fp.cpp =================================================================== RCS file: /cvsroot/easycalc/PPCport/core/mlib/fp.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** fp.cpp 5 Jul 2009 20:53:37 -0000 1.2 --- fp.cpp 24 Sep 2009 21:16:50 -0000 1.3 *************** *** 532,536 **** /* Extract decimal digits of mantissa */ for (i = 0; i < prec; ++i, --dec) { ! Int32 d = fcd.d; *s++ = d + _T('0'); if (!dec) --- 532,536 ---- /* Extract decimal digits of mantissa */ for (i = 0; i < prec; ++i, --dec) { ! Int32 d = (Int32) (fcd.d); *s++ = d + _T('0'); if (!dec) Index: calcDB.cpp =================================================================== RCS file: /cvsroot/easycalc/PPCport/core/mlib/calcDB.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** calcDB.cpp 6 Aug 2009 21:41:40 -0000 1.3 --- calcDB.cpp 24 Sep 2009 21:16:50 -0000 1.4 *************** *** 313,316 **** --- 313,318 ---- MemPtrFree(record); *err = c_notvar; + memset(&result, 0, sizeof(result)); // Mapi: added this to return an initialized value + // in all cases. return result; } |