Refactor multiple goto statements from the loop body - c#
I am struggling to replicate the following fortran 77 subroutine into C# method. The main problem is that I am trying to get rid of the fortran's goto statements.
The fortran to C# conversion looks fine, still the goto statements remained. Is there a way all of them can be replaced with conditionals or some other ways? Thank you.
Here is the fortran subroutine:
C PROGRAM TEMP
subroutine TEMP (acl,adu,aeff,c,cair,cb,cbare,
+ cclo,count1,csum,di,ed,emcl,emsk,enbal,
+ enbal2,ere,erel,esw,eswdif,eswphy,eswpot,
+ evap,facl,fcl,fec,feff,food,h,hc,he,ht,htcl,icl,j,
+ mbody,p,po,r1,r2,rbare,rcl,
+ rclo,rclo2,rdcl,rdsk,rob,rsum,sex,sigm,sw,swf,swm,
+ ta,tbody,tcl,tcore,tmrt,tsk,v,vb,vb1,vb2,
+ vpa,vpts,wetsk,wd,wr,ws,wsum,xx)
real acl,adu,aeff,c(0:10),cair,cb,cbare,
+ cclo,csum,di,ed,emcl,emsk,enbal,
+ enbal2,ere,erel,esw,eswdif,eswphy,eswpot,
+ evap,facl,fcl,fec,feff,food,h,hc,he,ht,htcl,icl,
+ mbody,p,po,r1,r2,rbare,rcl,
+ rclo,rclo2,rdcl,rdsk,rob,rsum,sigm,sw,swf,swm,
+ ta,tbody,tcl,tcore(1:7),tmrt,tsk,v,vb,vb1,vb2,
+ vpa,vpts,wetsk,wd,wr,ws,wsum,xx
integer count1,count3,j,sex
wetsk = 0.
adu = 0.203 * mbody ** 0.425 * ht ** 0.725
hc = 2.67 + ( 6.5 * v ** 0.67)
hc = hc * (p /po) ** 0.55
feff = 0.725
C
facl = (- 2.36 + 173.51 * icl - 100.76 * icl * icl + 19.28
+ * (icl ** 3.)) / 100.
C
if (facl .gt.1.) facl = 1.
rcl = (icl/6.45)/facl
if (icl.ge.2.) y = 1.
if ((icl .gt. 0.6) .and. (icl .lt. 2.)) y = (ht - 0.2) / ht
if ((icl .le. 0.6) .and. (icl .gt. 0.3)) y = 0.5
if ((icl .le. 0.3) .and. (icl .gt. 0.)) y = 0.1
r2 = adu * (fcl - 1. + facl) / (2. * 3.14 * ht * y)
r1 = facl * adu / (2. * 3.14 * ht * y)
di = r2 - r1
C TEMPERATURE
do 90 j = 1,7
tsk = 34.
count1 = 0
tcl = (ta + tmrt + tsk) / 3.
count3 = 1
enbal2 = 0.
20 acl = adu * facl + adu * (fcl - 1.)
rclo2 = emcl*sigm *((tcl+273.2)** 4.-(tmrt+273.2)** 4.)*feff
htcl = 6.28 * ht * y * di / (rcl * alog(r2/r1) * acl)
tsk = 1. / htcl * (hc * (tcl - ta) + rclo2) + tcl
C RADIATION
aeff = adu * feff
rbare = aeff * (1.-facl) * emsk * sigm *
+ ((tmrt + 273.2) ** 4. - (tsk + 273.2) ** 4.)
rclo = feff * acl * emcl * sigm *
+ ((tmrt + 273.2) ** 4. - (tcl + 273.2) ** 4.)
rsum = rbare + rclo
C CONVECT
cbare = hc * (ta - tsk) * adu * (1. - facl)
cclo = hc * (ta - tcl ) * acl
csum = cbare + cclo
C CORE
c(0) = h + ere
c(1) = adu * rob * cb
c(2) = 18. - 0.5 * tsk
c(3) = 5.28 * adu * c(2)
c(4) = 0.0208 * c(1)
c(5) = 0.76075 * c(1)
c(6) = c(3) - c(5) - tsk * c(4)
c(7) = - c(0) * c(2) - tsk * c(3) + tsk * c(5)
c(8) = c(6) * c(6) - 4. * c(4) * c(7)
c(9) = 5.28 * adu - c(5) - c(4) * tsk
c(10) = c(9) * c(9) - 4. * c(4) *
+ (c(5) * tsk - c(0) - 5.28 * adu * tsk)
C
if (tsk.eq.36.) tsk=36.01
tcore(7) = c(0) / (5.28 * adu + c(1) * 6.3 / 3600.) + tsk
tcore(3) = c(0) / (5.28 * adu + (c(1) * 6.3 / 3600.) /
+ (1 + 0.5 * (34. -tsk))) + tsk
if (c(10) .lt. 0.) goto 22
tcore(6) = (- c(9) - c(10) ** 0.5) / (2. * c(4))
tcore(1) = (- c(9) + c(10) ** 0.5) / (2. * c(4))
22 if (c(8) .lt. 0.) goto 24
tcore(2) = (- c(6) + abs(c(8)) ** 0.5) / (2. * c(4))
tcore(5) = (- c(6) - abs(c(8)) ** 0.5) / (2. * c(4))
24 tcore(4) = c(0) / (5.28 * adu + c(1) * 1. / 40.) + tsk
C TRANSPARENCE
tbody = 0.1 * tsk + 0.9 * tcore (j)
swm = 304.94 * (tbody - 36.6) * adu / 3600000.
vpts = 6.11 * 10. ** (7.45 * tsk / (235. + tsk))
if (tbody .le. 36.6) swm = 0.
swf = 0.7 * swm
if(sex .eq. 1) sw = swm
if(sex .eq. 2) sw = swf
eswphy = - sw * evap
he = 0.633 * hc / (p * cair)
fec = 1. / (1. + 0.92 * hc * rcl)
eswpot = he * (vpa - vpts) * adu * evap * fec
wetsk = eswphy / eswpot
if (wetsk .gt. 1.) wetsk = 1.
eswdif = eswphy - eswpot
if (eswdif .le. 0.) esw = eswpot
if (eswdif .gt. 0.) esw = eswphy
if (esw .gt. 0.) esw = 0.
C DIFFERENCE
rdsk = 0.79 * 10. ** 7.
rdcl = 0.
ed = evap / (rdsk + rdcl) * adu * (1 - wetsk) * (vpa-vpts)
C VB
vb1 = 34. - tsk
vb2 = tcore(j) - 36.6
if (vb2 .lt.0.) vb2 = 0.
if (vb1 .lt.0.) vb1 = 0.
vb = (6.3 + 75. * (vb2)) / (1. + 0.5 * vb1)
C BALANCE
enbal = h + ed + ere + esw + csum + rsum + food
C COVER
if (count1 .eq.0) xx = 1.
if (count1 .eq.1) xx = 0.1
if (count1 .eq.2) xx = 0.01
if (count1 .eq.3) xx = 0.001
if (enbal .gt. 0.) tcl = tcl + xx
if (enbal .lt. 0.) tcl = tcl - xx
if ((enbal .le. 0.) .and. (enbal2 .gt. 0.)) goto 30
if ((enbal .ge. 0.) .and. (enbal2 .lt. 0.)) goto 30
enbal2 = enbal
count3 = count3 + 1
C
if (count3 .gt. 200) goto 30
goto 20
30 if ((count1 .eq.0.).or.(count1.eq.1.).or.(count1.eq.2.)) then
count1 = count1 + 1.
enbal2 = 0.
goto 20
end if
C
if (count1 .eq. 3.) then
C
if ((j .eq. 2) .or. (j .eq. 5)) goto 40
if ((j .eq. 6) .or. (j .eq. 1)) goto 50
if (j .eq. 3) goto 60
if (j .eq. 7) goto 70
if (j .eq. 4) goto 80
end if
40 if (c(8) .lt. 0.) goto 90
if ((tcore(j) .ge. 36.6) .and. (tsk .le. 34.050)) goto 80
goto 90
50 if (c(10) .lt. 0. ) goto 90
if ((tcore(j) .ge. 36.6) .and. (tsk .gt. 33.850)) goto 80
goto 90
60 if ((tcore(j) .lt. 36.6) .and. (tsk .le. 34.000)) goto 80
goto 90
70 if ((tcore(j) .lt. 36.6) .and. (tsk .gt. 34.000)) goto 80
goto 90
80 if ((j .ne. 4) .and. (vb .ge. 91.)) goto 90
if ((j. eq. 4) .and. (vb .lt. 89.)) goto 90
if (vb .gt. 90.) vb = 90.
C LOSSES
ws = sw * 3600. * 1000.
if (ws .gt.2000.) ws = 2000.
wd = ed / evap * 3600. * (-1000.)
wr = erel / evap * 3600. * (-1000.)
wsum = ws + wr + wd
goto 100
90 continue
100 return
end
And here is the C# attempt to replicate the upper fortran subroutine:
using System;
public static class GlobalMembers_TEMP
{
//C PROGRAM TEMP
// --------------------------------------------
public static void TEMP(acl, adu, aeff, c, cair, cb, cbare, cclo, count1, csum, di, ed , emcl , emsk , enbal , enbal2 , ere , erel , esw , eswdif , eswphy , eswpot , evap , facl , fcl , fec , feff , food , h , hc , he , ht , htcl , icl , j , mbody , p , po , r1 , r2 , rbare , rcl , rclo , rclo2 , rdcl , rdsk , rob , rsum , sex , sigm , sw , swf , swm , ta , tbody , tcl, tcore , tmrt , tsk , v , vb , vb1 , vb2 , vpa , vpts , wetsk , wd , wr , ws , wsum , xx)
{
float acl;
float adu;
float aeff;
float[] c = new float[11]; // ???
float cair;
float cb;
float cbare;
float cclo;
float csum;
float di;
float ed;
float emcl;
float emsk;
float enbal;
float enbal2;
float ere;
float erel;
float esw;
float eswdif;
float eswphy;
float eswpot;
float evap;
float facl;
float fcl;
float fec;
float feff;
float food;
float h;
float hc;
float he;
float ht;
float htcl;
float icl;
float mbody;
float p;
float po;
float r1;
float r2;
float rbare;
float rcl;
float rclo;
float rclo2;
float rdcl;
float rdsk;
float rob;
float rsum;
float sigm;
float sw;
float swf;
float swm;
float ta;
float tbody;
float tcl;
float[] tcore = new float[7]; // ???
float tmrt;
float tsk;
float v;
float vb;
float vb1;
float vb2;
float vpa;
float vpts;
float wetsk;
float wd;
float wr;
float ws;
float wsum;
float xx;
int count1;
int count3;
int j;
int sex;
wetsk = 0.0;
adu = 0.203 * mbody * *0.425 * ht * *0.725;
hc = 2.67 + (6.5 * v * *0.67);
hc = hc * (p / po) * *0.55;
feff = 0.725;
//C rcl = icl / 6.45
facl = (-2.36 + 173.51 * icl - 100.76 * icl * icl + 19.28 * (icl * *3.0)) / 100.0;
//C
if (facl > 1.0)
{
facl = 1.0;
}
rcl = (icl / 6.45) / facl;
if (icl >= 2.0)
{
y = 1.0;
}
if ((icl > 0.6)) && ((icl < 2.0))
{
y = (ht - 0.2) / ht;
}
if ((icl <= 0.6)) && ((icl > 0.3))
{
y = 0.5;
}
if ((icl <= 0.3)) && ((icl > 0.0))
{
y = 0.1;
}
r2 = adu * (fcl - 1.0 + facl) / (2.0 * 3.14 * ht * y);
r1 = facl * adu / (2.0 * 3.14 * ht * y);
di = r2 - r1;
//C TEMPERATURE
for (90 j = 1, 7)
{
tsk = 34.0;
count1 = 0;
tcl = (ta + tmrt + tsk) / 3.0;
count3 = 1;
enbal2 = 0.0;
g20:
acl = adu * facl + adu * (fcl - 1.0);
rclo2 = emcl * sigm * ((tcl + 273.2) * *4.0 - (tmrt + 273.2) * *4.0) * feff;
htcl = 6.28 * ht * y * di / (rcl * alog(r2 / r1) * acl);
tsk = 1.0 / htcl * (hc * (tcl - ta) + rclo2) + tcl;
//C RADIATION
aeff = adu * feff;
rbare = aeff * (1.0 - facl) * emsk * sigm * ((tmrt + 273.2) * *4.0 - (tsk + 273.2) * *4.0);
rclo = feff * acl * emcl * sigm * ((tmrt + 273.2) * *4.0 - (tcl + 273.2) * *4.0);
rsum = rbare + rclo;
//C CONVECT
cbare = hc * (ta - tsk) * adu * (1.0 - facl);
cclo = hc * (ta - tcl) * acl;
csum = cbare + cclo;
//C CORE
c[0] = h + ere;
c[1] = adu * rob * cb;
c[2] = 18.0 - 0.5 * tsk;
c[3] = 5.28 * adu * c[2];
c[4] = 0.0208 * c[1];
c[5] = 0.76075 * c[1];
c[6] = c[3] - c[5] - tsk * c[4];
c[7] = -c[0] * c[2] - tsk * c[3] + tsk * c[5];
c[8] = c[6] * c[6] - 4.0 * c[4] * c[7];
c[9] = 5.28 * adu - c[5] - c[4] * tsk;
c[10] = c[9] * c[9] - 4.0 * c[4] * (c[5] * tsk - c[0] - 5.28 * adu * tsk);
//C
if (tsk == 36.0)
{
tsk = 36.01;
}
tcore[7] = c[0] / (5.28 * adu + c[1] * 6.3 / 3600.0) + tsk;
tcore[3] = c[0] / (5.28 * adu + (c[1] * 6.3 / 3600.0) / (1 + 0.5 * (34.0 - tsk))) + tsk;
if (c[10] < 0.0)
{
goto g22;
}
tcore[6] = (-c[9] - c[10] * *0.5) / (2.0 * c[4]);
tcore[1] = (-c[9] + c[10] * *0.5) / (2.0 * c[4]);
g22:
if (c[8] < 0.0)
{
goto g24;
}
tcore[2] = (-c[6] + Math.Abs(c[8]) * *0.5) / (2.0 * c[4]);
tcore[5] = (-c[6] - Math.Abs(c[8]) * *0.5) / (2.0 * c[4]);
g24:
tcore[4] = c[0] / (5.28 * adu + c[1] * 1.0 / 40.0) + tsk;
//C TRANSPARENCE
tbody = 0.1 * tsk + 0.9 * tcore[j];
swm = 304.94 * (tbody - 36.6) * adu / 3600000.0;
vpts = 6.11 * 10.0 * *(7.45 * tsk / (235.0 + tsk));
if (tbody <= 36.6)
{
swm = 0.0;
}
swf = 0.7 * swm;
if (sex == 1)
{
sw = swm;
}
if (sex == 2)
{
sw = swf;
}
eswphy = -sw * evap;
he = 0.633 * hc / (p * cair);
fec = 1.0 / (1.0 + 0.92 * hc * rcl);
eswpot = he * (vpa - vpts) * adu * evap * fec;
wetsk = eswphy / eswpot;
if (wetsk > 1.0)
{
wetsk = 1.0;
}
eswdif = eswphy - eswpot;
if (eswdif <= 0.0)
{
esw = eswpot;
}
if (eswdif > 0.0)
{
esw = eswphy;
}
if (esw > 0.0)
{
esw = 0.0;
}
//C DIFFERENCE
rdsk = 0.79 * 10.0 * *7.0;
rdcl = 0.0;
ed = evap / (rdsk + rdcl) * adu * (1 - wetsk) * (vpa - vpts);
//C VB
vb1 = 34.0 - tsk;
vb2 = tcore[j] - 36.6;
if (vb2 < 0.0)
{
vb2 = 0.0;
}
if (vb1 < 0.0)
{
vb1 = 0.0;
}
vb = (6.3 + 75.0 * (vb2)) / (1.0 + 0.5 * vb1);
//C BALANCE
enbal = h + ed + ere + esw + csum + rsum + food;
//C COVER
if (count1 == 0)
{
xx = 1.0;
}
if (count1 == 1)
{
xx = 0.1;
}
if (count1 == 2)
{
xx = 0.01;
}
if (count1 == 3)
{
xx = 0.001;
}
if (enbal > 0.0)
{
tcl = tcl + xx;
}
if (enbal < 0.0)
{
tcl = tcl - xx;
}
if ((enbal <= 0.0)) && ((enbal2 > 0.0))
{
goto g30;
}
if ((enbal >= 0.0)) && ((enbal2 < 0.0))
{
goto g30;
}
enbal2 = enbal;
count3 = count3 + 1;
//C
if (count3 > 200)
{
goto g30;
}
goto g20;
g30:
if ((count1 == 0.0))
{
|| ((count1 == 1.0)) || ((count1 == 2.0))
{
count1 = count1 + 1.0;
enbal2 = 0.0;
goto g20;
}
}
//C
if (count1 == 3.0)
{
//C
if ((j == 2))
{
|| ((j == 5)) goto g40;
}
if ((j == 6))
{
|| ((j == 1)) goto g50;
}
if (j == 3)
{
goto g60;
}
if (j == 7)
{
goto g70;
}
if (j == 4)
{
goto g80;
}
}
g40:
if (c[8] < 0.0)
{
goto g90;
}
if ((tcore[j] >= 36.6)) && ((tsk <= 34.050))
{
goto g80;
}
goto g90;
g50:
if (c[10] < 0.0)
{
goto g90;
}
if ((tcore[j] >= 36.6)) && ((tsk > 33.850))
{
goto g80;
}
goto g90;
g60:
if ((tcore[j] < 36.6)) && ((tsk <= 34.000))
{
goto g80;
}
goto g90;
g70:
if ((tcore[j] < 36.6)) && ((tsk > 34.000))
{
goto g80;
}
goto g90;
g80:
if ((j != 4)) && ((vb >= 91.0))
{
goto g90;
}
if ((j.eq.4)) && ((vb < 89.0))
{
goto g90;
}
if (vb > 90.0)
{
vb = 90.0;
}
//C LOSSES
ws = sw * 3600.0 * 1000.0;
if (ws > 2000.0)
{
ws = 2000.0;
}
wd = ed / evap * 3600.0 * (-1000.0);
wr = erel / evap * 3600.0 * (-1000.0);
wsum = ws + wr + wd;
goto g100;
}
g100:
return;
}
}
You can try to turn this code into a state machine where each state represents a single goto inside the loop.
For that you will need to turn your method into a class, that contains all the parameters and variables as class fields, and has one constructor that initializes appropriate fields and (optionally) runs the "method".
I won't transition your method because it is far too huge as an example, but I will give you the following:
Original not converted method:
public static void Do(Int32 param1, Int32 param2)
{
Int32 loopI;
Int32 outBefore,
outAfter,
outGoto1 = 0,
outGoto2 = 0;
Int32[] outValues = new Int32[7];
Console.WriteLine("METHOD:");
Console.WriteLine("DoBeforeLoop");
outBefore = param1 + param2;
Boolean wasInGoto2 = false;
for (loopI = 0; loopI < 7; loopI++)
{
Console.WriteLine("Iteration {0}", loopI);
Console.WriteLine("FortranLoopMethodState.LoopCycleStart");
outValues[loopI] = loopI;
goto1:
Console.WriteLine("FortranLoopMethodState.Goto1");
outGoto1 = loopI + param1;
if (wasInGoto2)
{
wasInGoto2 = false;
goto end;
}
wasInGoto2 = true;
Console.WriteLine("FortranLoopMethodState.Goto2");
outGoto2 = loopI + param2;
goto goto1;
end:
DoNothing(); // We don't use break, do we?
}
Console.WriteLine("DoAfterLoop");
outAfter = param1 - param2;
Console.WriteLine(outGoto1);
Console.WriteLine(outGoto2);
}
Converted method:
public class ExecuteFortranMethod
{
private enum FortranLoopMethodState
{
LoopCycleStart,
Goto1,
Goto2,
LoopCycleEnded
}
#region Params
private Int32 param1;
private Int32 param2;
#endregion
#region Variables
public Int32 loopI;
public Int32 outBefore,
outAfter,
outGoto1,
outGoto2;
public Int32[] outValues = new Int32[7];
#endregion
#region Constructors
public ExecuteFortranMethod(Int32 param1, Int32 param2)
{
this.param1 = param1;
this.param2 = param2;
this.Invoke();
}
#endregion
#region Methods
private void Invoke()
{
Console.WriteLine("STATE MACHINE CLASS:");
this.DoBeforeLoop();
for (this.loopI = 0; this.loopI < 7; this.loopI++)
{
Console.WriteLine("Iteration {0}", this.loopI);
var state = FortranLoopMethodState.LoopCycleStart;
do
{
state = DoLoop(state);
}
while (state != FortranLoopMethodState.LoopCycleEnded);
}
this.DoAfterLoop();
}
#endregion
#region "Method" body methods
private void DoBeforeLoop()
{
Console.WriteLine("DoBeforeLoop");
this.outBefore = this.param1 + this.param2;
}
private void DoAfterLoop()
{
Console.WriteLine("DoAfterLoop");
this.outAfter = this.param1 - this.param2;
}
Boolean wasInGoto2;
private FortranLoopMethodState DoLoop(FortranLoopMethodState state)
{
switch (state)
{
case FortranLoopMethodState.LoopCycleStart:
{
Console.WriteLine("FortranLoopMethodState.LoopCycleStart");
this.outValues[this.loopI] = this.loopI;
return FortranLoopMethodState.Goto1;
}
case FortranLoopMethodState.Goto1:
{
Console.WriteLine("FortranLoopMethodState.Goto1");
this.outGoto1 = this.loopI + this.param1;
if (this.wasInGoto2)
{
this.wasInGoto2 = false;
return FortranLoopMethodState.LoopCycleEnded;
}
return FortranLoopMethodState.Goto2;
}
case FortranLoopMethodState.Goto2:
{
this.wasInGoto2 = true;
Console.WriteLine("FortranLoopMethodState.Goto2");
this.outGoto2 = this.loopI + this.param2;
return FortranLoopMethodState.Goto1;
}
default:
throw new InvalidOperationException("The state is invalid");
}
}
#endregion
}
Program.cs
Do(10, 20);
Console.WriteLine();
var result = new ExecuteFortranMethod(10, 20);
Console.WriteLine(result.outGoto1);
Console.WriteLine(result.outGoto2);
Console.WriteLine("Press any key");
Console.ReadKey(true);
As you can see the outputs are equivalent. I won't describe exactly how you can transfer the actual method, because it is a tedious (but not unreal) task with no real shortcuts and most of the ideas will be clear when you read even this tiny example.
P.S.1: While there are no real shortucts (except of course of writing some small tool that can generate such state machine classes for the given code), you may simplify the manual job with a bit of regular expressions search and replace (Ctrl+F). For example, replacing pattern goto(\d+) with case State.Goto$1 { will replace all gotos with switch case stubs.
P.S.2: If there are far too many switch cases(gotos) you may want to replace switch with some Dictionary lookup.
P.S.3: And as many have already pointed it, I'd still recommend you to try to understand the original code flow and to rewrite it from scratch.
A goto inside the loop means two things:
stop the current execution of the loop (that's like using continue)
skip everything before the goto label (that's like using an if)
Instead of if-statements you could theoretically use a big switch statement without breaks on each case (fall through mode), so instead of doing a goto you can just set the switch value (create a variable) to the correct position and then call continue.
But that would be the same logic and lead to the same problems that you have when using gotos. It is not understandable, debuggable, maintainable for someone who didn't write the code. After 3-4 months it is even hard for the same person.
As a first step you should try to take each block and identify what exactly it does and if possible extract it to a method/function with a good name.
When your finished you hopefully have a huge goto mess with some function calls in it. Try to reduce the mess with if statements. g70 looks like a probable candidate because it conditionaly performs the g80 stuff or skips it and continues at g90, but haven't looked in detail at it.
Once you have it down to that level, you can also make execution graphs, one row (maybe in a spreadsheet) for each goto label and just write down which functions then get called until it reaches another goto.
Basic for a good refactoring is a good understanding of the problem, so maybe interviewing the person who wrote the pascal code to explain what it does can help you a lot.
Would be fun to see what you made out of the code once it's done and what it really does!
Related
What does "-?" as double type represent in C#?
So I'm running iterations with this formula: double x = 10 / 0.25 * ((0.0002 * x1 * (10 - 0.25 * x1)) + 0.00217 * x2 * (20 - 0.25 * x2)); With this process: Xn+1 = f(Xn). And if you start from negative X you will eventually end up with (-/+) infinity, so after 6 iterations I'm supposed to get infinity, but what I got surprised me and I couldn't find anywhere what that is, I got "-?", I've tried comparing it to +/- infinity and tried to compare it to int numbers just to clarify what it is, but I cant get anything out of it, for example, I've tried if ("-?" > 1000) break;, and it doesn't outcome as "true". Neither am I getting any errors by comparing it to int/double, I need to stop iterations when I start going into infinity, how can I do that? code: public static double CalculateX1(double x1, double x2) { double x = 10 / 0.25 * ((0.0002 * x1 * (10 - 0.25 * x1)) + 0.00217 * x2 * (20 - 0.25 * x2)); return x; } public static double CalculateX2(double x2, double x1) { double y = 20 / 0.25 * ((0.00052 * x2 * (20 - 0.25 * x2)) + 0.0075 * x1 * (10 - 0.25 * x1)); return y; } static void Main(string[] args) { string writePath = #"C:\Users\evluc\Desktop\cord.txt"; double X = -5; double Y = -5; int pointer = 1; double[,] coordinates = new double[10001, 2]; coordinates[0, 0] = X; coordinates[0, 1] = Y; for (int i = 0; i < 5000; i++) { //double XTemp = CalculateX1(X, Y); //double YTemp = CalculateX2(Y, X); //X = CalculateX1(coordinates[pointer - 1, 0], coordinates[pointer - 1, 1]); //Y = CalculateX2(coordinates[pointer - 1, 1], coordinates[pointer - 1, 0]); coordinates[pointer, 0] = CalculateX1(coordinates[pointer - 1, 0], coordinates[pointer - 1, 1]); coordinates[pointer, 1] = CalculateX2(coordinates[pointer - 1, 1], coordinates[pointer - 1, 0]); pointer++; if (Math.Abs(coordinates[pointer, 0]) > 1000 || Math.Abs(coordinates[pointer, 1]) > 1000) { Console.WriteLine("infinity"); Console.ReadKey(); } } for (int i = 0; i < 5000; i++) { Console.WriteLine("X = " + coordinates[i, 0] + "," + "Y = " + coordinates[i, 1] + "; "); } }
I think whatever you use to display/inspect the value cannot print ∞. double d = double.MinValue; d *= 2; Console.WriteLine($"{d}: IsInfinity: {double.IsNegativeInfinity(d)}"); -∞: IsInfinity: True Stopping at infinity Here's a loop that stops at infinity: double d = 2; var i = 1; while(!double.IsInfinity(d)) { d = i*d*d; i = -i; } Console.WriteLine(d); -∞
How to return two list with a double method? [duplicate]
This question already has answers here: Return multiple values to a method caller (28 answers) Closed 3 years ago. I'm working on a C# program that getting some values from textboxes and set them to some variables in 2 class that their name is:"Tfluid" and "Twell" then I use them in another class the name is "Tcalc" through a method with name "fluidcal" and make some calculations with them, I have 2 crossed while that means it starts with a "ql" and then match a final P to each "ql". now I want to use these (ql, P) to plot a graph in another form, so what is the best way of saving them (i don't know what should I return with the fluidal method) and how can I add them to the chart? this is my calculation process public double Fluidcal(TWell well, TFluid fluid) { double froudnumber, noslipholdup, vm, vsl, vsg, liquidvelocitynumber, l1, l2, l3, l4, fluidregim, hozhold, C, psy, liqhold, liqholdseg, liqholdinter, fn, nren, densityn, viscosityn, y, S, ftp, dpdzel, dpdzf, dpperdzt, rhos, Ek,ql; ql = 1; while (ql<=fluid.maxoilflowrate) { vsl = (ql) / Math.PI * Math.Pow(well.tubingdiameter / 2, 2); //superficial velocities vsg = (ql * fluid.gor) / Math.PI * Math.Pow(well.tubingdiameter / 2, 2); vm = vsl + vsg; double nowlength = 0; double P = well.wellheadpressure; while (nowlength <= well.welldepth) { froudnumber = Math.Pow(vm, 2) / well.tubingdiameter * 32.174; //froud number noslipholdup = vsl / vm; //no slip holdup double newoildensity = fluid.oildensity / (1 + 0.00056 * (0.01515 * nowlength)); liquidvelocitynumber = vsl * Math.Pow((newoildensity / 32.174 * fluid.gasliquidsurfacetension), 0.25); //liquid velocity number //correlating parametrs l1 = 316 * Math.Pow(noslipholdup, 0.302); l2 = 0.0009252 * Math.Pow(noslipholdup, -2.4684); l3 = 0.1 * Math.Pow(noslipholdup, -1.4516); l4 = 0.5 * Math.Pow(noslipholdup, -6.738); fluidregim = 0; C = 0; liqhold = 0; if ((noslipholdup < 0.01 && froudnumber < l1) || (noslipholdup >= 0.01 && froudnumber < l2)) { segregated(froudnumber, noslipholdup, liquidvelocitynumber, out fluidregim, out hozhold, out C, out psy, out liqhold); } else if ((noslipholdup >= 0.01 && l2 < froudnumber && froudnumber <= l3)) { transition(froudnumber, noslipholdup, liquidvelocitynumber, l2, l3, out fluidregim, out hozhold, out C, out psy, out liqhold, out liqholdseg, out liqholdinter); } else if ((noslipholdup >= 0.01 && noslipholdup < 0.4 && froudnumber > l3 && froudnumber <= l1) || (noslipholdup >= 0.4 && l3 < froudnumber && froudnumber <= 4)) { intermittent(froudnumber, noslipholdup, liquidvelocitynumber, out fluidregim, out hozhold, out C, out psy, out liqhold); } else if ((noslipholdup < 0.4 && froudnumber >= l1) || (noslipholdup >= 0.4 && froudnumber > l4)) { disturbuted(froudnumber, noslipholdup, out fluidregim, out hozhold, out C, out psy, out liqhold); } // else // { // System.Windows.Forms.MessageBox.Show("Undefined Flow Regim"); //} y = noslipholdup / Math.Pow(liqhold, 2); if (y > 1 && y < 1.2) { S = Math.Log(2.2 * y - 1.2); } else { S = (Math.Log(y)) / (-0.0523 + 3.182 * Math.Log(y) - 0.8725 * Math.Pow(Math.Log(y), 2) + 0.01853 * Math.Pow(Math.Log(y), 4)); } viscosityn = fluid.oilviscosity * noslipholdup + fluid.gasviscosity * (1 - noslipholdup); densityn = fluid.oildensity * noslipholdup + (1 - noslipholdup) * fluid.gasdensity; nren = densityn * vm * (well.tubingdiameter) / viscosityn; fn = 1 / Math.Pow((2 * Math.Log(nren / (4.5223 * Math.Log(nren) - 3.8215))), 2); ftp = fn * Math.Exp(S); rhos = fluid.oildensity * liqhold + fluid.gasdensity * (1 - liqhold); Ek = rhos * vm * vsg / 32.2 * P; dpdzel = (1) * rhos; dpdzf = ftp * densityn * Math.Pow(vm, 2) / 2 * 32.174 * well.tubingdiameter; dpperdzt = (dpdzel + dpdzf) / 1 - Ek; P = P + 5 * dpperdzt; yax.Add(P); nowlength = +5; vsl = vm * liqhold; vsg = vsl * fluid.gor; } ql = +20; }return; //what should I write here?I want pairs of(ql,P)
There are a few ways you could do this. I would suggest encapsulating the lists in an object and returning that object from your method. For example: public class MyLists { public List<double> ListOne { get; set; } public List<double> ListTwo { get; set; } } Of course, you could use a Tuple. If I understand your question correctly...
how to read values from my text file and store in variables to do mathematical calculations?
int a=6378137; int A0=6367449; double B0=16038.43; double C0=16.83261; double D0=0.021984; double E0= 0.000313; double k0=0.9996; double e1=0.081819; double e1sq = 0.006739497; double m = -120; double l = 60; int n = 11; int o = 6 *n- 183; double p = ((m - o) * 0.01744444); double q = l * 0.0174444; double r = m * 0.0174444; double s = a * (1 - e1 * e1) / (1 - ((e1 * Math.Sin(q)) * (e1 * Math.Sin(q))*(e1 * Math.Sin(q)))); double t = a / ((1 - (e1 * Math.Sin(q)))); double v = A0 * q - B0 * Math.Sin(2 * q) + C0 * Math.Sin(4 * q) - D0 * Math.Sin(6 * q) + E0 * Math.Sin(8 * q); double x = v * k0; double y = t * Math.Sin(q) * Math.Cos(q) / 2; double z = (t * Math.Sin(q) * (Math.Cos(q)* Math.Cos(q)*Math.Cos(q)) / 24) * (5 - (Math.Tan(q)* Math.Tan(q)) + 9 * e1sq * (Math.Cos(q) *Math.Cos(q)) + 4 * e1sq *e1sq * (Math.Cos(q)*Math.Cos(q)*Math.Cos(q)*Math.Cos(q)) ) * k0; double AA = t * Math.Cos(q) * k0; double AB = (Math.Cos(q)*Math.Cos(q)*Math.Cos(q)) * (t / 6) * (1 - (Math.Tan(q) *Math.Tan(q)) + e1sq * (Math.Cos(q) *Math.Cos(q))) * k0; double AC = ((p * p * p * p * p * p) * t * Math.Sin(q) * (Math.Cos(q) * Math.Cos(q) * Math.Cos(q) * Math.Cos(q) * Math.Cos(q)) / 720) * (61 - 58 * (Math.Tan(q) * Math.Tan(q)) + (Math.Tan(q) * Math.Tan(q) * Math.Tan(q) * Math.Tan(q)) + 270 * e1sq * (Math.Cos(q) * Math.Cos(q)) - 330 * e1sq * (Math.Sin(q) * Math.Sin(q))) * k0; double AD = (x+y*p*p+z*(p*p*p*p)); double AE; double AF; if (AD < 0) { AE = (10000000 + AD); } else { AE = AD; } AF = 500000 + (AA * p + AB * (p * p * p)); using (StreamWriter writer = new StreamWriter(#"C:\Users\dcf\Desktop\out.txt")) { writer.WriteLine("north:{0}",AE); writer.WriteLine("east:{0}",AF); } This is my code where m and l value should have the value from text file. my text file looks like m,l 56536716.534,43843327.880 56584832.507,43501825.240 56669161.226,43199470.844 56715913.464,42856275.950 56782842.247,42435886.070 56810019.308,42071222.859 56836462.600,41706208.191 56900872.591,41282609.754 i need to read m and l value directly from my text file to do calcultion.
Assuming the values for m and l are alternating in your textfile. First you should read in the file, split the values by a delimter(e.g. ",") and assign them to variables m and l. Then you can call your calculation-function for each private void readFile(pathToFile) { var textfilecontent= File.ReadAllLines(pathToFile); for (int i = 1; i < textfilecontent.Length; i++) { // splits the values at delimiter , var valPerLine = textfilecontent[i].Split(','); // only works if you have only two numbers per line calculate(Convert.ToDouble(valPerLine[0]), Convert.ToDouble(valPerLine[1])) } } private void calculate(double m, double l) { // your formulas, m and l should now be inserted as parameter int a=6378137; int A0=6367449; double B0=16038.43; double C0=16.83261; double D0=0.021984; double E0= 0.000313; double k0=0.9996; double e1=0.081819; double e1sq = 0.006739497; //double m = -120; //double l = 60; int n = 11; int o = 6 *n- 183; double p = ((m - o) * 0.01744444); double q = l * 0.0174444; double r = m * 0.0174444; double s = a * (1 - e1 * e1) / (1 - ((e1 * Math.Sin(q)) * (e1 * Math.Sin(q))*(e1 * Math.Sin(q)))); double t = a / ((1 - (e1 * Math.Sin(q)))); double v = A0 * q - B0 * Math.Sin(2 * q) + C0 * Math.Sin(4 * q) - D0 * Math.Sin(6 * q) + E0 * Math.Sin(8 * q); double x = v * k0; double y = t * Math.Sin(q) * Math.Cos(q) / 2; double z = (t * Math.Sin(q) * (Math.Cos(q)* Math.Cos(q)*Math.Cos(q)) / 24) * (5 - (Math.Tan(q)* Math.Tan(q)) + 9 * e1sq * (Math.Cos(q) *Math.Cos(q)) + 4 * e1sq *e1sq * (Math.Cos(q)*Math.Cos(q)*Math.Cos(q)*Math.Cos(q)) ) * k0; double AA = t * Math.Cos(q) * k0; double AB = (Math.Cos(q)*Math.Cos(q)*Math.Cos(q)) * (t / 6) * (1 - (Math.Tan(q) *Math.Tan(q)) + e1sq * (Math.Cos(q) *Math.Cos(q))) * k0; double AC = ((p * p * p * p * p * p) * t * Math.Sin(q) * (Math.Cos(q) * Math.Cos(q) * Math.Cos(q) * Math.Cos(q) * Math.Cos(q)) / 720) * (61 - 58 * (Math.Tan(q) * Math.Tan(q)) + (Math.Tan(q) * Math.Tan(q) * Math.Tan(q) * Math.Tan(q)) + 270 * e1sq * (Math.Cos(q) * Math.Cos(q)) - 330 * e1sq * (Math.Sin(q) * Math.Sin(q))) * k0; double AD = (x+y*p*p+z*(p*p*p*p)); double AE; double AF; if (AD < 0) { AE = (10000000 + AD); } else { AE = AD; } AF = 500000 + (AA * p + AB * (p * p * p)); using (StreamWriter writer = new StreamWriter(#"C:\Users\dcf\Desktop\out.txt")) { writer.WriteLine("north:{0}",AE); writer.WriteLine("east:{0}",AF); } }
System.NullReferenceException C# advice?
I'm getting an exception when I'm trying to run my program.. It seems to me it's working well but I'm missing something as it appears. public Coord LatLonToUTMXY(double lat, double lon) { Coord co = new Coord(); co.zone = Math.Floor((lon + 180.0) / 6) + 1; //double zone = Math.Floor((lon + 180.0) / 6) + 1; MapLatLonToXY(DegToRad(lat), DegToRad(lon), UTMCentralMeridian(co.zone), co.eastNorth); /* Adjust easting and northing for UTM system. */ co.eastNorth[0] = co.eastNorth[0] * UTMScaleFactor + 500000.0; co.eastNorth[1] = co.eastNorth[1] * UTMScaleFactor; if (co.eastNorth[1] < 0.0) co.eastNorth[1] = co.eastNorth[1] + 10000000.0; return co; } I'm getting the exception here ... public void MapLatLonToXY(double phi, double lambda, double lambda0, double [] xy) { //Some previous code //Exception for xy[0] ....! xy[0] = N * Math.Cos(phi) * l + (N / 6.0 * Math.Pow(Math.Cos(phi), 3.0) * l3coef * Math.Pow(l, 3.0)) + (N / 120.0 * Math.Pow(Math.Cos(phi), 5.0) * l5coef * Math.Pow(l, 5.0)) + (N / 5040.0 * Math.Pow(Math.Cos(phi), 7.0) * l7coef * Math.Pow(l, 7.0)); /* Calculate northing (y) */ xy[1] = ArcLengthOfMeridian(phi) + (t / 2.0 * N * Math.Pow(Math.Cos(phi), 2.0) * Math.Pow(l, 2.0)) + (t / 24.0 * N * Math.Pow(Math.Cos(phi), 4.0) * l4coef * Math.Pow(l, 4.0)) + (t / 720.0 * N * Math.Pow(Math.Cos(phi), 6.0) * l6coef * Math.Pow(l, 6.0)) + (t / 40320.0 * N * Math.Pow(Math.Cos(phi), 8.0) * l8coef * Math.Pow(l, 8.0)); return; } for the co it's a struct public struct Coord { public double zone; public double[] eastNorth; } What am I missing here ? Thanks
This is happening because the xy array in your function (which comes from Coord.eastNorth is uninitialized. I noticed that Coord was a struct and not a class.. any particular reason? This would work if Coord was a class public class Coord { public double Zone {get; set;} public List<double> EastNorth {get; set;} public Coord() { EastNorth = new List<double>(); } } public List<double> MapLatLonToXY(double phi, double lambda, double lambda0) { var xy = new List<double>(); //Exception for xy[0] ....! xy.Add(N * Math.Cos(phi) * l + (N / 6.0 * Math.Pow(Math.Cos(phi), 3.0) * l3coef * Math.Pow(l, 3.0)) + (N / 120.0 * Math.Pow(Math.Cos(phi), 5.0) * l5coef * Math.Pow(l, 5.0)) + (N / 5040.0 * Math.Pow(Math.Cos(phi), 7.0) * l7coef * Math.Pow(l, 7.0))); /* Calculate northing (y) */ xy.Add(ArcLengthOfMeridian(phi) + (t / 2.0 * N * Math.Pow(Math.Cos(phi), 2.0) * Math.Pow(l, 2.0)) + (t / 24.0 * N * Math.Pow(Math.Cos(phi), 4.0) * l4coef * Math.Pow(l, 4.0)) + (t / 720.0 * N * Math.Pow(Math.Cos(phi), 6.0) * l6coef * Math.Pow(l, 6.0)) + (t / 40320.0 * N * Math.Pow(Math.Cos(phi), 8.0) * l8coef * Math.Pow(l, 8.0))); return xy; } public Coord LatLonToUTMXY(double lat, double lon) { Coord co = new Coord(); co.zone = Math.Floor((lon + 180.0) / 6) + 1; //double zone = Math.Floor((lon + 180.0) / 6) + 1; co.EastNorth = MapLatLonToXY(DegToRad(lat), DegToRad(lon), UTMCentralMeridian(co.zone)); /* Adjust easting and northing for UTM system. */ co.EastNorth[0] = co.EastNorth[0] * UTMScaleFactor + 500000.0; co.EastNorth[1] = co.EastNorth[1] * UTMScaleFactor; if (co.EastNorth[1] < 0.0) co.EastNorth[1] = co.EastNorth[1] + 10000000.0; return co; }
RGB to HSL, hue calculation is wrong
I'm trying to convert a RGB32 value to HSL because I want to use the Hue component. I have used some examples that I found online to create this class: public class HSLColor { public Double Hue; public Double Saturation; public Double Luminosity; public HSLColor(Double H, Double S, Double L) { Hue = H; Saturation = S; Luminosity = L; } public static HSLColor FromRGB(Color Clr) { return FromRGB(Clr.R, Clr.G, Clr.B); } public static HSLColor FromRGB(Byte R, Byte G, Byte B) { Double _R = (R / 255d); Double _G = (G / 255d); Double _B = (B / 255d); Double _Min = Math.Min(Math.Min(_R, _G), _B); Double _Max = Math.Max(Math.Max(_R, _G), _B); Double _Delta = _Max - _Min; Double H = 0; Double S = 0; Double L = (float)((_Max + _Min) / 2.0f); if (_Delta != 0) { if (L < 0.5d) { S = (float)(_Delta / (_Max + _Min)); } else { S = (float)(_Delta / (2.0f - _Max - _Min)); } if (_R == _Max) { H = (_G - _B) / _Delta; } else if (_G == _Max) { H = 2f + (_B - _R) / _Delta; } else if (_B == _Max) { H = 4f + (_R - _G) / _Delta; } } //Convert to degrees H = H * 60d; if (H < 0) H += 360; //Convert to percent S *= 100d; L *= 100d; return new HSLColor(H, S, L); } private Double Hue_2_RGB(Double v1, Double v2, Double vH) { if (vH < 0) vH += 1; if (vH > 1) vH -= 1; if ((6.0d * vH) < 1) return (v1 + (v2 - v1) * 6 * vH); if ((2.0d * vH) < 1) return (v2); if ((3.0d * vH) < 2) return (v1 + (v2 - v1) * ((2.0d / 3.0d) - vH) * 6.0d); return (v1); } public Color ToRGB() { Color Clr = new Color(); Double var_1, var_2; if (Saturation == 0) { Clr.R = (Byte)(Luminosity * 255); Clr.G = (Byte)(Luminosity * 255); Clr.B = (Byte)(Luminosity * 255); } else { if (Luminosity < 0.5) var_2 = Luminosity * (1 + Saturation); else var_2 = (Luminosity + Saturation) - (Saturation * Luminosity); var_1 = 2 * Luminosity - var_2; Clr.R = (Byte)(255 * Hue_2_RGB(var_1, var_2, Hue + (1 / 3))); Clr.G = (Byte)(255 * Hue_2_RGB(var_1, var_2, Hue)); Clr.B = (Byte)(255 * Hue_2_RGB(var_1, var_2, Hue - (1 / 3))); } return Clr; } } However it doesn't seem to work correctly, If I use an input color of (R 0, G 255, B 193) for example: I get Hue = 0 while in photoshop if I choose the exact same RGB values I get: Hue = 165 which is the correct value. I want the Hue to be a value ranging from 0 to 360 or 0 to 240 What is the problem?.. Reference: EasyRGB RGB->HSL
R / 255 this is integer division, meaning you get either 0 or 1. try: (float)R / 255 and for all other cases. for constants try: ( 1 / 3 ) -> ( 1.0 / 3.0 )
The below is open source mixture of Drupal + some various programmers work mixed into one single function boasting RGB > HSL and back. It works flawlessly. <?php ### RGB >> HSL function _color_rgb2hsl($rgb) { $r = $rgb[0]; $g = $rgb[1]; $b = $rgb[2]; $min = min($r, min($g, $b)); $max = max($r, max($g, $b)); $delta = $max - $min; $l = ($min + $max) / 2; $s = 0; if ($l > 0 && $l < 1) { $s = $delta / ($l < 0.5 ? (2 * $l) : (2 - 2 * $l)); } $h = 0; if ($delta > 0) { if ($max == $r && $max != $g) $h += ($g - $b) / $delta; if ($max == $g && $max != $b) $h += (2 + ($b - $r) / $delta); if ($max == $b && $max != $r) $h += (4 + ($r - $g) / $delta); $h /= 6; } return array($h, $s, $l); } ### HSL >> RGB function _color_hsl2rgb($hsl) { $h = $hsl[0]; $s = $hsl[1]; $l = $hsl[2]; $m2 = ($l <= 0.5) ? $l * ($s + 1) : $l + $s - $l*$s; $m1 = $l * 2 - $m2; return array(_color_hue2rgb($m1, $m2, $h + 0.33333), _color_hue2rgb($m1, $m2, $h), _color_hue2rgb($m1, $m2, $h - 0.33333)); } ### Helper function for _color_hsl2rgb(). function _color_hue2rgb($m1, $m2, $h) { $h = ($h < 0) ? $h + 1 : (($h > 1) ? $h - 1 : $h); if ($h * 6 < 1) return $m1 + ($m2 - $m1) * $h * 6; if ($h * 2 < 1) return $m2; if ($h * 3 < 2) return $m1 + ($m2 - $m1) * (0.66666 - $h) * 6; return $m1; } ### Convert a hex color into an RGB triplet. function _color_unpack($hex, $normalize = false) { if (strlen($hex) == 4) { $hex = $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3]; } $c = hexdec($hex); for ($i = 16; $i >= 0; $i -= 8) { $out[] = (($c >> $i) & 0xFF) / ($normalize ? 255 : 1); } return $out; } ### Convert an RGB triplet to a hex color. function _color_pack($rgb, $normalize = false) { foreach ($rgb as $k => $v) { $out |= (($v * ($normalize ? 255 : 1)) << (16 - $k * 8)); }return '#'. str_pad(dechex($out), 6, 0, STR_PAD_LEFT); } print "Hex: "; $testhex = "#b7b700"; print $testhex; $testhex2rgb = _color_unpack($testhex,true); print "<br />RGB: "; var_dump($testhex2rgb); print "<br />HSL color module: "; $testrgb2hsl = _color_rgb2hsl($testhex2rgb); //Convert to HSL var_dump($testrgb2hsl); print "<br />RGB: "; $testhsl2rgb = _color_hsl2rgb($testrgb2hsl); // And back to RGB var_dump($testhsl2rgb); print "<br />Hex: "; $testrgb2hex = _color_pack($testhsl2rgb,true); var_dump($testrgb2hex); ?>