Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Floats aren't stable for accumulating and decrementing funds. Here's your actual example:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BadFloat { class Program { static void Main(string[] args) { Currency yourMoneyAccumulator = 0.0d; int count = 200000; double increment = 20000.01d; //1 cent for (int i = 0; i &lt; count; i++) yourMoneyAccumulator += increment; Console.WriteLine(yourMoneyAccumulator + " accumulated vs. " + increment * count + " expected"); } } struct Currency { private const double EPSILON = 0.00005; public Currency(double value) { this.value = value; } private double value; public static Currency operator +(Currency a, Currency b) { return new Currency(a.value + b.value); } public static Currency operator -(Currency a, Currency b) { return new Currency(a.value - b.value); } public static Currency operator *(Currency a, double factor) { return new Currency(a.value * factor); } public static Currency operator *(double factor, Currency a) { return new Currency(a.value * factor); } public static Currency operator /(Currency a, double factor) { return new Currency(a.value / factor); } public static Currency operator /(double factor, Currency a) { return new Currency(a.value / factor); } public static explicit operator double(Currency c) { return System.Math.Round(c.value, 4); } public static implicit operator Currency(double d) { return new Currency(d); } public static bool operator &lt;(Currency a, Currency b) { return (a.value - b.value) &lt; -EPSILON; } public static bool operator &gt;(Currency a, Currency b) { return (a.value - b.value) &gt; +EPSILON; } public static bool operator &lt;=(Currency a, Currency b) { return (a.value - b.value) &lt;= +EPSILON; } public static bool operator &gt;=(Currency a, Currency b) { return (a.value - b.value) &gt;= -EPSILON; } public static bool operator !=(Currency a, Currency b) { return Math.Abs(a.value - b.value) &lt;= EPSILON; } public static bool operator ==(Currency a, Currency b) { return Math.Abs(a.value - b.value) &gt; EPSILON; } public bool Equals(Currency other) { return this == other; } public override int GetHashCode() { return ((double)this).GetHashCode(); } public override bool Equals(object other) { return other is Currency &amp;&amp; this.Equals((Currency)other); } public override string ToString() { return this.value.ToString("C4"); } } } </code></pre> <p>On my box this gives $4,000,002,000.0203 accumulated vs. 4000002000 expected in C#. It's a bad deal if this gets lost over many transactions in a bank - it doesn't have to be large ones, just many. Does that help?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload