Hi-prec math for visual studio c#?

  • Context: C# 
  • Thread starter Thread starter devronious
  • Start date Start date
  • Tags Tags
    Visual
Click For Summary

Discussion Overview

The discussion revolves around the need for high-precision mathematical operations in Visual Studio C#. Participants explore available libraries, specific requirements for mathematical operations, and the possibility of implementing custom solutions.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Devin inquires about high-precision math libraries for Visual Studio C#.
  • One participant mentions a commercial package but asks about specific needs regarding operations and performance.
  • Devin specifies the requirement for floating-point operations with basic math operations such as addition, subtraction, multiplication, division, exponentiation, and square root.
  • Another participant questions the necessity of speed, suggesting that if performance is not critical, a custom solution could be feasible.
  • Devin confirms that speed is not a concern.
  • A participant provides a code snippet for a custom high-precision number class, discussing its structure and potential limitations without claiming it is complete or error-free.
  • Devin expresses appreciation for the provided code and notes the use of binary operations.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a specific library or solution, as the discussion includes various suggestions and considerations without definitive conclusions.

Contextual Notes

The provided code snippet is not compiled or tested, and participants acknowledge that certain operations (like multiplication and division) are not fully implemented, leaving room for further development and refinement.

devronious
Messages
7
Reaction score
0
Is there a Hi-prec math for visual studio c#?

Thanks in advance,

Devin
 
Technology news on Phys.org
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?
 
I'm doing floating point with basic math ops, ie: +,-,*,/,^, sqrt
 
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?
 
Doesn't need to be fast.
 
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.
 
Cool, thanks for your time, effort & code. I like your usage of binary ops.

-Devin
 

Similar threads

  • · Replies 0 ·
Replies
0
Views
2K
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 25 ·
Replies
25
Views
15K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K