A wonder of double convert to integer
-
See the FunA() and FunB() result, why any diffent?
private void FunA() { double value; System.Text.StringBuilder sb = new System.Text.StringBuilder(); for( **value = 2.1;** value >= -1.1; value -= 0.1 ) sb.AppendFormat("Ceiling({0:f})={1} Floor({0:f})={2} Round({0:f})={3} (int)({0:f}+0.5)={4} Truncate({0:f})={5}\n", value, Math.Ceiling(value), Math.Floor(value), Math.Round(value), (int)(value+0.5), Decimal.Truncate(Convert.ToDecimal(value))); return sb.ToString(); } private string FunB() { double value; System.Text.StringBuilder sb = new System.Text.StringBuilder(); for( **value = 5.1;** value >= -1.1; value -= 0.1 ) sb.AppendFormat("Ceiling({0:f})={1} Floor({0:f})={2} Round({0:f})={3} (int)({0:f}+0.5)={4} Truncate({0:f})={5}\n", value, Math.Ceiling(value), Math.Floor(value), Math.Round(value), (int)(value+0.5), Decimal.Truncate(Convert.ToDecimal(value))); return sb.ToString(); }
Reuslts is: FunA(): Ceiling(2.10)=3 Floor(2.10)=2 Round(2.10)=2 (int)(2.10+0.5)=2 Truncate(2.10)=2 Ceiling(2.00)=2 Floor(2.00)=2 Round(2.00)=2 (int)(2.00+0.5)=2 Truncate(2.00)=2 Ceiling(1.90)=2 Floor(1.90)=1 Round(1.90)=2 (int)(1.90+0.5)=2 Truncate(1.90)=1 Ceiling(1.80)=2 Floor(1.80)=1 Round(1.80)=2 (int)(1.80+0.5)=2 Truncate(1.80)=1 Ceiling(1.70)=2 Floor(1.70)=1 Round(1.70)=2 (int)(1.70+0.5)=2 Truncate(1.70)=1 Ceiling(1.60)=2 Floor(1.60)=1 Round(1.60)=2 (int)(1.60+0.5)=2 Truncate(1.60)=1 Ceiling(1.50)=2 Floor(1.50)=1 Round(1.50)=1 (int)(1.50+0.5)=1 Truncate(1.50)=1 Ceiling(1.40)=2 Floor(1.40)=1 Round(1.40)=1 (int)(1.40+0.5)=1 Truncate(1.40)=1 Ceiling(1.30)=2 Floor(1.30)=1 Round(1.30)=1 (int)(1.30+0.5)=1 Truncate(1.30)=1 Ceiling(1.20)=2 Floor(1.20)=1 Round(1.20)=1 (int)(1.20+0.5)=1 Truncate(1.20)=1 Ceiling(1.10)=2 Floor(1.10)=1 Round(1.10)=1 (int)(1.10+0.5)=1 Truncate(1.10)=1 Ceiling(1.00)=1 Floor(1.00)=0 Round(1.00)=1 (int)(1.00+0.5)=1 Truncate(1.00)=0 Ceiling(0.90)=1 Floor(0.90)=0 Round(0.90)=1 (int)(0.90+0.5)=1 Truncate(0.90)=0 Ceiling(0.80)=1 Floor(0.80)=0 Round(0.80)=1 (int)(0.80+0.5)=1 Truncate(0.80)=0 Ceiling(0.70)=1 Floor(0.70)=0 Round(0.70)=1 (int)(0.70+0.5)=1 Truncate(0.70)=0 Ceiling(0.60)=1 Floor(0.60)=0 Round(0.60)=1 (int)(0.60+0.5)=1 Truncate(0.60)=0 Ceiling(0.50)=1 Floor(0.50)=0 Round(0.50)=0 (int)(0.50+0.5)=0 Truncate(0.50)=0 Ceiling(0.40)=1 Floor(0.40)=0 Round(0.40)=0 (int)(0.40+0.5)=0 Truncate(0.40)=0 Ceiling(0.30)=1 Floor(0.3 -
See the FunA() and FunB() result, why any diffent?
private void FunA() { double value; System.Text.StringBuilder sb = new System.Text.StringBuilder(); for( **value = 2.1;** value >= -1.1; value -= 0.1 ) sb.AppendFormat("Ceiling({0:f})={1} Floor({0:f})={2} Round({0:f})={3} (int)({0:f}+0.5)={4} Truncate({0:f})={5}\n", value, Math.Ceiling(value), Math.Floor(value), Math.Round(value), (int)(value+0.5), Decimal.Truncate(Convert.ToDecimal(value))); return sb.ToString(); } private string FunB() { double value; System.Text.StringBuilder sb = new System.Text.StringBuilder(); for( **value = 5.1;** value >= -1.1; value -= 0.1 ) sb.AppendFormat("Ceiling({0:f})={1} Floor({0:f})={2} Round({0:f})={3} (int)({0:f}+0.5)={4} Truncate({0:f})={5}\n", value, Math.Ceiling(value), Math.Floor(value), Math.Round(value), (int)(value+0.5), Decimal.Truncate(Convert.ToDecimal(value))); return sb.ToString(); }
Reuslts is: FunA(): Ceiling(2.10)=3 Floor(2.10)=2 Round(2.10)=2 (int)(2.10+0.5)=2 Truncate(2.10)=2 Ceiling(2.00)=2 Floor(2.00)=2 Round(2.00)=2 (int)(2.00+0.5)=2 Truncate(2.00)=2 Ceiling(1.90)=2 Floor(1.90)=1 Round(1.90)=2 (int)(1.90+0.5)=2 Truncate(1.90)=1 Ceiling(1.80)=2 Floor(1.80)=1 Round(1.80)=2 (int)(1.80+0.5)=2 Truncate(1.80)=1 Ceiling(1.70)=2 Floor(1.70)=1 Round(1.70)=2 (int)(1.70+0.5)=2 Truncate(1.70)=1 Ceiling(1.60)=2 Floor(1.60)=1 Round(1.60)=2 (int)(1.60+0.5)=2 Truncate(1.60)=1 Ceiling(1.50)=2 Floor(1.50)=1 Round(1.50)=1 (int)(1.50+0.5)=1 Truncate(1.50)=1 Ceiling(1.40)=2 Floor(1.40)=1 Round(1.40)=1 (int)(1.40+0.5)=1 Truncate(1.40)=1 Ceiling(1.30)=2 Floor(1.30)=1 Round(1.30)=1 (int)(1.30+0.5)=1 Truncate(1.30)=1 Ceiling(1.20)=2 Floor(1.20)=1 Round(1.20)=1 (int)(1.20+0.5)=1 Truncate(1.20)=1 Ceiling(1.10)=2 Floor(1.10)=1 Round(1.10)=1 (int)(1.10+0.5)=1 Truncate(1.10)=1 Ceiling(1.00)=1 Floor(1.00)=0 Round(1.00)=1 (int)(1.00+0.5)=1 Truncate(1.00)=0 Ceiling(0.90)=1 Floor(0.90)=0 Round(0.90)=1 (int)(0.90+0.5)=1 Truncate(0.90)=0 Ceiling(0.80)=1 Floor(0.80)=0 Round(0.80)=1 (int)(0.80+0.5)=1 Truncate(0.80)=0 Ceiling(0.70)=1 Floor(0.70)=0 Round(0.70)=1 (int)(0.70+0.5)=1 Truncate(0.70)=0 Ceiling(0.60)=1 Floor(0.60)=0 Round(0.60)=1 (int)(0.60+0.5)=1 Truncate(0.60)=0 Ceiling(0.50)=1 Floor(0.50)=0 Round(0.50)=0 (int)(0.50+0.5)=0 Truncate(0.50)=0 Ceiling(0.40)=1 Floor(0.40)=0 Round(0.40)=0 (int)(0.40+0.5)=0 Truncate(0.40)=0 Ceiling(0.30)=1 Floor(0.3Use double or Decimal, they are more precise. Christian Graus - Microsoft MVP - C++
-
Use double or Decimal, they are more precise. Christian Graus - Microsoft MVP - C++
-
OK, then try Decimal. Floating point isn't precise, that's just how it is. 1.5 + .5 is probably 1.999999, and then casting always rounds down. Christian Graus - Microsoft MVP - C++
-
See the FunA() and FunB() result, why any diffent?
private void FunA() { double value; System.Text.StringBuilder sb = new System.Text.StringBuilder(); for( **value = 2.1;** value >= -1.1; value -= 0.1 ) sb.AppendFormat("Ceiling({0:f})={1} Floor({0:f})={2} Round({0:f})={3} (int)({0:f}+0.5)={4} Truncate({0:f})={5}\n", value, Math.Ceiling(value), Math.Floor(value), Math.Round(value), (int)(value+0.5), Decimal.Truncate(Convert.ToDecimal(value))); return sb.ToString(); } private string FunB() { double value; System.Text.StringBuilder sb = new System.Text.StringBuilder(); for( **value = 5.1;** value >= -1.1; value -= 0.1 ) sb.AppendFormat("Ceiling({0:f})={1} Floor({0:f})={2} Round({0:f})={3} (int)({0:f}+0.5)={4} Truncate({0:f})={5}\n", value, Math.Ceiling(value), Math.Floor(value), Math.Round(value), (int)(value+0.5), Decimal.Truncate(Convert.ToDecimal(value))); return sb.ToString(); }
Reuslts is: FunA(): Ceiling(2.10)=3 Floor(2.10)=2 Round(2.10)=2 (int)(2.10+0.5)=2 Truncate(2.10)=2 Ceiling(2.00)=2 Floor(2.00)=2 Round(2.00)=2 (int)(2.00+0.5)=2 Truncate(2.00)=2 Ceiling(1.90)=2 Floor(1.90)=1 Round(1.90)=2 (int)(1.90+0.5)=2 Truncate(1.90)=1 Ceiling(1.80)=2 Floor(1.80)=1 Round(1.80)=2 (int)(1.80+0.5)=2 Truncate(1.80)=1 Ceiling(1.70)=2 Floor(1.70)=1 Round(1.70)=2 (int)(1.70+0.5)=2 Truncate(1.70)=1 Ceiling(1.60)=2 Floor(1.60)=1 Round(1.60)=2 (int)(1.60+0.5)=2 Truncate(1.60)=1 Ceiling(1.50)=2 Floor(1.50)=1 Round(1.50)=1 (int)(1.50+0.5)=1 Truncate(1.50)=1 Ceiling(1.40)=2 Floor(1.40)=1 Round(1.40)=1 (int)(1.40+0.5)=1 Truncate(1.40)=1 Ceiling(1.30)=2 Floor(1.30)=1 Round(1.30)=1 (int)(1.30+0.5)=1 Truncate(1.30)=1 Ceiling(1.20)=2 Floor(1.20)=1 Round(1.20)=1 (int)(1.20+0.5)=1 Truncate(1.20)=1 Ceiling(1.10)=2 Floor(1.10)=1 Round(1.10)=1 (int)(1.10+0.5)=1 Truncate(1.10)=1 Ceiling(1.00)=1 Floor(1.00)=0 Round(1.00)=1 (int)(1.00+0.5)=1 Truncate(1.00)=0 Ceiling(0.90)=1 Floor(0.90)=0 Round(0.90)=1 (int)(0.90+0.5)=1 Truncate(0.90)=0 Ceiling(0.80)=1 Floor(0.80)=0 Round(0.80)=1 (int)(0.80+0.5)=1 Truncate(0.80)=0 Ceiling(0.70)=1 Floor(0.70)=0 Round(0.70)=1 (int)(0.70+0.5)=1 Truncate(0.70)=0 Ceiling(0.60)=1 Floor(0.60)=0 Round(0.60)=1 (int)(0.60+0.5)=1 Truncate(0.60)=0 Ceiling(0.50)=1 Floor(0.50)=0 Round(0.50)=0 (int)(0.50+0.5)=0 Truncate(0.50)=0 Ceiling(0.40)=1 Floor(0.40)=0 Round(0.40)=0 (int)(0.40+0.5)=0 Truncate(0.40)=0 Ceiling(0.30)=1 Floor(0.3You can't make it exact using a floating point data type. A floating poing value is often not exactly the value you specify, as some values can not be represented exactly. For an example, the value 2.00 might actually be 2.000000000000001, and using Math.Ceiling() on that value will result in the value 3.0. --- b { font-weight: normal; }
-
You can't make it exact using a floating point data type. A floating poing value is often not exactly the value you specify, as some values can not be represented exactly. For an example, the value 2.00 might actually be 2.000000000000001, and using Math.Ceiling() on that value will result in the value 3.0. --- b { font-weight: normal; }
I see. So that's say I can not get a very exact float value. But the other question is why the procedure is same, only diffrent between the begining value and loop times, it will make this difrrent. As you said, every time Math.Ceiling(2.00) must equal 3, but why in FunA() it is equal 2. They all are called by Math.Ceiling(value) and the value=2.00, the very small gap can not explain this result. === Game is power! ===
-
I see. So that's say I can not get a very exact float value. But the other question is why the procedure is same, only diffrent between the begining value and loop times, it will make this difrrent. As you said, every time Math.Ceiling(2.00) must equal 3, but why in FunA() it is equal 2. They all are called by Math.Ceiling(value) and the value=2.00, the very small gap can not explain this result. === Game is power! ===
-
I see. So that's say I can not get a very exact float value. But the other question is why the procedure is same, only diffrent between the begining value and loop times, it will make this difrrent. As you said, every time Math.Ceiling(2.00) must equal 3, but why in FunA() it is equal 2. They all are called by Math.Ceiling(value) and the value=2.00, the very small gap can not explain this result. === Game is power! ===