This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

c++/4341: g++ built-in type double/float looses precision



>Number:         4341
>Category:       c++
>Synopsis:       g++ built-in type double/float looses precision
>Confidential:   no
>Severity:       critical
>Priority:       high
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Mon Sep 17 19:16:00 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Steve R. Johnson
>Release:        3.0.1 and below
>Organization:
>Environment:
RedHat Linux 7.0 + manual compiliations
>Description:
Variables of float or double types cannot be assigned to
some numbers properly.  For example:
double	db=36.3916;
It will actually assign to some arbitray infinite number like
36.3915999999999999999999999999999974327894329873428973421
2341897234198742389723418792341904231890423189034128904231
4389745378945392378943908757534278543343453490343456476...
I found this out by doing a cout with a precision of 15 of
the origional variable, except that it doesn't print out
the random garbage.  I didn't set the precision that high.

I wanted to perform an algorithm that will determine the
size of a double-type number.  This can be done by dividing
the number by the appropriate number of times by 10 to make
it .363916.  Then, in a loop, casts the db into a long
variable.
long lg=db  //lg=0 when db=0.363916
and moves the decimal to the right by 1 until lg==db;
lg=3		<	db=3.63916
lg=36		<	db=36.3916
lg=363		<	db=363.916
lg=3639		<	db=3639.16
lg=36391	<	db=36391.6
lg=363916	==	db=363916	//loop should terminate
But, this last condition will never occur because db is
really 363915999999999999999999898349340540954...
This type of algorithm will create an infinite loop.
Some numbers will do this in float too, but the same number
will not necessarily have this problem in both float and
double.
>How-To-Repeat:
If you wish for me to write a valid algorithm that is
effected by this bug, e-mail me and let me know.  Not all
floating point numbers have this bug.
>Fix:
None that I found yet.
>Release-Note:
>Audit-Trail:
>Unformatted:


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]