1. /* sdf2dbld.cpp by K.Tsuru */
  2. // function ID 3001
  3. /********************************************************************
  4. SDouble class
  5. Provides a conversion SDouble --> long double.
  6. If you let err = 0, an out of range of double does not terminate the
  7. program and return DBL_MAX or 0.0. The error (OVERFLOW_ERR or UNDERFLOW_ERR)
  8. can be detected by SNError().
  9. Default value is declared as err = 1.
  10. *********************************************************************/
  11. #ifndef SN_H
  12. #include "sn.h"
  13. #endif
  14. static const char* func = "doubleLD";
  15. long double ldpow10(const int n){
  16. if(!n) return 1.0;
  17. long double r(1), z(10.0);
  18. int m = n>0 ? n : -n;
  19. while(1){
  20. if( m & 1 ) r *= z;
  21. m /= 2;
  22. if( !m ) break;
  23. z *= z;
  24. }
  25. if(n > 0) return r;
  26. return 1.0/r;
  27. }
  28. ldouble doubleLD(const SDouble& sd, int err){
  29. if(sd.Type() != sd.REAL) SNManager::SetError(sd.RADIX_ERR, func, 3001);
  30. if(sd.SNSign() == 0) return 0.0;
  31. // detect error
  32. long de = sd.DExp();
  33. SNManager::SNErrorFlag error = sd.NO_ERR;
  34. ldouble d = 0.0;
  35. if(de > LDBL_MAX_10_EXP-1){
  36. error = sd.OVERFLOW_ERR;
  37. d = LDBL_MAX;
  38. }else if( de < LDBL_MIN_10_EXP+1 ){
  39. error = sd.UNDERFLOW_ERR;
  40. d = 0.0;
  41. }
  42. if(error != sd.NO_ERR){
  43. if(err) sd.SetError(error, func, 3001);
  44. else sd.SetErrorFlag(error);
  45. return d;
  46. }
  47. stringstream ss;
  48. sd.SetFormat(0, LDBL_DIG+2, 0, sd.ROUND);
  49. ss.precision(LDBL_DIG+2);
  50. ss << scientific << sd;
  51. ss >> d;
  52. return d;
  53. }

sdf2dbld.cpp : last modifiled at 2017/08/23 10:42:40(1,484 bytes)
created at 2017/10/07 10:22:50
The creation time of this html file is 2017/10/07 11:29:39 (Sat Oct 07 11:29:39 2017).