From: Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject: Re: Decimals after the point
Date: Wed, 06 Nov 2002 18:06:00 -0800
Date: 2002-11-07T02:44:40+00:00 [thread overview]
Message-ID: <9qhcqa.bp3.ln@beastie.ix.netcom.com> (raw)
In-Reply-To: 4bhy9.52855$T_3.627411@wagner.videotron.net
Genevieve B. fed this fish to the penguins on Wednesday 06 November
2002 03:01 pm:
> Hi!
>
> For a project at university, there is a bonus that is to check for
> only 2 decimals after the point in a float.
> does anybody have an idea?
>
> I have an idea of putting my float into a string instead and then
> check for the point...and then check for only 2 numbers after it...
>
Probably not valid -- after all, you could code the float->string
write operation to only write two decimal places... Hence you'd always
see just two decimals <G>
However, the assignment is rather error-prone, due to the problems of
decimal <> binary conversions. What was input as 1.40 may be output as
1.3999999. The assignment should properly specify what behavior is to
be taken in such a case.
{The following is from a Python interpreter -- float is double <G>}
>>> a = 1.40
>>> a
1.3999999999999999
>>> print "%4f" % a
1.400000
>>> print "%7f" % a
1.400000
>>> print "%7.7f" % a
1.4000000
>>> print "%10.9f" % a
1.400000000
>>> print "%10.10f" % a
1.4000000000
>>> print "%16.15f" % a
1.400000000000000
>>> print "%16.16f" % a
1.3999999999999999
>>> print "%20.16f" % a
1.3999999999999999
So what is the correct output for your assignment? The internal (and
16 decimal place representation) turned 1.40 -> 1.399999999999, so
effectively, 1.40 does NOT have only two decimal places. How close do
two floating point numbers have to be to be accepted as "the same".
--
> ============================================================== <
> wlfraed@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
> wulfraed@dm.net | Bestiaria Support Staff <
> ============================================================== <
> Bestiaria Home Page: http://www.beastie.dm.net/ <
> Home Page: http://www.dm.net/~wulfraed/ <
next prev parent reply other threads:[~2002-11-07 2:06 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-11-06 23:01 Decimals after the point Genevieve B.
2002-11-06 22:08 ` Decimals after the point (school question) Larry Kilgallen
2002-11-07 2:06 ` Dennis Lee Bieber [this message]
2002-11-08 3:50 ` Decimals after the point Steven Deller
2002-11-09 5:08 ` SteveD
2002-11-18 1:35 ` Richard Riehle
2002-11-18 5:17 ` Dennis Lee Bieber
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox