public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: g++/gcc : Floating point issue
@ 2006-07-26 15:17 Neil Ferguson
  0 siblings, 0 replies; 5+ messages in thread
From: Neil Ferguson @ 2006-07-26 15:17 UTC (permalink / raw)
  To: gcc-help

This is not a GCC issue.

The 'double' type has a finite precision. Your number is almost certainly
being approximated to the nearest representable value.

The only ways to deal with this are to:
(1) arrange your calculations so that finite precision is not an issue, or
(2) use a different data format.

If you're interested in option (2), GMP might meet your needs:

     http://www.swox.com/gmp/

Neil.

abuzer.salik@tcs.com wrote:
> Hi,
> 
> I have an application which uses double values a lot. If I try to put say
> 401.74 in a double variable it is getting stored as 401.74000000000001
> which creates problem in the calculations down the line. ( I am checking
> these values using gdb debugger).
> 
> So, I am wondering if there is a way by which I can store the correct
> values (i.e. 401.74) in the double.
> 
> We are using g++ compiler on Linux box.
> 
> Do we need to look for another compiler/hardware or current setup can
> handle this?
> 
> Any suggestion/solution is highly appreciated.
> 
> Regards,
> Abuzer Salik

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: g++/gcc : Floating point issue
  2006-07-26 14:53 abuzer.salik
  2006-07-26 15:03 ` Daniel Llorens del Río
@ 2006-07-28 19:56 ` miridian
  1 sibling, 0 replies; 5+ messages in thread
From: miridian @ 2006-07-28 19:56 UTC (permalink / raw)
  To: gcc-help

abuzer.salik@tcs.com wrote:

> I have an application which uses double values a lot. If I try to put say
> 401.74 in a double variable it is getting stored as 401.74000000000001
> which creates problem in the calculations down the line. ( I am checking
> these values using gdb debugger).
> 
> So, I am wondering if there is a way by which I can store the correct
> values (i.e. 401.74) in the double.

No.

Only dyadic rationals (a rational number of the form a/(2^b) where a is 
an integer and b is a natural number) have a finite binary expansion.

   40174/100 = 200087/50    (and 50 is not 2^b for a natural number b)

See the links:
http://en.wikipedia.org/wiki/Binary_numeral_system#Representing_real_numbers
http://en.wikipedia.org/wiki/Dyadic_fraction


> Do we need to look for another compiler/hardware or current setup can
> handle this?

You can use an arbitrary precision numeric library, for ex. GMP
  http://www.swox.com/gmp/

Regars,
Luca.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: g++/gcc : Floating point issue
@ 2006-07-26 15:36 Bill McEnaney
  0 siblings, 0 replies; 5+ messages in thread
From: Bill McEnaney @ 2006-07-26 15:36 UTC (permalink / raw)
  To: abuzer.salik, gcc-help

Well, friend, I wish I knew how to solve your problem.  Sadly, I doubt
that it's solvable.  From what I've read, it seems that floating point
numbers are seldom exact.  One book, Michael Covington's "Prolog in
Depth," I think, I read that since floating point numbers are often not
equal to each other, it's a good idea not to test them for equality.  It
seems better to test them using >=, <=, or !=.

Some author, perhaps Covington. wrote that some Prolog programs consider
two floating point numbers equal when the difference between them is
less than some tolerance.

I suspect your problem arises because word lengths limit the the
precision of floating point numbers that a computer can represent. 
There are other problems, too, I'm sure.

I've heard of some programming languages that allow floating point
numbers that can have almost unlimited precision because those languages
represent floating point numbers as lists of integers or as integers in
binary coded decimal form.  Hope this helps.

Bill
Abuzer Salik writes:

Hi,
 
I have an application which uses double values a lot. If I try to put
say 401.74 in a double variable it is getting stored as
401.74000000000001 which creates problem in the calculations down the
line. ( I am checking these values using gdb debugger).
 
So, I am wondering if there is a way by which I can store the correct
values (i.e. 401.74) in the double.

We are using g++ compiler on Linux box.
 
Do we need to look for another compiler/hardware or current setup can
handle this?

Any suggestion/solution is highly appreciated.
 
"Pro-choice?"  Then click here.
http://cathinsight.com/morality/saying.htm

"Men must look for the peace of Christ in the Kingdom of Christ... When
once men recognize, both in private and in public life, that Christ is
King, society will at last receive the great blessings of real liberty,
well-ordered discipline, peace and harmony."  Pope Pius XI

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: g++/gcc : Floating point issue
  2006-07-26 14:53 abuzer.salik
@ 2006-07-26 15:03 ` Daniel Llorens del Río
  2006-07-28 19:56 ` miridian
  1 sibling, 0 replies; 5+ messages in thread
From: Daniel Llorens del Río @ 2006-07-26 15:03 UTC (permalink / raw)
  To: gcc-help


On 26 Jul, 2006, at 16:52, abuzer.salik@tcs.com wrote:

> Hi,
>
> I have an application which uses double values a lot. If I try to  
> put say
> 401.74 in a double variable it is getting stored as 401.74000000000001
> which creates problem in the calculations down the line. ( I am  
> checking
> these values using gdb debugger).
>
> So, I am wondering if there is a way by which I can store the correct
> values (i.e. 401.74) in the double.

Use a library of rationals if you need exact values, I think GMP  
provides one.

> Do we need to look for another compiler/hardware or current setup can
> handle this?

As long as you use doubles, which are tied to the hardware, you'll  
have the same problem. The precision is more or less standard across  
architectures. IOW it has little to do with the compiler.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* g++/gcc : Floating point issue
@ 2006-07-26 14:53 abuzer.salik
  2006-07-26 15:03 ` Daniel Llorens del Río
  2006-07-28 19:56 ` miridian
  0 siblings, 2 replies; 5+ messages in thread
From: abuzer.salik @ 2006-07-26 14:53 UTC (permalink / raw)
  To: gcc-help

Hi,

I have an application which uses double values a lot. If I try to put say
401.74 in a double variable it is getting stored as 401.74000000000001
which creates problem in the calculations down the line. ( I am checking
these values using gdb debugger).

So, I am wondering if there is a way by which I can store the correct
values (i.e. 401.74) in the double.

We are using g++ compiler on Linux box.

Do we need to look for another compiler/hardware or current setup can
handle this?

Any suggestion/solution is highly appreciated.

Regards,
Abuzer Salik
=====-----=====-----=====
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2006-07-28 19:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-26 15:17 g++/gcc : Floating point issue Neil Ferguson
  -- strict thread matches above, loose matches on Subject: below --
2006-07-26 15:36 Bill McEnaney
2006-07-26 14:53 abuzer.salik
2006-07-26 15:03 ` Daniel Llorens del Río
2006-07-28 19:56 ` miridian

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).