2

I can not understand why is done differently. Example below.

float val = 0.94f;
int i = (int) ( val * 100);

The result of 93?

float val = 0.94f;
float val1 = val * 100;
int i = (int) (val1);

The result of 94?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Check your code - ( * val 100) – JGPhilip Sep 02 '15 at 10:11
  • float is the problem, `(int)(0.94 * 100);` works as expected but not `(int)(0.94f * 100);` – Tim Schmelter Sep 02 '15 at 10:17
  • Strangely enough: In a Debug build you get the "wrong" behaviour, in a Release build, all is well. This is most likely due to the fact that the compiler optimizes both versions to the same code in a Release build. – Jens Sep 02 '15 at 10:18
  • http://stackoverflow.com/questions/753948/why-is-floating-point-arithmetic-in-c-sharp-imprecise , similar answer the problem is float that has bad precision and mixing it with int isn't making it better. – Thorarins Sep 02 '15 at 10:26
  • Use *rounding up*, not *truncation*: `int i = (int) ( val * 100 + 0.5);` note `+ 0.5` – Dmitry Bychenko Sep 02 '15 at 10:29
  • use double , if you want to solve the problem , but I guess this is a more theoretical question? – Thorarins Sep 02 '15 at 10:30
  • @Thorarins: `Double` *doesn't solve* the problem: `0.93999999f` or `0.9399999999999999d` cause the same error. Use *rounding* (i.e. `+0.5`) not *truncation* – Dmitry Bychenko Sep 02 '15 at 11:08
  • yes rounding is the way to go , but for this case double solves it – Thorarins Sep 02 '15 at 11:22

0 Answers0