• Create Account

# Grain

Member Since 08 Apr 2004
Offline Last Active Mar 18 2015 12:19 PM

### Digits of Pi

19 November 2013 - 05:16 PM

I'm kind of OCD and irrational numbers really bother me. So how many digits of Pi would you need to calculate a circle the size of the known universe with the accuracy of the Planck length? That way, at least in a physical sense Pi does have a practical end and I can stop worrying about it.

### Large array allocation & out of memory error

29 September 2013 - 12:22 AM

I am having trouble with this prime number sieve running out of memory.

It fails at Values.Capacity = n; Previously Values was just an array defined like this : bool[] Values = new bool[n]; but had the same result. I am passing primesieve an int.MaxValue.
static int[] Primesieve(int n)
{
List<bool> Values = new List<bool>();
Values.Capacity = n;
int[] primes = new int[(int)Math.Sqrt(n)];
int primecount = 0;
Values[0] = true;
Values[1] = true;

for (int i = 0; i < Math.Sqrt(n); i++)
{
if (Values[i] == false)
{
for (int j = i * i; j < Values.Count; j += i)
Values[j] = true;
primes[primecount] = i;
primecount++;
}
}
return primes;
}
I believed my problem to be that since they are bools, a value type they are being created on the stack so I did this to force heap allocation. but it still fails at the same place.

public class Reference<T>
{
public T Ref;
}
static int[] Primesieve(int n)
{
List<Reference<bool>> Values = new List<Reference<bool>>();
Values.Capacity = n;
int[] primes = new int[(int)Math.Sqrt(n)];
int primecount = 0;
Values[0].Ref = true;
Values[1].Ref = true;
for (int i = 0; i < Math.Sqrt(n); i++)
{
if (Values[i].Ref == false)
{
for (int j = i * i; j < Values.Count; j += i)
Values[j].Ref = true;
primes[primecount] = i;
primecount++;
}
}
return primes;
}
My machine has 16gb of ram running win7 64-bit and i even targeted the build specifically to x64. So I know it cat actually be running out of memory as int.MaxValue Booleans should only take around 2gb assuming .Net is not packing 8 of them into 1 byte,  and if it is then it should only really take up 265mb.

Or am I some how still failing to use the heap?

### Check if a List<T>.Enumerator is valid

31 August 2013 - 03:19 PM

In C# how can I check if a List enumerator is valid before using it? Valid meaning it has been attached to a List already. Since enumerator is a struct it's not comparable to null so that option is out. The .Current property can be null even if the enumerator is valid, for example if MoveNext() has not yet been called for the first time or if the end of the list has been reached, so checking that isn't helpful in all cases either.

I can call MoveNext() in a try/catch block, which would tell me if it's valid or not, but if the the enumerator IS valid I don't wan't to move to the next item just yet. I would wan't to check if the .Current property if not null and then work with that item before calling MoveNext(). And I don't really like catching exceptions as part of preforming basic logic anyway, they should be only for error handling.

### Vector Projection with zero length

20 August 2013 - 03:32 AM

Currently I'm doing this

public static Vector2 Project(this Vector2 A, Vector2 B)
{
if (A.X != 0f || A.Y != 0)
return Vector2.Multiply(A, Vector2.Dot(A, B) / Vector2.Dot(A, A));
else
return A;
//return B; ???
}

Without the zero length check in there every thing blows up because this method returns an effectively infinite length vector.  What is the best thing to return when the length of A is zero?