* Decimals after the point @ 2002-11-06 23:01 Genevieve B. 2002-11-06 22:08 ` Decimals after the point (school question) Larry Kilgallen ` (2 more replies) 0 siblings, 3 replies; 7+ messages in thread From: Genevieve B. @ 2002-11-06 23:01 UTC (permalink / raw) 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... Thank you ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Decimals after the point (school question) 2002-11-06 23:01 Decimals after the point Genevieve B. @ 2002-11-06 22:08 ` Larry Kilgallen 2002-11-07 2:06 ` Decimals after the point Dennis Lee Bieber 2002-11-09 5:08 ` SteveD 2 siblings, 0 replies; 7+ messages in thread From: Larry Kilgallen @ 2002-11-06 22:08 UTC (permalink / raw) In article <4bhy9.52855$T_3.627411@wagner.videotron.net>, "Genevieve B." <geneb@videotron.ca> writes: > 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... What part of the bonus do we get to share ? ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Decimals after the point 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 2002-11-08 3:50 ` Steven Deller 2002-11-09 5:08 ` SteveD 2 siblings, 1 reply; 7+ messages in thread From: Dennis Lee Bieber @ 2002-11-07 2:06 UTC (permalink / raw) 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/ < ^ permalink raw reply [flat|nested] 7+ messages in thread
* RE: Decimals after the point 2002-11-07 2:06 ` Decimals after the point Dennis Lee Bieber @ 2002-11-08 3:50 ` Steven Deller 0 siblings, 0 replies; 7+ messages in thread From: Steven Deller @ 2002-11-08 3:50 UTC (permalink / raw) Actually, for any binary representation, the only "2 decimal digit" values are those for which all bits after the first two fractional bits are zeroes, yielding all numbers that are some integer plus: 0.00, 0.25, 0.5, 0.75 The reason for this is that 2**N divides into 10**2 evenly only for N=0,1,2. So rather than test the decimal value, you only need to check the fractional part for *equality* with the above values. Normally equality testing with floating point values is not advisable, but it is called for here. I'll leave how to capture the fractional part of a floating point value and how to test it to you. Hint: Read the Ada manual attributes section. Regards, Steve Deller > -----Original Message----- > From: comp.lang.ada-admin@ada.eu.org > [mailto:comp.lang.ada-admin@ada.eu.org] On Behalf Of Dennis Lee Bieber > Sent: Wednesday, November 06, 2002 8:06 PM > To: comp.lang.ada@ada.eu.org > Subject: Re: Decimals after the point > > > 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 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Decimals after the point 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 ` Decimals after the point Dennis Lee Bieber @ 2002-11-09 5:08 ` SteveD 2002-11-18 1:35 ` Richard Riehle 2 siblings, 1 reply; 7+ messages in thread From: SteveD @ 2002-11-09 5:08 UTC (permalink / raw) "Genevieve B." <geneb@videotron.ca> wrote in message news:4bhy9.52855$T_3.627411@wagner.videotron.net... > 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... > I like your approach: Straightforward. Easy to understand. Easy to verify. Gets the job done. SteveD > Thank you > > > ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Decimals after the point 2002-11-09 5:08 ` SteveD @ 2002-11-18 1:35 ` Richard Riehle 2002-11-18 5:17 ` Dennis Lee Bieber 0 siblings, 1 reply; 7+ messages in thread From: Richard Riehle @ 2002-11-18 1:35 UTC (permalink / raw) SteveD wrote: > "Genevieve B." <geneb@videotron.ca> wrote in message > > > 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... > > > I like your approach: > Straightforward. > Easy to understand. > Easy to verify. > Gets the job done. I am reminded of a project that required reading floating point numbers from a VAX on an another computer. The project involved data from a site that had just lost a contract and they had to send the data to us. The sign bit for the VAX is smack-dab in the middle of the 32 bit word. Our machine had the sign bit in the high-order bit. We first struggled with a alorithm to convert VAX floats to our machine's floats. Then we abandoned that. Instead, we bought time on a VAX, converted all the VAX float to textual representation, and then read that into our system. This can be done so easily with Text_IO. The amazing thing is that many Ada programmers still don't know this and I still see some of them trying to write their own algorithms to convert floating point values to strings and vice-versa. Richard Riehle ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Decimals after the point 2002-11-18 1:35 ` Richard Riehle @ 2002-11-18 5:17 ` Dennis Lee Bieber 0 siblings, 0 replies; 7+ messages in thread From: Dennis Lee Bieber @ 2002-11-18 5:17 UTC (permalink / raw) Richard Riehle fed this fish to the penguins on Sunday 17 November 2002 05:35 pm: > I am reminded of a project that required reading floating point > numbers from a VAX on an another computer. The project > involved data from a site that had just lost a contract and > they had to send the data to us. The sign bit for the VAX > is smack-dab in the middle of the 32 bit word. Our machine > had the sign bit in the high-order bit. > Heh... E-Float, D-Float, G-Float, or H-Float <G> -- > ============================================================== < > 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/ < ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2002-11-18 5:17 UTC | newest] Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 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 ` Decimals after the point Dennis Lee Bieber 2002-11-08 3:50 ` Steven Deller 2002-11-09 5:08 ` SteveD 2002-11-18 1:35 ` Richard Riehle 2002-11-18 5:17 ` Dennis Lee Bieber
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox