Thread Closed

Hi-prec math for visual studio c#?

 
Share Thread Thread Tools
Jul23-08, 12:41 AM   #1
 

Hi-prec math for visual studio c#?


Is there a Hi-prec math for visual studio c#?

Thanks in advance,

Devin
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> Bird's playlist could signal mental strengths and weaknesses
>> Minus environment, patterns still emerge: Computational study tracks E. coli cells' regulatory mechanisms
>> Bacterium uses natural 'thermometer' to trigger diarrheal disease, scientists find
Jul23-08, 09:02 AM   #2
 
Recognitions:
Homework Helper Homework Help
Science Advisor Science Advisor
There's at least one commercial package out there, but it's not cheap:
http://www.extremeoptimization.com/

What are your needs? What does the library need to do, and how fast does it need to work? Are you just using big numbers, or are you doing matrices? Basic operations (+, -, *, /, %, ^), transcendental (sin, sinh, asin, exp, log, erf, gamma), or specialized (bessel, hypergeom, prime operations, etc.)? Integer or floating-point?
Jul23-08, 10:12 AM   #3
 
I'm doing floating point with basic math ops, ie: +,-,*,/,^, sqrt
Jul23-08, 10:27 AM   #4
 
Recognitions:
Homework Helper Homework Help
Science Advisor Science Advisor

Hi-prec math for visual studio c#?


Does it need to be fast? Is anything running in a tight loop, or are you just writing (say) a calculator where an extra millisecond won't hurt?
Jul23-08, 12:12 PM   #5
 
Doesn't need to be fast.
Jul23-08, 01:59 PM   #6
 
Recognitions:
Homework Helper Homework Help
Science Advisor Science Advisor
You could write your own, then, or have someone write it. Quick code:

Code:
class Bignum {
	protected ulong[] limb;
	public Bignum (long initialValue) {
		limb = new ulong[] {initialValue};
	}

	public Bignum (long[] initialValue) {
		Array.Copy(limb, initialValue, initialValue.Length);
	}

	public Bignum (string initialValue) {
		// Bad method, replace with a faster one if needed
		limb = new ulong[] {long.Parse(initialValue.Substring(0, 18))};
		for (int i = 18; i < initialValue.Length; ++i)
			limb = limb * 10 + long.Parse(initialValue[i]);
	}

	public Bignum (Bignum initialValue) {
		Array.Copy(limb, initialValue.limb, initialValue.limb.Length);
	}

	public static Bignum operator+ (Bignum a, Bignum b) {
		long carry = 0;
		int sz = Math.Max (a.limb.Length. b.limb.length);

		Bignum result = new Bignum(a);
		for (int i = 0; i < sz; ++i) {
			long o1 = result.limb[i];
			long o2 = b.limb[i];
			limb[i] += b.limb[i] + carry;
			if (result.limb[i] < o1 || result.limb[i] < o2)
				carry = 1;
			else
				carry = 0;
		}
		if (carry == 1) {
			Array.Copy(result.limb, result.limb, result.limb.Length + 1);
			result.limb[sz] = 1;
		}
		return result;
	}

	public static Bignum operator* (Bignum a, Bignum b) {
		// Code here
	}

	public static Bignum operator^ (Bignum a, uint b) {
		Bignum result = new Bignum(a);
		Bignum carry = new Bignum(0);
		while (b > 1) {
			if ((b&1) == 1)
				carry *= result;
			result *= result;
			b >>= 1;
		}
		return result * carry;
	}
}
I haven't compiled this (or even used an IDE) but this should be a good start. Subtraction is easy, multiplication not too hard, and division... well, code that as far as you need it. I decided to do the exponentiation so it wouldn't be too slow.
Jul25-08, 09:44 AM   #7
 
Cool, thanks for your time, effort & code. I like your usage of binary ops.

-Devin
Thread Closed
Thread Tools


Similar Threads for: Hi-prec math for visual studio c#?
Thread Forum Replies
C code in visual studio Programming & Comp Sci 7
stumped on Visual Studio / Visual Basic Programming & Comp Sci 5
What is the difference between Visual Studio .Net 2005 and Visual Studio 2005? Computing & Technology 4
Flaw in Crystal Reports Web Viewer affects Visual Studio, Outlook, and CRM Computing & Technology 0
Visual Studio Computing & Technology 13