Decimal Arithmetic

1. Junior Member
Join Date
Dec 1969
Posts
10

## Decimal Arithmetic

Hi all,<BR>2 points:<BR><BR>1. I&#039;m looking for a calculation that would show the advantages of using the decimal primitive in C#, ie a calculation that would yield different results when using floats and decimals.<BR><BR>In Java such calculations would be repeatedly dividing 9 by 10, or 0.70 * 1.05. With C#, these give correct results even with floats and doubles.<BR><BR><BR>2. What is the point of the Decimal class? does it offer any advantages over the decimal primitive, or is it just a wrapper class?<BR><BR>Cheers,<BR>Robin

2. Senior Member
Join Date
Dec 1969
Posts
1,424

## RE: Decimal Arithmetic

I whipped this up as a quick-n-dirty C# windows forms app. I just dragged 2 buttons onto Form1.cs and tied them to this code.<BR><BR>When I ran it, the results were different:<BR>--------------------------------------------------------<BR><BR>private void DecimalsButton_Click(object sender, System.EventArgs e)<BR>{<BR> StringBuilder sb = new System.Text.StringBuilder() ;<BR> System.Decimal principal = 100000m ;<BR> System.Decimal monthlyPayment = 1100.52m ;<BR> System.Decimal interestRate = 0.0055m ; <BR> <BR> for(int months = 0 ; months &#060; 360 ; months++) {<BR> Decimal interestAdded = (((principal * interestRate) * 100) / 100) ;<BR> Decimal amountToApply = (monthlyPayment - interestAdded) ;<BR> <BR> principal = (principal - amountToApply) ;<BR> <BR> sb.AppendFormat("{0:C} {1:C}
", amountToApply, principal) ;<BR> }<BR> MessageBox.Show(sb.ToString()) ;<BR>}<BR><BR>private void FloatsButton_Click(object sender, System.EventArgs e)<BR>{<BR> StringBuilder sb = new System.Text.StringBuilder() ;<BR> float principal = 100000f ;<BR> float monthlyPayment = 1100.52f ;<BR> float interestRate = 0.0055f ; <BR><BR> for(int months = 0 ; months &#060; 360 ; months++) <BR> {<BR> float interestAdded = (((principal * interestRate) * 100) / 100) ;<BR> float amountToApply = (monthlyPayment - interestAdded) ;<BR> <BR> principal = (principal - amountToApply) ;<BR> <BR> sb.AppendFormat("{0:C} {1:C}
", amountToApply, principal) ;<BR> }<BR> MessageBox.Show(sb.ToString()) ;<BR>}

3. Senior Member
Join Date
Dec 1969
Posts
1,424

## To further illustrate the point:-

Actually, that sample was a re-hash of a javascript demo that BillW helped me work through last year.<BR><BR>There&#039;s a smaller, similar javascript based demo that I created at the bottom of this page:<BR><BR>----------------------------------------<BR><BR>http://www.flws.com.au/RE/RE_PresentF.html

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## How about an easy demo:

I&#039;m not "up" on using C# with the Decimal class, so excuse any syntax errors, but the intent should be clear.<BR><BR>Decimal dc = new Decimal(1./100.);<BR>double db = 1./100.;<BR><BR>Decimal sumdc = 0;<BR>double sumdb = 0.0;<BR>for ( int i = 1; i &#060;= 100; ++i )<BR>{<BR> sumdb += db; <BR> sumdc.Add( dc ); // intended to be equiv of just prior line<BR>}<BR>sumdb -= 1.0; <BR>sumdc.Sub( 1.0 ); // ditto<BR><BR>... now display both sumdb and sumdc, if necessary forcing floating point notation for sumdb.<BR><BR>I got a value of 6.66133814775094E-16 for sumdb. Enough said?<BR><BR>(You *should* get exactly zero for sumdc.)<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Forgot to mention...

Using Decimal is almost surely from 10 to 100 times slower than using double.<BR><BR>That&#039;s because double is done natively, in the hardware of any x86 chip since the 386. Decimal has to be done with slow and ugly software, taking many many steps to do something that is so simple in double.<BR><BR><BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•