Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
i am trying to create a calculated column. originally the column was constructed with a for loop and values were hard coded in. this WORKED for testing purposes, however changes needed to be made so that users could input different values.

the problem is that the calculation has a limit. there's a root and you can't have anything below zero in the root, so i have been trying to figure out a way to stop the loop before hitting that limit.

i tried running the do-while loop and keep getting outof memory exceptions and apparently i have something like 3+gb physical memory.

thoroughly unsure what to do, thank you for any help

here's my initial for loop:
C#
public DataTable sphCalcBCZone1(decimal b2, decimal b5, decimal b16)
{
    DataTable dt = new DataTable();
    DataColumn c = new DataColumn("Z1_BC");
    c.DataType = System.Type.GetType("System.Decimal");
    dt.Columns.Add(c);
    decimal stepPoint = 0M;
    double stepd = Convert.ToDouble(stepPoint);
    decimal gobbledegook = 8.6M;
    for (stepd = 0; stepPoint <= gobbledegook; stepPoint = stepPoint + .005M)
    {
        DataRow row = dt.NewRow();
        double stepd = Convert.ToDouble(stepPoint); double b2d = Convert.ToDouble(b2); double b5d = Convert.ToDouble(b5); double b16d = Convert.ToDouble(b16);
        double valC1 = Math.Pow(stepd, 2) / (b2d + Math.Sqrt(Math.Pow(b2d, 2) - b5d * Math.Pow(stepd, 2))) + b16d;
        decimal valC2 = Convert.ToDecimal(valC1);
        row["Z1_BC"] = valC2; dt.Rows.Add(row);
    }
    return dt;
}


here's my do-while loop in which i tried to put in the "limit" and i got the outofmemory exception
C#
public DataTable sphCalcBCZone1(decimal b2, decimal b5, decimal b16)
    {
        DataTable dt = new DataTable();
        DataColumn c = new DataColumn("Z1_BC");
        c.DataType = System.Type.GetType("System.Decimal");
        dt.Columns.Add(c);
        double b2d = Convert.ToDouble(b2); double b5d = Convert.ToDouble(b5); double b16d = Convert.ToDouble(b16);
        double limit = Math.Pow(b2d, 2) - b5d * Math.Pow(step, 2);
        do
        {
            DataRow row = dt.NewRow();
            double valC1 = Math.Pow(step, 2) / (b2d + Math.Sqrt(Math.Pow(b2d, 2) - b5d * Math.Pow(step, 2))) + b16d;
            decimal valC2 = Convert.ToDecimal(valC1);
            row["Z1_BC"] = valC2; dt.Rows.Add(row);

//forgot to add this line...now value is too large/small for decimal
step = step + .005;


        } while (limit > 0);
        return dt;
    }
Posted
Updated 13-Feb-14 7:50am
v2
Comments
thatraja 13-Feb-14 13:36pm    
how much rows? Include needed details
memberxxxxxxxxxxxxxxxxx 13-Feb-14 13:55pm    
the number of rows changes depending on the inputs from the user. that's why i tried to limit the function.

for this loop...
b2d = 8.6
b5d = 1
b16d = 0

i also added a new line in the code so the exception changed to:
"number too large or small for decimal.

so the first calculation should be...

0^2
_______ + 0
(8.6 + sqrt(8.6^2 - 1*0^2)

shouldn't that add up to 0?

also...thanks for the help :)

You're never changing limit so it'll loop infinitely
 
Share this answer
 
Look inside your Do/While loop. Do you see anything in the that modifies the variable you're using to control the loop, you called it "limit".

Currently, it will go forever and actually run the process out of memory adding rows to a DataTable.
 
Share this answer
 
thank you all.

here's the finished bit that runs

C#
DataTable dt = new DataTable();
    DataColumn c = new DataColumn("Z1_BC");
    c.DataType = System.Type.GetType("System.Decimal");
    dt.Columns.Add(c);
    double step = 0;
    double limit;
    double b2d = Convert.ToDouble(b2); double b5d = Convert.ToDouble(b5); double b16d = Convert.ToDouble(b16);
    do
    {

        limit = Math.Pow(b2d, 2) - b5d * Math.Pow(step, 2);
        DataRow row = dt.NewRow();
        double valC1 = Math.Pow(step, 2) / (b2d + Math.Sqrt(Math.Pow(b2d, 2) - b5d * Math.Pow(step, 2))) + b16d;
       // decimal valC2 = Convert.ToDecimal(valC1);
        row["Z1_BC"] = valC1; dt.Rows.Add(row);
        step = step + .005;
    } while (limit > .000000000001);
    return dt;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900