Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem in Float Arithmetic

Reply
Thread Tools

Problem in Float Arithmetic

 
 
Selvam
Guest
Posts: n/a
 
      06-23-2008
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

 
Reply With Quote
 
 
 
 
Lionel B
Guest
Posts: n/a
 
      06-23-2008
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
 
Reply With Quote
 
 
 
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      06-23-2008
Selvam <(E-Mail Removed)> writes:

> 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


Have a look at:
"What Every Computer Scientist Should Know About Floating-Point Arithmetic"
http://focus.hut.fi/docs/WorkShop/co...berg1.doc.html
http://docs-pdf.sun.com/800-7895/800-7895.pdf
http://portal.acm.org/citation.cfm?id=103163

> Can anyone tell what will
> be the solution for this problem???


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.

If you want exact arithmetic, use integers (and rationals or
fixed-point numbers). http://gmplib.org/


> 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).

--
__Pascal Bourguignon__
 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      06-23-2008
On Mon, 23 Jun 2008 14:15:22 +0200, Pascal J. Bourguignon wrote:

> Selvam <(E-Mail Removed)> writes:
>
>> 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


[...]

> 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
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      06-23-2008
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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-23-2008
On Jun 23, 1:51 pm, Selvam <(E-Mail Removed)> wrote:

> I am getting exponent value when doing float arithmetic in
> C++. Instead of that I need accurate value.


I'm not sure what you mean by "accurate value". You're
probablyl getting a reasonably accurate value in the results,
but just not formatting it correctly.

> float amount = 0.0f;
> float x = 0.99999976f;
> amount= 1.0f - x;


> I am getting amount as 2.3841858e-007 instead of 0.00000024


Which is probably more accurate. (Note that despite what you
wrote, you certainly never had a float value of 0.99999976.) Of
course, if what you want is the value rounded to 8 points after
the decimal, then you have to tell the computer to output it
like that:
std::cout.setf( std::ios::fixed, std::ios::floatfield ) ;
std::cout.precision( 8 ) ;
std::cout << x ;
Typically, of course, you'll wrap those first two statements in
some sort of application specific manipulator, and write
something like:

std::cout << appliFmt << x ;

(Also: float is only good for six or seven decimal digits
accuracy; you probably want to consider double.)

> Can anyone tell what will be the solution for this problem???


> Note : I am getting correct value in C#.Net but not in C++


I'm pretty sure that you're getting the same results in both
cases. But the default formatting isn't necessarily the same.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float point arithmetic a-a != 0.0 Sean C Programming 12 03-09-2010 07:22 PM
Float Arithmetic: Return A Fixnum When Float == Fixnum MaggotChild Ruby 6 12-02-2009 04:28 AM
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments