Skip to main content

alphaWorks  >  Forums  >  decNumber  >  developerWorks

Optimising use of decNumber    Point your RSS reader here for a feed of the latest messages in this thread


     

 
 

My developerWorks
 Welcome, Guest
Sign in or register
This question is answered.

Permlink Replies: 2 - Pages: 1 - Last Post: Jan 7, 2009 7:02 PM Last Post By: Jamesss Threads: [ Previous | Next ]
Jamesss

Posts: 3
Registered: Nov 17, 2008 05:13:37 PM
Optimising use of decNumber
Posted: Nov 17, 2008 05:39:30 PM
 
Click to report abuse...   Click to reply to this thread Reply
Hi,

I need to pass around a decNumber encoded in an unsigned 64 bit integer so currently I'm using the following code...

decNumber value;
    decContext context;
 
    ..... decContextDefault and so on.......
 
    // encode
    decimal64 d64;
    unsigned long long encodedFD;
    decimal64FromNumber( &d64, &value, &context );
    memcpy( &encodedFD, &d64, sizeof(unsigned long long) );
 
    // decode
    decimal64 d64;
    memcpy( &d64, &encodedFD, sizeof(unsigned long long) );
    decimal64ToNumber( &d64, &value )


In addition, I have to often initialise a decNumber from a separately supplied coefficient and exponent, e.g. (123456, 3) => 123.456

Currently I do it like so... (some error handling code etc ommited)

decNumber value;
    decContext context;
    decContextDefault( &context, DEC_INIT_BASE );
    context.traps = 0;
    
    if ( coefficient == 0 )
    {
        decNumberZero( &value );
    }
    // values that fit in 32 bits can be handled by decNumberFromInt32
    else if ( ( coefficient <= LONG_MAX ) && ( coefficient >= LONG_MIN ) )
    {
        decNumberFromInt32( &value, coefficient );
        
        if ( exponent != 0 )
        {
            decNumber numExponent;
            decNumberFromInt32( &numExponent, -exponent );
            decNumberScaleB( &value, &value, &numExponent, &context );
        }
    }
    // larger values use string conversion routines
    else
    {
        char buff[DECNUMBER_BUFFSIZE];
        decNumberFromString( &value, lltoa(buff, coefficient), &context );
 
        if ( exponent != 0 )
        {
            // raise 10 to the power of -exponent
            decNumber thePow, ten;
            ... set thePow to -exponent ...
            ... set ten to 10 ...
            decNumberPower( &ten, &ten, thePow, &context );
 
            decNumberMultiply( &value, &value, &ten, &context );
        }
    }


Simply put, is there a faster or better way to do this?

thanks,

James
mfc

Posts: 7
Registered: Jul 23, 2005 02:55:17 AM
Re: Optimising use of decNumber
Posted: Dec 21, 2008 09:17:06 AM   in response to: Jamesss in response to: Jamesss's post
 
Click to report abuse...   Click to reply to this thread Reply
First -- apologies for the slow respose .. for some reason I did not receive the original post.

On the first question: memcpy is ususally very efficiently compiled, so that is probably a good choice (if concerned, generate an assembly listing; the memcpy should be translated to a few in-line instructions. If there's a call to a subroutine, then be worried).

On the second: looks good, except for raising number to the power, ScaleB should be a lot faster.

Mike
Jamesss

Posts: 3
Registered: Nov 17, 2008 05:13:37 PM
Re: Optimising use of decNumber
Posted: Jan 07, 2009 07:02:57 PM   in response to: mfc in response to: mfc's post
 
Click to report abuse...   Click to reply to this thread Reply
Thanks for the reply Mike and Happy New Year!

Point your RSS reader here for a feed of the latest messages in all forums