Article View: comp.std.c
Article #2151Re: Change to time() fn?
From: Bernard Fouche
Date: Sat, 26 Sep 1992 15:34
Date: Sat, 26 Sep 1992 15:34
62 lines
2854 bytes
2854 bytes
In article <1992Sep17.120636.29070@linus.mitre.org>, jrv@truth.uucp (Vanzandt) writes:
> It seems to me that time_t should be a double. It would allow representation
> of any time during the life of the universe, with good resolution (microseconds)
> for times near the present. I haven't seen a copy of the standard. Would
> such an implementation conform? Surely it's possible for a conforming
> implementation to represent my birthday? (hint: before 1970). (There were
> a few astronomical events of interest before 1970, too.)
That's wrong. Using a floating point representation for precise data
like this is very dangerous.
I compiled this code on a 486 running Dell Unix System V R4.0 :
main()
{
double a=1000000000000000.0;
double b=10000000000000000.0;
printf("a=%f.\n",a);
a+=1;
printf("a=%f.\n",a);
printf("b=%f.\n",b);
b+=1;
printf("b=%f.\n",b);
}
Here are the results :
a=1000000000000000.000000.
a=1000000000000001.000000.
b=10000000000000000.000000.
b=10000000000000000.000000.
As you see, "1" is too "far away" from b value to be taken into
consideration by the add operation : adding it to b doesn't change the
mantissa value. If the floating poing mecanism can manage numbers of a
huge size, it's by loosing accuracy is some situations.
To use floating points numbers without understanding this, is often a
way to produce wrong results without knowing why. (for instance
designing a financial package built over floating point numbers with a
small mantissa).
The solution is to use what's call "big numbers" : numbers that can
allow you to perform your calculations of nearly any types with
accuracy but are not (yet ?) directly known by the micro-processor, as
integers are or as (when using a math co-processor) floating points
numbers are.
When you use "big numbers", you are trading speed (you need to use
librairies and not directly processor's intructions to manipulate the
date) for accuracy.
--
--------------------------------------------------------------------------
Bernard Fouche VOX : +(33) (1) 48.04.56.30 FAX : (33) (1) 48.04.98.95
Email : bernard@cpio1.fr.mugnet.org
Postal Mail : CPIO S.A., 13-15 Rue de la Verrerie, 75004 Paris, France
Technical Details
Message-ID:
<1992Sep26.153421.7698@cpio1.fr.mugnet.org>Path:
arc1-ca.pugleaf.net!sparky!uunet!paladin.american.edu!europa.asd.contel.com!darwin.sura.net!haven.umd.edu!mimsy!nmrdc1!frmug!cpio1!bernardReferences:
<1992Sep12.140423.23672@iqsc.COM> <1992Sep13.184530.27370@ima.isc.com> <1992Sep15.042120.27559@nntpd.lkg.dec.com> <1992Sep17.120636.29070@linus.mitre.org>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!paladin.american.edu!europa.asd.contel.com!darwin.sura.net!haven.umd.edu!mimsy!nmrdc1!frmug!cpio1!bernard From: bernard@cpio1.fr.mugnet.org (Bernard Fouche) Newsgroups: comp.std.c Subject: Re: Change to time() fn? Message-ID: <1992Sep26.153421.7698@cpio1.fr.mugnet.org> Date: 26 Sep 92 15:34:21 GMT References: <1992Sep12.140423.23672@iqsc.COM> <1992Sep13.184530.27370@ima.isc.com> <1992Sep15.042120.27559@nntpd.lkg.dec.com> <1992Sep17.120636.29070@linus.mitre.org> Sender: bernard@cpio1.fr.mugnet.org (Bernard Fouche) Organization: CPIO S.A. Lines: 62