Results 1 to 4 of 4

Thread: C++ 128-BIT Variable

  1. #1
    Junior Member
    Join Date
    Apr 2006
    Posts
    20

    C++ 128-BIT Variable

    I have two machines that I am writing a program on. One is RHEL 3.0 U7 and the other is XP SP2. My XP SP2 machine has Visual C++ 6.0.

    What I am trying to do is write a program that can handle 128-bit numbers. I am able to use the "long long" variable in Linux, but my XP SP2 machine can only handle "long" or "long double".

    Is there a variable type that is for 128-bit numbers? Is there a class or header type that I need for this?

  2. #2
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    Have a look at either MAPM[1] or gmp[2]. I used both of them extensively.
    While MAPM is easier to handle, gmp is faster (in particular the calculation
    of the logarithm).

    Both libraries are capable to handle arbitrary precision floats (not just 64
    or 80bits) and integers of arbitrary length.

    Cheers.



    [1] http://www.tc.umn.edu/~ringx004/mapm-main.html
    [2] http://www.swox.com/gmp/
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  3. #3
    Junior Member
    Join Date
    Apr 2006
    Posts
    20
    Thanks! I have looked into each one. GMP does sound like the way I want to go.

    However, there is one problem. I am not a c++ guru by any means. How would I go about using either one of these libraries? Is it simply a matter of adding a header file and declare my "MAPM" of "GMP" variable?

    I would definitely appreciate any help.

  4. #4
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    Is it simply a matter of adding a header file and declare my "MAPM" of "GMP" variable?
    It indeed is almost as easy as this:

    You have to make sure that you link the gmp.lib / mapm.lib (libgmp.a / libmapm.a)
    libraries to your project:
    Visual C++ 6.0: Project --> Settings --> Link --> Object/library modules
    gcc: -lgmp -lmapm


    Here are two working examples:

    gmp:
    Code:
    #include "gmp.h"
    
    // C++ with operator overloading: gmpxx.h[1]
    // log, exp, ...		: mpfr.h[2]
    
    void main(){
    	mpf_t myMpf_t1,myMpf_t2,myMpf_res;
    
    	mpf_set_default_prec(512);	// minimal precision (in bits)
    
    
        mpf_init(myMpf_t1);
        mpf_init(myMpf_t2);
        mpf_init(myMpf_res);
    
    	mpf_set_d(myMpf_t1,1.2345e260);
    	mpf_set_d(myMpf_t2,2.3456e300);
    
    
    	mpf_mul(myMpf_res,myMpf_t1,myMpf_t2);
    
    
    	printf("Result exceeds double range: %e\n",mpf_get_d(myMpf_res));
    	gmp_printf("mpf_t: %.*Fe\n",8,myMpf_res);	// 8 digits
    
        mpf_clear(myMpf_t1);
        mpf_clear(myMpf_t2);
        mpf_clear(myMpf_res);
    
    }
    MAPM:
    Code:
    #include "M_APM.h"
    
    void main(){
    	MAPM myMapm_t1,myMapm_t2,myMapm_res;
    	char buf[1024];
    
    
    	m_apm_cpp_precision(64);		// minimal precision (in digits!)
    	
    
    
    	myMapm_t1=1.2345e260;
    	myMapm_t2=2.3456e300;
    
    	myMapm_res=myMapm_t1*myMapm_t2;
    
    
    
    		myMapm_res.toString(buf,8);
    	printf("Result exceeds double range: %e\n",atof(buf));
    	printf("MAPM: %s\n",buf);	
    
    
    
    }
    MAPM is easier to use, gmp is faster. However, the C++-wrapper of gmp
    adds to gmp the nice MAPM-like behaviour.

    Cheers


    [1] http://www.swox.com/gmp/#DOC
    [2] http://www.mpfr.org/
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •