Thread: Suggestions desired

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733

    Suggestions desired

    The part marked with a TODO comment in the below is what I'm looking for suggestions on:
    Code:
    #define ACHS_0TO9 "0123456789"
    #define ACHS_ATOZ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    #define ACHS_atoz "abcdefghijklmnopqrstuvwxyz"
    
    typedef struct _ACHN
    {
    	uint not0;
    	ach	sign[2];
    	ach text[bitsof(umax)];
    } ACHN;
    
    typedef struct _ACHF
    {
    	ACHN full, part, exp;
    } ACHF;
    
    BASIC void	umax2achs( ACHN *dst, umax val, achs base )
    {
    	ach tmp[bitsof(umax)] = {0};
    	uint i = 0, j = 0, max = achsnot0( base );
    
    	memset( dst, 0, sizeof(ACHN) );
    
    	while ( val )
    	{
    		tmp[j++] = base[val % max];
    		val /= max;
    	}
    
    	dst->not0 = j;
    	while ( j )
    		dst->text[i++] = tmp[--j];
    }
    
    BASIC void smax2achs( ACHN *dst, smax val, achs base )
    {
    	if ( val < 0 )
    	{
    		umax2achs( dst, -val, base );
    		dst->sign[0] = '-';
    		return;
    	}
    	umax2achs( dst, val, base );
    }
    
    typedef union _LDNUM_UNION
    {
    	ldnum ln;
    	uchar bytes[sizeof(ldnum)];
    } LDNUM_UNION;
    
    BASIC void ldnum2achs( ACHF *dst, ldnum val, achs base )
    {
    	LDNUM_UNION mantissa = {0};
    	smax full = 0, part = 0;
    	dint exp = 0;
    	uint max = achsnot0( base );
    
    	mantissa = frexpl( val, &exp );
    	smax2achs( &(dst->exp), exp, ACHS_0TO9 );
    	if ( exp < LDBL_MIN_10_EXP || exp > LDBL_MAX_10_EXP )
    	{
    		/* TODO: */
    	}
    	else
    	{
    		full = (smax)val;
    		umax2achs( &(dst->full), full, base );
    		val -= full;
    		while ( floorl(val) != val )
    			val *= max;
    		part = (smax)val;
    		umax2achs( &(dst->part), part, base );
    	}
    }
    Never converted a floating point number to text via the standard api before so not sure how to extract the larger/smaller numbers, best I can think of is to bring the size down/up prior to extracting any numbers

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    wound up going with this:
    Code:
    #define ACHS_0TO9 "0123456789"
    #define ACHS_ATOZ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    #define ACHS_atoz "abcdefghijklmnopqrstuvwxyz"
    
    typedef struct _ACHN
    {
    	uint not0;
    	ach	sign[2];
    	ach text[bitsof(umax)];
    } ACHN;
    
    typedef struct _ACHF { ACHN full, part, exp; } ACHF;
    typedef union _ACHV { ACHF n; ACHN i[3]; } ACHV;
    
    BASIC void	umax2achs( ACHN *dst, umax val, achs base )
    {
    	ach tmp[bitsof(umax)] = {0};
    	uint i = 0, j = 0, max = achsnot0( base );
    
    	memset( dst, 0, sizeof(ACHN) );
    	if ( !max )
    		return;
    
    	while ( val )
    	{
    		tmp[j++] = base[val % max];
    		val /= max;
    	}
    
    	dst->not0 = j;
    	while ( j )
    		dst->text[i++] = tmp[--j];
    }
    
    BASIC void smax2achs( ACHN *dst, smax val, achs base )
    {
    	if ( val < 0 )
    	{
    		umax2achs( dst, -val, base );
    		dst->sign[0] = '-';
    		return;
    	}
    	umax2achs( dst, val, base );
    }
    
    BASIC void ldnum2achs( ACHF *dst, ldnum val, achs base )
    {
    	dint exp = 0;
    	ldnum full = 0, part = 0;
    	uint max = achsnot0( base );
    
    	if ( !max )
    	{
    		memset( dst, 0, sizeof(ACHF) );
    		return;
    	}
    
    	frexpl( val, &exp );
    
    	if ( exp > LDBL_MAX_10_EXP )
    	{
    		if ( val < 0 )
    		{
    			while ( -val > max )
    				val /= max;
    		}
    		else
    		{
    			while ( val > max )
    				val /= max;
    		}
    	}
    
    	if ( val > 0u )
    	{
    		full = floorl( val );
    		part = val - full;
    	}
    	else
    	{
    		full = ceill(val);
    		part = -(val + full);
    	}
    
    	while ( floorl(part) != part )
    		part *= max;
    
    	smax2achs( &(dst->full), (smax)full, base );
    	umax2achs( &(dst->part), (umax)part, base );
    	smax2achs( &(dst->exp), exp, ACHS_0TO9 );
    }
    Currently untested but I'll get to that later after I fix a pressing bug elsewhere

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why am i not getting the desired output??
    By Priyanka Mandal in forum C++ Programming
    Replies: 2
    Last Post: 09-04-2013, 01:30 PM
  2. Not Getting Desired Output
    By DevoAjit in forum C Programming
    Replies: 2
    Last Post: 03-09-2012, 05:46 AM
  3. Not getting the desired o/p with ~ operator. Kindly help.
    By DevinLgls in forum C Programming
    Replies: 5
    Last Post: 05-24-2011, 12:03 PM
  4. Much desired program.
    By MannyCalavera in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 09-23-2005, 05:40 PM
  5. not getting the desired output. WHY???
    By KristTlove in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2003, 02:08 PM

Tags for this Thread