| dhp -
Dalsoft High Precision package allows to perform floating point
calculations with the precision higher than that supported by most of
the numeric hardware. It provides C++ wrapper which allows for easy
incorporation with the existing code. dhp may be particularly useful in number of ways:
Technical specifications
the amount of the available memory' Currently the library is provided only as a 64-bit object file for a x86 Linux OS and a 32-bit object file for the Windows OS. WE ASSUME NO LIABILITY WHATSOEVER, AND DISCLAIM ANY EXPRESS OR IMPLIED WARRANTY, RELATING TO SALE AND/OR USE OF OUR PRODUCTS INCLUDING LIABILITY OR WARRANTIES RELATING TO FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT. Our products are not intended for use in medical, life saving, life sustaining, critical control or safety systems, or in nuclear facility applications The software described in this document may contain software defects which may cause it to deviate from expected behavior. InstallationTo install dhp, copy the provided library ( linux_libdhp.a or windows_libdhp.a, dhp.lib ) and included file ( dhp.h ) into the appropriate working directories ( on Linux it may be /usr/local/lib64 for libdhp.a and /usr/local/include for dhp.h ) renaming the library as libdhp.a; e.g. on Linux do:sudo
cp linux_libdhp.a /usr/local/lib64/libdhp.a
See ReleaseNotes for more details. High Precision valuesdhp uses high precision representation of the floating point numbers. This representation has the form ( for non-zero values ):1.fraction
× 2exponent
and provides 32 bit exponent ( compare with 11 bits for IEEE double precision ). For C programs, dhp supports high precision representations with the fraction size of 128 and 1024 bits ( compare with 52 bits for IEEE double precision ). For C++ programs, dhp supports high precision representations with any fraction size . The fraction size if a high precision representation of the floating point number supported by dhp, is called it precision. Compiling and linking with the dhp libraryThis section describes how to use the dhp with your programs.To use dhp in C/C++ programs, you must #include the file dhp.h in every source file that calls dhp functions, accesses dhp global variables, or uses #defined constants provided by dhp. The procedures for linking dhp with the rest of your program vary according to the compiler and method you are using. When using C compiler, you must include the standard C++ library. On a typical Linux system this can be done as following gcc myprogram.c -ldhp -lstdc++
or g++ myprogram.cpp -ldhp
Exceptionsdhp functions, in addition to producing result, may also generate error code ( exceptions ). The possible error codes defined in dhp.h file and are:
Programming with dhpThis section describes how to make dhp calls in your program.Global Variablesdhp_errorunsigned int dhp_error; dhp_error contains the error code from the last executed dhp function. If the call succeeded, dhp_error will be 0 ( DHP_ERR_OK ). A list of error code values, their meanings, and #defined constants for them is in section ???. dhp_error_sum unsigned int dhp_error_sum; dhp_error_sum record bitwise OR of the error code from dhp calls. A list of error code values, their meanings, and #defined constants for them is in section ???. dhp_version unsigned short dhp_version; dhp_version contains the dhp's version in packed BCD format. For example, if the dhp's version is 2.1, the value of dhp_version will be 0x0201. The high byte of dhp_version represents two digits to the left of the decimal point (one digit per nibble) and the low byte represents two digits to the right of the decimal point (again, one digit per nibble). Using dhp in C++The C++ class supported by dhp is dhpreal. It can be defined in the following ways:dhpreal data() - defines data to be high precision representation of the floating point numbers that provides provides 32-bit exponent and 128-bit fraction. dhpreal data( unsigned int FractionSizeInBits ) - defines data to be high precision representation of the floating point numbers that provides provides 32 bits exponent and and at least FractionSizeInBits bits fraction. dhpreal data( const dhpreal& source ) - defines data to be high precision representation identical to source ( having the same fraction size and the same value ). Note that some compilers require explicit cast for defining dhprel data items with a parameter, e.g. dhpreal data(
( unsigned int )100 );
to define data to be high precision representation of the floating point numbers that provides 32 bits exponent and at least 100 bits fraction. dhp supports all arithmetic operations on the variables of the type dhpreal and allows mixing them with argument of the type double. For example, the following: dhpreal a, b(500);
a = 1.23; b = a*10; will define a to be a floating point high precision representation with the fraction size of 128 bits, b to be a floating point high precision representation with the fraction size of 500 bits, set a to 1.23 and b to 12.3 dhp provides the following C++ class functionsget_doubledouble get_double()return the value of this as a double. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF Example: dhpreal a;
double d; . d = a.get_double(); get_stringint get_string( char *str, unsigned int strSz, unsigned int cntAfterDot )set str to represent the value of this in the following format: [-+][D1][.D2]
where D1 - is a decimal integer denoting the integer part of the floating point value D2 - is a decimal integer denoting the fractional part of the floating point value. At most strSz characters of str are affected ( including null character terminating the string ) and at most cntAfterDot characters are set after the decimal point '.'. return not-zero value if str contains the integer part D1 ( and decimal point in the case this is not a whole number ), zero - otherwise Possible Exceptions: none Example: dhpreal a;
char s[1024]; int stat; . stat = a.get_string( s, 1024, 4 ); set_stringvoid set_string( const char *str )set this to floating point number represented as a string str. Input string str has the following format: [-+][D1][.D2]
where D1 - is a decimal integer denoting the integer part of the floating point value D2 - is a decimal integer denoting the fractional part of the floating point value Result of on inputs not confirming to the above description is undefined. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF Example: dhpreal a;
. a.set_string( "-123.456" ); is0int is0()return not-zero value if this is equal to 0, zero - if this has numeric representation of a not zero value. Possible Exceptions: none Example: dhpreal a( 2000 );
. if ( !a.is0() ) . operator=dhpreal& operator( double src )set this to the value of src. Possible Exceptions: DHP_ERR_INV, DHP_ERR_OVF Example: dhpreal a( 250 );
double d; . a = d; dhpreal& operator=( const dhpreal& src ) set this to the value of src; the precision of this ( the size of it fraction ) is not affected. Possible Exceptions: none Example: dhpreal a( 250 ), b;
. a = b; dhp provides the following C++ external functionsoperator+const dhpreal operator+( const dhpreal& src1, const dhpreal& src2 )return dhpreal value equal src1 + src2. The precision of the returned result is set to the maximum of precisions of src1 and src2. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF const dhpreal operator+( double src1, const dhpreal& src2 ) const dhpreal operator+( const dhpreal& src1, double src2 ) return dhpreal value equal src1 + src2. The precision of the returned result is set to the maximum of precisions of the dhpreal argument. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF, DHP_ERR_INV Example: dhpreal a, b( 200 ), c;
. a = b + c; c = a + 1.2; operator-const dhpreal operator-( const dhpreal& src1, const dhpreal& src2 )return dhpreal value equal src1 - src2. The precision of the returned result is set to the maximum of precisions of src1 and src2. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF const dhpreal operator-( double src1, const dhpreal& src2 ) const dhpreal operator-( const dhpreal& src1, double src2 ) return dhpreal value equal src1 - src2. The precision of the returned result is set to the maximum of precisions of the dhpreal argument. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF, DHP_ERR_INV Example: dhpreal a, b( 200 ), c;
. a = b - c; c = a - 1.2; operator*const dhpreal operator*( const dhpreal& src1, const dhpreal& src2 )return dhpreal value equal src1 * src2. The precision of the returned result is set to the maximum of precisions of src1 and src2. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF const dhpreal operator*( double src1, const dhpreal& src2 ) const dhpreal operator*( const dhpreal& src1, double src2 ) return dhpreal value equal src1 * src2. The precision of the returned result is set to the maximum of precisions of the dhpreal argument. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF, DHP_ERR_INV Example: dhpreal a, b( 200 ), c;
. a = b * c; b = c*3.4; operator/const dhpreal operator/( const dhpreal& src1, const dhpreal& src2 )return dhpreal value equal src1 / src2. The precision of the returned result is set to the maximum of precisions of src1 and src2. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF, DHP_ERR_DZE const dhpreal operator/( double src1, const dhpreal& src2 ) const dhpreal operator/( const dhpreal& src1, double src2 ) return dhpreal value equal src1 / src2. The precision of the returned result is set to the maximum of precisions of the dhpreal argument. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF, DHP_ERR_INV, DHP_ERR_DZE Example: dhpreal a, b( 200 ), c;
. a = b / c; c = 5.6/b; compare functionsconst int operator<( const dhpreal& src1, const dhpreal& src2 )const int operator<=( const dhpreal& src1, const dhpreal& src2 ) const int operator>( const dhpreal& src1, const dhpreal& src2 ) const int operator>=( const dhpreal& src1, const dhpreal& src2 ) const int operator==( const dhpreal& src1, const dhpreal& src2 ) const int operator!=( const dhpreal& src1, const dhpreal& src2 ) Possible Exceptions: none const int operator<( const dhpreal& src1, double src2 ) const int operator<( double src1, const dhpreal& src2 ) const int operator<=( const dhpreal& src1, double src2 ) const int operator<=( double src1, const dhpreal& src2 ) const int operator>( const dhpreal& src1, double src2 ) const int operator>( double src1, const dhpreal& src2 ) const int operator>=( const dhpreal& src1, double src2 ) const int operator>=( double src1, const dhpreal& src2 ) const int operator==( const dhpreal& src1, double src2 ) const int operator==( double src1, const dhpreal& src2 ) const int operator!=( const dhpreal& src1, double src2 ) const int operator!=( double src1, const dhpreal& src2 ) Possible Exceptions: DHP_ERR_INV return not-zero value if corresponding relation between src1 and src2 is true, zero - otherwise. Example: dhpreal a, b( 200 ), c;
. if ( ( a < b ) && ( c >= 2.3 ) ) sqrtdhpreal sqrt( const dhpreal& src )return dhpreal value equal SQRT( src ). The precision of the returned result is set to the precision of src. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF, DHP_ERR_INV Example: dhpreal a, b( 200 );
. a = sqrt( b ); truncdhpreal trunc( const dhpreal& src )return dhpreal value equal to the integer nearest to src and not larger in absolute value than src. The precision of the returned result is set to the precision of src. Possible Exceptions: none Example: dhpreal a, b( 200 );
. a = trunc( b ); rounddhpreal round( const dhpreal& src )return dhpreal value equal to the integer nearest to src. The precision of the returned result is set to the precision of src. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF Example: dhpreal a, b( 200 );
. a = round( b ); fabsdhpreal fabs( const dhpreal& src )return dhpreal value equal to the absolute value of src.The precision of the returned result is set to the precision of src. Possible Exceptions: none Example: dhpreal a, b( 200 );
. a = fabs( b ); cmpint cmp( const dhpreal& src1, const dhpreal& src2 )return and integer less than, equal to, or greater than zero is src1 is , respectively, less than, equal, or greater than src2 Possible Exceptions: none Example: dhpreal a, b( 200 );
. if ( cmp( a, b ) < 0 ) Using dhp in CNaming conventionsAll functions, provided by dhp have name that begins with 'dhp_' or 'dhp1024_' ( depending on the type of it arguments/result ) and followed by the low-case mnemonic, e.g 'dhp_add'. Data typesThe C data types supported by dhp are dhp_t and dhp1024_t dhp_t is a high precision representation of the floating point numbers that provides 32 bit exponent and 128 bits fraction. dhp1024_t is a high precision representation of the floating point numbers that provides 32 bit exponent and 1024 bits fraction. dhp C functionsdhp provides several sets of functions for each of the supported data types.
dhp_get_double, dhp1024_get_doubledouble dhp_get_double( dhp_t src )double dhp1024_get_double( dhp1024_t src ) return the value of src as a double. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF Example: dhp_t a;
double d; . d = dhp_get_double( a ); dhp_get_string, dhp1024_get_stringint dhp_get_string( dhp_t, src, char *str, unsigned int strSz, unsigned int cntAfterDot )int dhp1024_get_string( dhp1024_t, src, char *str, unsigned int strSz, unsigned int cntAfterDot ) set str to represent the value of src in the following format: [-+][D1][.D2]
where D1 - is a decimal integer denoting the integer part of the floating point value D2 - is a decimal integer denoting the fractional part of the floating point value. At most strSz characters of str are affected ( including null character terminating the string ) and at most cntAfterDot characters are set after the decimal point '.'. return not-zero value if str contains the integer part D1 ( and decimal point in the case this is not a whole number ), zero - otherwise Possible Exceptions: none Example: dhp_t a;
char s[1024]; int stat; . stat = dhp_get_string( a, s, 1024, 4 ); dhp_set_double, dhp1024_set_doubledhp_t dhp_set_double( double src )dhp1024_t dhp1024_set_double( double src ) return the item of an appropriate data type that has high precision representation of src. Possible Exceptions: DHP_ERR_INV, DHP_ERR_OVF Example: dhp1024_t a;
double d; . a = dhp1024_set_double( d ); dhp_set_string, dhp1024_set_stringdhp_t dhp_set_string( const char *str )dhp1024_t dhp1024_set_string( const char *str ) return the item of an appropriate data type that has high precision floating point number with the value represented as a string str. Input string str has the following format: [-+][D1][.D2]
where D1 - is a decimal integer denoting the integer part of the floating point value D2 - is a decimal integer denoting the fractional part of the floating point value Result of on inputs not confirming to the above description is undefined. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF Example: dhp_t a;
. a = dhp_set_string( "-123.456" ); dhp_add, dhp1024_adddhp_t dhp_add( dhp_t src1, dhp_t src2 )dhp1024_t dhp1024_add( dhp1024_t src1, dhp1024_t src2 ) return the item of an appropriate data type that has high precision floating point number with the value equal to src1 + src2. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF Example: dhp1024_t a, b, c;
. a = dhp1024_add( b, c ); dhp_sub, dhp1024_subdhp_t dhp_sub( dhp_t src1, dhp_t src2 )dhp1024_t dhp1024_sub( dhp1024_t src1, dhp1024_t src2 ) return the item of an appropriate data type that has high precision floating point number with the value equal to src1 - src2. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF Example: dhp_t a, b, c;
. a = dhp_sub( b, c ); dhp_mul, dhp1024_muldhp_t dhp_mul( dhp_t src1, dhp_t src2 )dhp1024_t dhp1024_mul( dhp1024_t src1, dhp1024_t src2 ) return the item of an appropriate data type that has high precision floating point number with the value equal to src1 * src2. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF Example: dhp_t a, b, c;
. a = dhp_mul( b, c ); dhp_div, dhp1024_divdhp_t dhp_div( dhp_t src1, dhp_t src2 )dhp1024_t dhp1024_div( dhp1024_t src1, dhp1024_t src2 ) return the item of an appropriate data type that has high precision floating point number with the value equal to src1 / src2. Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF, DHP_ERR_DZE Example: dhp_t a, b, c;
. a = dhp_div( b, c ); dhp_sqrt, dhp1024_sqrtdhp_t dhp_sqrt( dhp_t src )dhp1024_t dhp1024_sqrt( dhp1024_t src ) return the item of an appropriate data type that has high precision floating point number with the value equal to SQRT( src ) Possible Exceptions: DHP_ERR_OVF, DHP_ERR_UNF, DHP_ERR_INV Example: dhp_t a, b;
. a = dhp_sqrt( b ); dhp_trunc, dhp1024_truncdhp_t dhp_trunc( dhp_t src )dhp_t dhp1024_trunc( dhp1024_t src ) return the item of an appropriate data type that has high precision floating point number with the value equal to the integer nearest to src and not larger in absolute value than src Possible Exceptions: none Example: dhp_t a, b;
. a = dhp_trunc( b ); dhp_round, dhp1024_rounddhp_t dhp_round( dhp_t src )dhp_t dhp1024_round( dhp1024_t src ) return the item of an appropriate data type that has high precision floating point number with the value equal to the integer nearest to src Possible Exceptions: none Example: dhp_t a, b;
. a = dhp_trunc( b ); dhp_fabs, dhp1024_fabsdhp_t dhp_fabs( dhp_t src )dhp1024_t dhp1024_fabs( dhp1024_t src ) return the item of an appropriate data type that has high precision floating point number with the value equal the absolute value of src Possible Exceptions: none Example: dhp_t a, b;
. a = dhp_fabs( b ); dhp_neg, dhp1024_negdhp_t dhp_neg( dhp_t src )dhp1024_t dhp1024_neg( dhp1024_t src ) return the item of an appropriate data type that has high precision floating point number with the value equal to -src Possible Exceptions: none Example: dhp_t a, b;
. a = dhp_neg( b ); dhp_is0, dhp1024_is0int dhp_is0( dhp_t src )int dhp1024_is0( dhp1024_t src ) return not-zero value if src is equal to 0, zero - if src has numeric representation of a not zero value. Possible Exceptions: none Example: dhp_t a;
. if ( !dhp_is0( a ) ) . dhp_cmp, dhp1024_cmpint dhp_cmp( dhp_t src1, dhp_t src2 )int dhp1024_cmp( dhp_t src1, dhp_t src2 ) return and integer less than, equal to, or greater than zero is src1 is, respectively, less than, equal, or greater than src2 Possible Exceptions: none Example: dhp_t a, b;
. if ( dhp_cmp( a, b ) < 0 ) ExamplesAssume that the task is to compute the first 100 items from the sequence of Fibonacci numbersF0 = 0, F1 = 1, Fn+2 = Fn+1 + Fn
using de Moivre's formula Fn = (
φn -
φ'n )/ √5
where φ = ( 1 + √5 )/2,
φ' = ( 1 - √5 )/2
The following program uses standard double precision variables to achieve this goal: double p, p1, pn, p1n, fn, sqrt5;
int i; sqrt5 = sqrt( 5. ); p = ( 1. + sqrt5 )/2.; p1 = ( 1. - sqrt5 )/2.; pn = p; p1n = p1; for ( i = 0; i < 100; i++ ) { fn = ( pn - p1n )/sqrt5;
} printf( "F%d = %f\n", i, fn ); pn *= p; p1n *= p1; using dhp in CIn C the above program can be rewritten to utilize dhp as following:#include <dhp.h>
. dhp_t p, p1, pn, p1n, fn, sqrt5; int i; char s[1024]; sqrt5 = dhp_sqrt( dhp_set_double( 5. ) ); p = dhp_div( dhp_add( dhp_set_double( 1. ), sqrt5 ), dhp_set_double( 2. ) ); p1 = dhp_div( dhp_add( dhp_set_double( 1. ), sqrt5 ), dhp_set_double( 2. ) ); pn = p; p1n = p1; for ( i = 0; i < 100; i++ ) { fn = dhp_div( dhp_sub( pn, p1n ), sqrt5 );
} dhp_get_string( fn, s, 1000, 6 ); printf( "F%d = %s\n", i, s ); pn = dhp_mul( pn, p ); p1n = dhp_mul( p1n, p1 ); This example shows that implementing dhp functionality in C requires significant modification of the original program. using dhp in C++The program that uses standard double precision variables ( listed above ) can be rewritten to utilize dhp as following:#include <dhp.h>
. dhpreal p, p1, pn, p1n, fn, sqrt5; int i; char s[1000]; fn = 5.; sqrt5 = sqrt( fn ); p = ( 1. + sqrt5 )/2.; p1 = ( 1. - sqrt5 )/2.; pn = p; p1n = p1; for ( i = 0; i < 100; i++ ) { fn = ( pn - p1n )/sqrt5;
} fn.get_string( s, 1000, 6 ); printf( "F%d = %s\n", i, s ); pn *= p; p1n *= p1; The changes from the original ( normal ) program are marked in this colour. This example shows that implementing dhp functionality in C++ is straightforward and requires very minor modification of the original program. Results of executionThe following table lists generated Fibonacci number for selected indexes:
|