arbitrary precision support for btScalar?

kkimlabs
Posts: 3
Joined: Sun Nov 13, 2011 5:23 am

arbitrary precision support for btScalar?

Post by kkimlabs »

I noticed that btScalar can be tweaked as float or double easily.

Actual definition:
http://www.google.com/codesearch#IgfHeG ... e=cs&l=199

but then can it be a 3rd party floating point library? for example,

GNU GMP's mpf_class

example usage:

Code: Select all

#include<gmpxx.h>
#include<iostream>
#include<iomanip>


int main() 
{
	long prec = 1000;
	mpf_set_default_prec(prec);
	
	mpf_class a(1);
	mpf_class b(mpf_class(1)/sqrt(mpf_class(2)));
	mpf_class t(mpf_class(1)/mpf_class(4));
	mpf_class p(1);
	mpf_class x,y,pi;
	
	while ( a - b > mpf_class(1e-1000))
	{
		x = (a + b)/2;
		y = sqrt(a*b);
		t = t - p*(a-x)*(a-x);
		a = x;
		b = y;
		p *=2;
	}
	
	pi =  (a+b)*(a+b)/(mpf_class(4)*t);
	
	std::cout << std::setprecision(80) << pi << std::endl;
	
	return 0;
}
Then, because we have the perfect control of underlying floating point system, we can make every floating point operations bit-wise identical across any platforms. It will benefit network games a lot.

I wonder
1. if it is possible,
2. if not, what is the practical issues?
Last edited by kkimlabs on Sun Nov 13, 2011 5:50 am, edited 1 time in total.
kkimlabs
Posts: 3
Joined: Sun Nov 13, 2011 5:23 am

Re: arbitrary precision support for btScalar?

Post by kkimlabs »

Just tried compiling, with modified btScalar.h

Code: Select all

...
#include <gmpxx.h>
...
///The btScalar type abstracts floating point numbers, to easily switch between double and single floating point precision.
#if defined(BT_USE_DOUBLE_PRECISION)
typedef mpf_class btScalar;
//this number could be bigger in double precision
#define BT_LARGE_FLOAT 1e30
#else
typedef mpf_class btScalar;
//keep BT_LARGE_FLOAT*BT_LARGE_FLOAT < FLT_MAX
#define BT_LARGE_FLOAT 1e18f
#endif
...

Code: Select all

[kkb110@kkim bullet-build]$ make clean
[kkb110@kkim bullet-build]$ make
[  1%] Building CXX object src/LinearMath/CMakeFiles/LinearMath.dir/btAlignedAllocator.o
In file included from /tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btAlignedAllocator.h:23:0,                                                                      
                 from /tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btAlignedAllocator.cpp:16:
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h: In function ‘btScalar btSqrt(btScalar)’:
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h:262:16: error: cannot convert ‘btScalar {aka __gmp_expr<__mpf_struct [1], __mpf_struct [1]>}’ to ‘float’ for argument ‘1’ to ‘float sqrtf(float)’
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h: In function ‘btScalar btFabs(btScalar)’:
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h:265:52: error: cannot convert ‘btScalar {aka __gmp_expr<__mpf_struct [1], __mpf_struct [1]>}’ to ‘float’ for argument ‘1’ to ‘float fabsf(float)’
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h: In function ‘btScalar btCos(btScalar)’:
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h:266:50: error: cannot convert ‘btScalar {aka __gmp_expr<__mpf_struct [1], __mpf_struct [1]>}’ to ‘float’ for argument ‘1’ to ‘float cosf(float)’
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h: In function ‘btScalar btSin(btScalar)’:
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h:267:50: error: cannot convert ‘btScalar {aka __gmp_expr<__mpf_struct [1], __mpf_struct [1]>}’ to ‘float’ for argument ‘1’ to ‘float sinf(float)’
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h: In function ‘btScalar btTan(btScalar)’:
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h:268:50: error: cannot convert ‘btScalar {aka __gmp_expr<__mpf_struct [1], __mpf_struct [1]>}’ to ‘float’ for argument ‘1’ to ‘float tanf(float)’
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h: In function ‘btScalar btAcos(btScalar)’:
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h:274:16: error: cannot convert ‘btScalar {aka __gmp_expr<__mpf_struct [1], __mpf_struct [1]>}’ to ‘float’ for argument ‘1’ to ‘float acosf(float)’
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h: In function ‘btScalar btAsin(btScalar)’:
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h:281:16: error: cannot convert ‘btScalar {aka __gmp_expr<__mpf_struct [1], __mpf_struct [1]>}’ to ‘float’ for argument ‘1’ to ‘float asinf(float)’
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h: In function ‘btScalar btAtan(btScalar)’:
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h:283:52: error: cannot convert ‘btScalar {aka __gmp_expr<__mpf_struct [1], __mpf_struct [1]>}’ to ‘float’ for argument ‘1’ to ‘float atanf(float)’
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h: In function ‘btScalar btAtan2(btScalar, btScalar)’:
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h:284:69: error: cannot convert ‘btScalar {aka __gmp_expr<__mpf_struct [1], __mpf_struct [1]>}’ to ‘float’ for argument ‘1’ to ‘float atan2f(float, float)’
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h: In function ‘btScalar btExp(btScalar)’:
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h:285:50: error: cannot convert ‘btScalar {aka __gmp_expr<__mpf_struct [1], __mpf_struct [1]>}’ to ‘float’ for argument ‘1’ to ‘float expf(float)’
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h: In function ‘btScalar btLog(btScalar)’:
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h:286:50: error: cannot convert ‘btScalar {aka __gmp_expr<__mpf_struct [1], __mpf_struct [1]>}’ to ‘float’ for argument ‘1’ to ‘float logf(float)’
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h: In function ‘btScalar btPow(btScalar, btScalar)’:
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h:287:63: error: cannot convert ‘btScalar {aka __gmp_expr<__mpf_struct [1], __mpf_struct [1]>}’ to ‘float’ for argument ‘1’ to ‘float powf(float, float)’
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h: In function ‘btScalar btFmod(btScalar, btScalar)’:
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h:288:65: error: cannot convert ‘btScalar {aka __gmp_expr<__mpf_struct [1], __mpf_struct [1]>}’ to ‘float’ for argument ‘1’ to ‘float fmodf(float, float)’
In file included from /tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btAlignedAllocator.h:23:0,
                 from /tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btAlignedAllocator.cpp:16:
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h: In function ‘btScalar btAtan2Fast(btScalar, btScalar)’:
/tmp/yaourt-tmp-kkb110/aur-bullet-svn/src/bullet-build/src/LinearMath/btScalar.h:323:31: error: operands to ?: have different types ‘__gmp_expr<__mpf_struct [1], __gmp_unary_expr<__gmp_expr<__mpf_struct [1], __mpf_struct [1]>, __gmp_unary_minus> >’ and ‘btScalar {aka __gmp_expr<__mpf_struct [1], __mpf_struct [1]>}’
make[2]: *** [src/LinearMath/CMakeFiles/LinearMath.dir/btAlignedAllocator.o] Error 1
make[1]: *** [src/LinearMath/CMakeFiles/LinearMath.dir/all] Error 2
make: *** [all] Error 2
[kkb110@kkim bullet-build]$ 

Vino
Posts: 3
Joined: Fri Nov 11, 2011 10:35 am

Re: arbitrary precision support for btScalar?

Post by Vino »

Looks like you'll have to either provide alternate definitions for those functions, or provide conversion methods, perhaps with an operator overload.