449,312 Members | 1,686 Online Need help? Post your question and get tips & solutions from a community of 449,312 IT Pros & Developers. It's quick & easy.

# Problem in Float Arithmetic

 P: n/a Hi All, I am getting exponent value when doing float arithmetic in C++. Instead of that I need accurate value. float amount = 0.0f; float x = 0.99999976f; amount= 1.0f - x; I am getting amount as 2.3841858e-007 instead of 0.00000024 Can anyone tell what will be the solution for this problem??? Note : I am getting correct value in C#.Net but not in C++ Thanks in advance. Regards, K Jun 27 '08 #1
5 Replies

 P: n/a On Mon, 23 Jun 2008 04:51:40 -0700, Selvam wrote: Hi All, I am getting exponent value when doing float arithmetic in C++. Instead of that I need accurate value. float amount = 0.0f; float x = 0.99999976f; amount= 1.0f - x; I am getting amount as 2.3841858e-007 instead of 0.00000024 Can anyone tell what will be the solution for this problem??? Note : I am getting correct value in C#.Net but not in C++ See this FAQ: http://www.parashift.com/c++-faq-lit...html#faq-29.16 Regards, -- Lionel B Jun 27 '08 #2

 P: n/a Selvam

 P: n/a On Mon, 23 Jun 2008 14:15:22 +0200, Pascal J. Bourguignon wrote: Selvam Hi All,I am getting exponent value when doing float arithmetic in C++. Insteadof that I need accurate value.float amount = 0.0f;float x = 0.99999976f;amount= 1.0f - x;I am getting amount as 2.3841858e-007 instead of 0.00000024 [...] The bug you made is here: float amount; ^^^^^ ^^^^^^ Amounts ARE NOT floating point numbers. You should NEVER use float(-ing point) numbers in financial applications. Where did the OP say "financial application"? There are amounts of other stuff besides money, surely ;-) Aside from which, I'm not convinced that's realistic advice (I once programmed for a financial institution where we worked quite happily in floating point - paying very careful attention to precision and rounding, of course). If you want exact arithmetic, use integers (and rationals or fixed-point numbers). http://gmplib.org/ Maybe - but seeing as we don't know what the problem domain of the OP is, this may not be the way to go. >Note : I am getting correct value in C#.Net but not in C++ Only because C#{float} == C++{double}. Try: float weight = 0.0f; float x = 0.999999999999999976f; weight= 1.0f - x; in C#. (Notice how I changed the application domain, from amounts to weight, to make it a correct program). You've changed the application domain? You must be psychic. Personally I've no idea what the OP's application domain might be. -- Lionel B Jun 27 '08 #4

 P: n/a Selvam wrote: Instead of that I need accurate value. Then don't use floating point numbers. float amount = 0.0f; float x = 0.99999976f; amount= 1.0f - x; I am getting amount as 2.3841858e-007 instead of 0.00000024 Using 'double' instead of 'float' may decrease the rounding errors, but probably won't remove them. Jun 27 '08 #5

 P: n/a On Jun 23, 1:51 pm, Selvam

### This discussion thread is closed

Replies have been disabled for this discussion. 