home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Arashi 1.1.1 / source code / For your think c folder / Misc / Numbers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-14  |  2.7 KB  |  162 lines  |  [TEXT/KAHL]

  1. /*/
  2.      Project Arashi: Numbers.c
  3.      Major release: Version 1.1d2, 9/5/95
  4.  
  5.      Last modification: Friday, July 14, 1995, 0:49
  6.      Created: Sunday, July 25, 1993, 18:48
  7.  
  8.      Copyright © 1993-1995, Juri Munkki
  9. /*/
  10.  
  11. /*
  12. **    Some floating point number conversion utilies.
  13. */
  14. #include "cplusminusutil.h"
  15. #include "NumbersInternal.h"
  16. #include "LexStuff.h"
  17.  
  18. #ifdef NO_PRECOMPILED_HEADERS
  19. #include <Memory.h>
  20. #include <TextUtils.h>
  21. #endif
  22.  
  23. double_t    StringToLongDouble(
  24.     StringPtr    pstr)
  25. {
  26. #ifdef __cplusplus
  27.     double_t    result;
  28.     decimal        d;
  29.     char        tempString[256];
  30.     
  31.     
  32.     BlockMoveData(pstr + 1, tempString, pstr[0]);    //<fp.h>
  33.     tempString[pstr[0]] = '\0';
  34.  
  35.     if(MatchDouble((StringPtr) tempString, &result) == 0)
  36.     {    result = MakeNaN(255);
  37.     }
  38.     
  39.     return result;
  40. #else
  41.     #if !__option(mc68881) && __option(native_fp)
  42.         return str2num(pstr);
  43.     #else
  44.         extended    temp;
  45.         double_t    result;
  46.         
  47.         temp = str2num(pstr);
  48.         x80tox96(&temp, &result);
  49.         return result;
  50.     #endif
  51. #endif
  52. }
  53.  
  54. NumberClass    FindNumClass(
  55.     double_t    n)
  56. {
  57. #ifdef powerc
  58.     return (NumberClass) fpclassify(n);
  59. #else
  60.     #ifdef __MWERKS__
  61.         extended80    temp;
  62.         NumClass    theClass;
  63.         
  64.         x96tox80((extended96 *)&n, &temp);
  65.         ClassExtended(&temp, &theClass);
  66.  
  67.         return (NumberClass) theClass;
  68.     #else
  69.     #if !__option(mc68881) && __option(native_fp)
  70.         return (NumberClass) classextended(n);
  71.     #else
  72.         extended    temp;
  73.         
  74.         x96tox80(&n, &temp);
  75.         return (NumberClass) classextended(temp);
  76.     #endif
  77.     #endif
  78. #endif
  79. }
  80.  
  81. double_t    MakeNaN(
  82.     short    n)
  83. {
  84. #ifdef powerc
  85.     char    temp[32];
  86.     
  87.     NumToString((long) n, (StringPtr) temp);
  88.     temp[temp[0] +1] = '\0';
  89.     return nanl(temp + 1);    // fp.h
  90.     
  91. #else
  92.     #if __MWERKS__
  93.         extended80    temp;
  94.         double_t    result;
  95.         
  96.         temp = NAN(n);
  97.         x80tox96(&temp, (extended96 *)&result);
  98.         return result;
  99.     #else
  100.     #if !__option(mc68881) && __option(native_fp)
  101.         return nan(n);
  102.     #else
  103.         extended    temp;
  104.         double_t    result;
  105.         
  106.         temp = nan(n);
  107.         x80tox96(&temp, &result);
  108.         return result;
  109.     #endif
  110.     #endif
  111. #endif
  112. }
  113.  
  114. void    NumberToDecimalRecord(
  115.     double_t        number,
  116.     decimal            *decimalRecord,
  117.     short            howManyDecimals)
  118. {
  119.     decform        myDecForm;
  120.     
  121.     myDecForm.style = 1;
  122.     myDecForm.digits = howManyDecimals;
  123.     decimalRecord->exp = 0;
  124.  
  125. #ifdef powerc
  126.     num2dec(&myDecForm, number, decimalRecord);
  127. #else
  128.     #if __MWERKS__
  129.     {
  130.         extended80    temp;
  131.     
  132.         x96tox80((extended96 *)&number, &temp);
  133.         Num2Dec(&myDecForm, &temp, decimalRecord);
  134.     }
  135.     #else
  136.     #if !__option(mc68881) && __option(native_fp)
  137.         num2dec(&myDecForm,number,decimalRecord);
  138.     #else
  139.     {
  140.         extended    temp;
  141.     
  142.         x96tox80(&number, &temp);
  143.         num2dec(&myDecForm, temp, decimalRecord);
  144.     }
  145.     #endif
  146.     #endif
  147. #endif
  148. }
  149.  
  150. void    SetRoundToNearest(void)
  151. {
  152. #ifdef powerc
  153.     fesetround(FE_TONEAREST);    //    fenv.h
  154. #else
  155. #ifdef __MWERKS__
  156.     SetRound(ToNearest);        //    <SANE.h> types.h
  157. #else
  158.     setround(TONEAREST);        //    <SANE.h> types.h
  159. #endif
  160. #endif
  161. }
  162.