⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftoa.h

📁 电力系统中的保护装置全部代码
💻 H
字号:
/****************************************************************
	Convert floating binary to decimal	02-17-86
	Copyright 1986 Software Development Systems, Inc.
	All rights reserved
****************************************************************/
#define EBITS	(sizeof(FLT)==4 ? 6: 9)

/****************************************************************
	Converts the floating point number at 'isrc' to decimal.
	Stores characters for the first 'n' (or 'n' plus the number
	of digits left of decimal point when ffmt is nonzero,
	(which may result is less than 'n' digits if there are
	no digits left of the decimal point and in fact zeros to
	the right of the decimal point) significant digits (or
	MAXN digits if that is less) to the buffer at 'dst',
	followed by a nul, and returns 'dst'.  It is possible
	that no digits will be written to 'dst', just a nul.
	If the number was negative sets *sgnp to 1, else to 0.
	Stores the position of the decimal point in *ptp,
	(0==immediately left of the leftmost significant digit,
	negative means even further left.)
	??? infinity/nan  ???ovfl
	???monotonic ???cvb:cvd:cvb same as input number cases,etc.
****************************************************************/
char *FTOA( char *dst,
			int ffmt,
			FLT *isrc,
			int n,
			int *ptp,
			int *sgnp )
{
    rchar *p = dst;  rint exp, i;  auto long digit;  auto FLT src, f;

    src = *isrc;
    *sgnp = 0;
    if ( TSTM( &src ) ) {
	*sgnp = 1;
	NEG( &src, &src ); }

    /********************************************************
	    Range the number to get a decimal exponent.
    ********************************************************/
    exp = 0;
    for ( i = EBITS; --i >= 0; ) {
	if ( CMP( &src, &tens[i] ) >= 0 ) {
	    DIV( &src, &src, &tens[i] );
	    exp += (1 << i); }}
    if ( TST( &src ) ) {
	for ( i = EBITS; --i >= 0; ) {
	    MUL( &f, &src, &tens[i] );
	    if ( CMP( &f, &tens[0] ) < 0 ) {
		src = f;
		exp -= (1 << i); }}
	*ptp = exp + 1; }	/* a single integer digit is left */
    else {
	*ptp = 0; }

    if ( ffmt ) {
	/********************************************************
		For '%f' format 'n' is the number of fraction
		digits desired, which must be converted to the
		number of significant digits according to the
		number of digits left of the decimal point.
		Note that a negative '*ptp' here means there
		are zeros right of the decimal point before the
		first significant digit.
	********************************************************/
	n += *ptp;
	if ( n < 0 ) {
	    /********************************************************
		    Even rounding won't give a nonzero digit
		    in the desired fraction digits, so make
		    the number zero.  NOTE: for the n==0 case
		    a roundup can produce a digit below.
	    ********************************************************/
	    ZERO( &src );  n = 0;  *ptp = 0; }}
    if ( n > MAXN ) n = MAXN;	/* limit precision to size of buffer */

    /********************************************************
	    Now convert the significant digits.
    ********************************************************/
    while ( n-- ) {
	FTOL( &digit, &src, 0l );
	SUB( &src, &src, &ones[(int)digit] );
	MUL( &src, &src, &tens[0] );
	*p++ = '0' + (int)digit; }
    *p = 0;
    FTOL( &digit, &src, 0l );		/* next digit determines rounding */
    if ( digit == 5 ) {
	SUB( &src, &src, &ones[(int)digit] );
	if ( TST( &src ) || p!=dst && ((p[-1]-'0') & 1) ) digit = 6;
	else                                              digit = 4; }
    if ( digit > 5 ) {
	/********************************************************
			    round up.
	********************************************************/
	for ( i = p - dst - 1; i >= 0; --i ) {
	    if ( ++dst[i] <= '9' ) break;
	    dst[i] = '0'; }
	if ( i < 0 ) {		/* carry out of the entire digit string */
	    if ( p != dst ) {		/* there were digits (all 0 now) */
		dst[0] = '1';		/* 1000...0 */
		++(*ptp); }		/* compensate decimal point */
	    else if ( ffmt ) {		/* there were no digits, but ffmt */
		dst[0] = '1';		/*   wants the extra digit (other */
		dst[1] = 0;		/*   formats with prec 0 get no digs)*/
		++(*ptp); }}}		/* compensate decimal point */
    return dst; }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -