Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

testing precsison in C# floating point variables

3 views
Skip to first unread message

ericmatteson...@hotmail.com

unread,
Sep 9, 2011, 10:35:26 PM9/9/11
to
------ */
// delete all lines above this line
using System;
using System.IO;
// nuprn.cs check precision of mantissia in
// 32 bit float and 64 bit double and
// cobol.NET decimal data types.
public class dopr
{
public int [] xdigits = new int [80];
public double ddw;
public int dase;
public int dexp;
public int dnegind;
public string dws;
public void prfsa()
{
// put mantissia digits of .ddw into .xdigits[]
// using base .dase
double lddw;
double ldcmp;
int lwctr;
int lwnext;
int lwallow;
int dlct;
double ldenom;
// char [] lone = new char[1];
ldenom=this.dase;
lddw=this.ddw;
// depth=2
// lwctr=0;
// lwallow=1;
this.dnegind=0;
this.dexp=0;
if(this.ddw < 0.0)
{
this.dnegind=1;
lddw=0.0-lddw;
}
if(lddw >= 1.0)
{
while(lddw >= 1.0)
{
this.dexp=this.dexp+1;
lddw=lddw/ldenom;
}
}
// depth=2
if((lddw > 0.0)&&(lddw < (1.0/ldenom)))
{
while(lddw < (1.0/ldenom))
{
this.dexp=this.dexp-1;
lddw=lddw*ldenom;
}
}
dlct=0;
while(dlct < 78)
{
lwctr=0;
lwallow=1;
while(lwallow > 0)
{
lwnext=lwctr+1;
ldcmp=lddw-(lwnext/ldenom);
if(ldcmp < 0.0)lwallow=0;
if(lwallow > 0)lwctr=lwnext;
// depth=4
if(lwctr >= (this.dase-1))lwallow=0;
}
if(lwctr > 9)lwctr=lwctr+7;
this.xdigits[dlct]=lwctr+48;
ldcmp=lddw-(lwctr/ldenom);
lddw=ldcmp;
lddw=lddw*ldenom;
dlct=dlct+1;
}
}
// depth = 1
public void prfsb(int prfbid)
{
// put prfbid digits from.xdigits
// into string .dws result
int dlctb,lwnextb;
char [] loneb = new char[1];
this.dws="";
dlctb=0;
while(dlctb < prfbid)
{
lwnextb=this.xdigits[dlctb];
// System.Console.WriteLine(lwnext);
// System.Console.WriteLine(" ");
// depth = 3
loneb[0]=(char)lwnextb;
this.dws=this.dws+System.Convert.ToString(loneb[0]);
dlctb=dlctb+1;
}
}
public int prepwid()
{
// get width of repeating decimal number
// one repitition
int prwictr,prwilef,prwirig,prwilow;
prwilow=1;
prwictr=1;
prwilef=this.xdigits[0];
while(prwilow > 0)
{
prwirig=this.xdigits[prwictr];
if(prwirig == prwilef)prwilow=0;
if(prwictr >= 76)prwilow=0;
if(prwilow > 0)prwictr=prwictr+1;
}
return prwictr;
}
public int prewidth()
{
// get number of digits that are
// potentially correct.
int preolop,preilop,prerig,prelef,prelow;
int preilim,presize;
preilim=this.prepwid();
preolop=0;
presize=0;
prelow=1;
while((preolop < 76)&&(prelow > 0))
{
preilop=0;
while((preilop < preilim)&&(prelow > 0))
{
prelef=this.xdigits[preilop];
prerig=this.xdigits[presize];
if(prelef != prerig)prelow=0;
if(presize >= 76)prelow=0;
if(prelow > 0)
{
presize=presize+1;
preilop=preilop+1;
}
}
preolop=preolop+preilim;
}
return presize;
}
// depth = 1
}
public class gobol
{
public int [] xdigits = new int [80];
public decimal ddw;
public int dase;
public int dexp;
public int dnegind;
public string dws;
public void prfsa()
{
decimal lddw;
decimal ldcmp;
int lwctr;
int lwnext;
int lwallow;
int dlct;
decimal ldenom;
// char [] lone = new char[1];
ldenom=this.dase;
lddw=this.ddw;
// depth=2
// lwctr=0;
// lwallow=1;
this.dnegind=0;
this.dexp=0;
if(this.ddw < 0.0M)
{
this.dnegind=1;
lddw=0.0M-lddw;
}
if(lddw >= 1.0M)
{
while(lddw >= 1.0M)
{
this.dexp=this.dexp+1;
lddw=lddw/ldenom;
}
}
// depth=2
if((lddw > 0.0M)&&(lddw < (1.0M/ldenom)))
{
while(lddw < (1.0M/ldenom))
{
this.dexp=this.dexp-1;
lddw=lddw*ldenom;
}
}
dlct=0;
while(dlct < 78)
{
lwctr=0;
lwallow=1;
while(lwallow > 0)
{
lwnext=lwctr+1;
ldcmp=lddw-(lwnext/ldenom);
if(ldcmp < 0.0M)lwallow=0;
if(lwallow > 0)lwctr=lwnext;
// depth=4
if(lwctr >= (this.dase-1))lwallow=0;
}
if(lwctr > 9)lwctr=lwctr+7;
this.xdigits[dlct]=lwctr+48;
ldcmp=lddw-(lwctr/ldenom);
lddw=ldcmp;
lddw=lddw*ldenom;
dlct=dlct+1;
}
}
// depth = 1
public void prfsb(int prfbid)
{
int dlctb,lwnextb;
char [] loneb = new char[1];
this.dws="";
dlctb=0;
while(dlctb < prfbid)
{
lwnextb=this.xdigits[dlctb];
// System.Console.WriteLine(lwnext);
// System.Console.WriteLine(" ");
// depth = 3
loneb[0]=(char)lwnextb;
this.dws=this.dws+System.Convert.ToString(loneb[0]);
dlctb=dlctb+1;
}
}
public int prepwid()
{
int prwictr,prwilef,prwirig,prwilow;
prwilow=1;
prwictr=1;
prwilef=this.xdigits[0];
while(prwilow > 0)
{
prwirig=this.xdigits[prwictr];
if(prwirig == prwilef)prwilow=0;
if(prwictr >= 76)prwilow=0;
if(prwilow > 0)prwictr=prwictr+1;
}
return prwictr;
}
public int prewidth()
{
int preolop,preilop,prerig,prelef,prelow;
int preilim,presize;
preilim=this.prepwid();
preolop=0;
presize=0;
prelow=1;
while((preolop < 76)&&(prelow > 0))
{
preilop=0;
while((preilop < preilim)&&(prelow > 0))
{
prelef=this.xdigits[preilop];
prerig=this.xdigits[presize];
if(prelef != prerig)prelow=0;
if(presize >= 76)prelow=0;
if(prelow > 0)
{
presize=presize+1;
preilop=preilop+1;
}
}
preolop=preolop+preilim;
}
return presize;
}
// depth = 1
}
class nuprn
{
public static void Main(String[] unusedu)
{
int arepwid,bigprewid,bigslawid,bigcobwid;
float czshrink;
dopr door=new dopr();
dopr slam=new dopr();
gobol cobol=new gobol();
door.dase=10;
slam.dase=10;
cobol.dase=10;
door.ddw=1.0/7.0;
czshrink=(float)door.ddw;
slam.ddw=(double)czshrink;
cobol.ddw=1.0M/7.0M;
door.prfsa();
slam.prfsa();
cobol.prfsa();
arepwid=door.prepwid();
door.prfsb(arepwid);
System.Console.WriteLine("Repeating decimal 1/7 "+door.dws);
bigprewid=door.prewidth();
bigslawid=slam.prewidth();
bigcobwid=cobol.prewidth();
slam.prfsb(bigslawid-1);
System.Console.WriteLine("32 Single precision "+slam.dws);
door.prfsb(bigprewid-1);
System.Console.WriteLine("64 Double precision "+door.dws);
if(bigcobwid > 7)
{
cobol.prfsb(bigcobwid-1);
System.Console.WriteLine("Cobol decimal list "+cobol.dws);
}
System.Console.WriteLine("Check precision of mantissia");
System.Console.WriteLine("program by Eric Matteson in C#");
}
}
// delete all lines below this line
/* --------

0 new messages