Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

utl_mpfr.c

Go to the documentation of this file.
00001 #include "calc.h"
00002 
00003 void prepare_rpn_result_2(calc_number_t *rpn, TCHAR *buffer, int size, int base)
00004 {
00005     char   temp[1024];
00006     char  *ptr, *dst;
00007     int    width, max_ld_width;
00008     unsigned long int n, q;
00009     mpz_t   zz;
00010     mpf_t   ff;
00011 
00012     mpz_init(zz);
00013     mpf_init(ff);
00014     mpfr_get_z(zz, rpn->mf, MPFR_DEFAULT_RND);
00015     mpfr_get_f(ff, rpn->mf, MPFR_DEFAULT_RND);
00016 
00017     switch (base) {
00018     case IDC_RADIO_HEX:
00019         gmp_sprintf(temp, "%ZX", zz);
00020         break;
00021     case IDC_RADIO_DEC:
00022         /*
00023          * The output display is much shorter in standard mode,
00024          * so I'm forced to reduce the precision here :(
00025          */
00026         if (calc.layout == CALC_LAYOUT_STANDARD)
00027             max_ld_width = 16;
00028         else
00029             max_ld_width = 64;
00030 
00031         /* calculate the width of integer number */
00032         if (mpf_sgn(ff) == 0)
00033             width = 1;
00034         else {
00035             mpfr_t t;
00036             mpfr_init(t);
00037             mpfr_abs(t, rpn->mf, MPFR_DEFAULT_RND);
00038             mpfr_log10(t, t, MPFR_DEFAULT_RND);
00039             width = 1 + mpfr_get_si(t, MPFR_DEFAULT_RND);
00040             mpfr_clear(t);
00041         }
00042         if (calc.sci_out == TRUE || width > max_ld_width || width < -max_ld_width)
00043             ptr = temp + gmp_sprintf(temp, "%*.*#Fe", 1, max_ld_width, ff);
00044         else {
00045             ptr = temp + gmp_sprintf(temp, "%#*.*Ff", width, ((max_ld_width-width-1)>=0) ? max_ld_width-width-1 : 0, ff);
00046             dst = strchr(temp, '.');
00047             while (--ptr > dst)
00048                 if (*ptr != '0')
00049                     break;
00050 
00051             /* put the string terminator for removing the final '0' (if any) */
00052             ptr[1] = '\0';
00053             /* check if the number finishes with '.' */
00054             if (ptr == dst)
00055                 /* remove the dot (it will be re-added later) */
00056                 ptr[0] = '\0';
00057         }
00058         break;
00059     case IDC_RADIO_OCT:
00060         gmp_sprintf(temp, "%Zo", zz);
00061         break;
00062     case IDC_RADIO_BIN:
00063         /* if the number is zero, just write 0 ;) */
00064         if (rpn_is_zero(rpn)) {
00065             temp[0] = TEXT('0');
00066             temp[1] = TEXT('\0');
00067             break;
00068         }
00069         /* repeat until a bit set to '1' is found */
00070         n = 0;
00071         do {
00072             q = mpz_scan1(zz, n);
00073             if (q == ULONG_MAX)
00074                 break;
00075             while (n < q)
00076                 temp[n++] = '0';
00077             temp[n++] = '1';
00078         } while (1);
00079         /* now revert the string into TCHAR buffer */
00080         for (q=0; q<n; q++)
00081             buffer[n-q-1] = (temp[q] == '1') ? TEXT('1') : TEXT('0');
00082         buffer[n] = TEXT('\0');
00083 
00084         mpz_clear(zz);
00085         mpf_clear(ff);
00086         return;
00087     }
00088     mpz_clear(zz);
00089     mpf_clear(ff);
00090     _sntprintf(buffer, SIZEOF(calc.buffer), TEXT("%s"), temp);
00091 }
00092 
00093 void convert_text2number_2(calc_number_t *a)
00094 {
00095     int base;
00096 #ifdef UNICODE
00097     int sz;
00098     char *temp;
00099 #endif
00100 
00101     switch (calc.base) {
00102     case IDC_RADIO_HEX: base = 16; break;
00103     case IDC_RADIO_DEC: base = 10; break;
00104     case IDC_RADIO_OCT: base = 8; break;
00105     case IDC_RADIO_BIN: base = 2; break;
00106     default: return;
00107     }
00108 #ifdef UNICODE
00109 /*
00110  * libmpfr and libgmp accept only ascii chars.
00111  */
00112     sz = WideCharToMultiByte(CP_ACP, 0, calc.buffer, -1, NULL, 0, NULL, NULL);
00113     if (!sz)
00114         return;
00115     temp = (char *)_alloca(sz);
00116     sz = WideCharToMultiByte(CP_ACP, 0, calc.buffer, -1, temp, sz, NULL, NULL);
00117     mpfr_strtofr(a->mf, temp, NULL, base, MPFR_DEFAULT_RND);
00118 #else
00119     mpfr_strtofr(a->mf, calc.buffer, NULL, base, MPFR_DEFAULT_RND);
00120 #endif
00121 }
00122 
00123 void convert_real_integer(unsigned int base)
00124 {
00125     switch (base) {
00126     case IDC_RADIO_DEC:
00127         break;
00128     case IDC_RADIO_OCT:
00129     case IDC_RADIO_BIN:
00130     case IDC_RADIO_HEX:
00131         if (calc.base == IDC_RADIO_DEC) {
00132             mpfr_trunc(calc.code.mf, calc.code.mf);
00133             apply_int_mask(&calc.code);
00134         }
00135         break;
00136     }
00137 }
00138 

Generated on Thu Feb 9 04:38:58 2012 for ReactOS by doxygen 1.6.3

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.