📄 wymath.cpp
字号:
/* Copyright license: GNU LGPL (see file COPYING) by I.J.Wang (2004)*/#define WYLIB_SOURCE#include "wymath.h"#include "wy__scanum.h"#include <math.h> // for isnan(..)..//// See wynum.h for declaration//inline static bool wy__testfail_neg(float) { return false; };inline static bool wy__testfail_neg(double) { return false; };namespace Wy {// Accept: [+-]ddd.ddd[(eE)[+-]ggg]//// ddd is internally converted to long long, digits of ddd>18 may overflow//WyRet _strnum(double& res, const char** endptr, WyCSeg cseg){ typedef double FopType; // Type for float operation typedef long long ResInt; // Integer type for result of text conversion const char* buf=cseg.begin(); const size_t blen=cseg.size(); WyRet r; ResInt res1,res2; int fdig,texp; size_t sblen(blen); for(;;) { r=Wy::wy_strnum3(res1,res2,fdig,texp,endptr,buf,sblen); const int dpp=fdig+texp; if(texp<=0) { if(dpp>fdig) { if(sblen<7) { WY_THROW( WyRet() ); } --sblen; continue; } } else { if(dpp<=fdig) { if(sblen<7) { WY_THROW( WyRet() ); } --sblen; continue; } } res=(static_cast<FopType>(res1)*std::pow(10.0,static_cast<double>(texp))+ static_cast<FopType>(res2)*std::pow(10.0,static_cast<double>(dpp)) ); //if(finite(res)==0) if(isinf(res)||isnan(res)) { --sblen; continue; } if(r==Ok) { break; } if(sblen==blen) { break; } } if(sblen!=blen) { WY_RETURN(Wym_ERANGE); } return(r);};//// Accept: [+-]ddd.ddd[(eE)[+-]ggg]//// ddd is internally converted to long long, digits of ddd>18 may overflow//WyRet _strnum(float& res, const char** endptr, WyCSeg cseg){ typedef double FopType; // Type for float operation typedef long long ResInt; // Integer type for result of text conversion const char* buf=cseg.begin(); const size_t blen=cseg.size(); WyRet r; ResInt res1,res2; int fdig,texp; size_t sblen(blen); for(;;) { r=Wy::wy_strnum3(res1,res2,fdig,texp,endptr,buf,sblen); const int dpp=fdig+texp; if(texp<=0) { if(dpp>fdig) { if(sblen<7) { WY_THROW( WyRet() ); } --sblen; continue; } } else { if(dpp<=fdig) { if(sblen<7) { WY_THROW( WyRet() ); } --sblen; continue; } } res=(static_cast<FopType>(res1)*std::pow(10.0,static_cast<double>(texp))+ static_cast<FopType>(res2)*std::pow(10.0,static_cast<double>(dpp)) ); //if(finite(res)==0) if(isinf(res)||isnan(res)) { --sblen; continue; } if(r==Ok) { break; } if(sblen==blen) { break; } } if(sblen!=blen) { WY_RETURN(Wym_ERANGE); } return(r);};WyRet _strnum(float& res, const char** endptr, WyCSeg cseg, const int& radix) { if((radix!=10)&&(radix!=0)) { WY_RETURN( Wym_EINVAL ); } return _strnum(res,endptr,cseg); };WyRet _strnum(double& res, const char** endptr, WyCSeg cseg, const int& radix) { if((radix!=10)&&(radix!=0)) { WY_RETURN( Wym_EINVAL ); } return _strnum(res,endptr,cseg); };WyRet _strnum(float& res, const char** endptr, WyCSeg cseg, int& radix) { if(radix!=10) { if(radix!=0) { WY_RETURN( Wym_EINVAL ); } radix=10; } return _strnum(res,endptr,cseg); };WyRet _strnum(double& res, const char** endptr, WyCSeg cseg, int& radix) { if(radix!=10) { if(radix!=0) { WY_RETURN( Wym_EINVAL ); } radix=10; } return _strnum(res,endptr,cseg); };bool wy_roughly_equ(double a, double b) WY__TSPC(){ const double ErrorRatio=1e-7; if(b==0) { return (a>-ErrorRatio)&&(a<ErrorRatio); } else { const double er((b-a)/b); return (er>-ErrorRatio)&&(er<ErrorRatio); }};//-----------WyRet _scanum(float& n, size_t& idx, const WyCSeg& text, const int& radix){ return wy__scanum(n,idx,text,radix); };WyRet _scanum(double& n, size_t& idx, const WyCSeg& text, const int& radix){ return wy__scanum(n,idx,text,radix); };WyRet _scanum(float& n, size_t& idx, const WyCSeg& text){ return wy__scanum(n,idx,text,0); };WyRet _scanum(double& n, size_t& idx, const WyCSeg& text){ return wy__scanum(n,idx,text,0); };}; // End of Wy
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -