Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #78691 > unrolled thread
| Started by | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| First post | 2014-10-04 10:58 -0400 |
| Last post | 2014-10-05 00:14 -0700 |
| Articles | 8 — 4 participants |
Back to article view | Back to comp.lang.python
A little more: decimal_portion Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-04 10:58 -0400
Re: A little more: decimal_portion Chris Angelico <rosuav@gmail.com> - 2014-10-05 01:07 +1000
Re: A little more: decimal_portion Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-04 11:16 -0400
Re: A little more: decimal_portion Chris Angelico <rosuav@gmail.com> - 2014-10-05 01:24 +1000
Re: A little more: decimal_portion Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-04 11:38 -0400
Re: A little more: decimal_portion Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-05 01:46 +1000
Re: A little more: decimal_portion Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-04 12:11 -0400
Re: A little more: decimal_portion wxjmfauth@gmail.com - 2014-10-05 00:14 -0700
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-10-04 10:58 -0400 |
| Subject | A little more: decimal_portion |
| Message-ID | <2p203aldkhli65bd9crd4ljbfuab0u90oc@4ax.com> |
A little more: decimal_portion
Write a function that takes two number parameters and returns a float
that is the decimal portion of the result of dividing the first
parameter by the second. (For example, if the parameters are 5 and 2,
the result of 5/2 is 2.5, so the return value would be 0.5)
http://imgur.com/a0Csi43
def decimal_portion(a,b):
return float((b/a)-((b//a)))
print (decimal_portion(5,2))
I get 0.4 and the answer is supposed to be 0.5.
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-10-05 01:07 +1000 |
| Message-ID | <mailman.14599.1412435262.18130.python-list@python.org> |
| In reply to | #78691 |
On Sun, Oct 5, 2014 at 12:58 AM, Seymore4Head <Seymore4Head@hotmail.invalid> wrote: > A little more: decimal_portion > > Write a function that takes two number parameters and returns a float > that is the decimal portion of the result of dividing the first > parameter by the second. (For example, if the parameters are 5 and 2, > the result of 5/2 is 2.5, so the return value would be 0.5) > > http://imgur.com/a0Csi43 > > def decimal_portion(a,b): > return float((b/a)-((b//a))) > > print (decimal_portion(5,2)) > > I get 0.4 and the answer is supposed to be 0.5. Work out exactly what your program is doing, step by step. Print out the intermediate steps in the calculation, and compare what the program's doing to what you expect to be happening. What's (b/a)? What's (b//a)? What's (b/a)-((b//a))? ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-10-04 11:16 -0400 |
| Message-ID | <7j303a9e0g7ipe7se3s5qc364nurjrcknp@4ax.com> |
| In reply to | #78692 |
On Sun, 5 Oct 2014 01:07:39 +1000, Chris Angelico <rosuav@gmail.com> wrote: >On Sun, Oct 5, 2014 at 12:58 AM, Seymore4Head ><Seymore4Head@hotmail.invalid> wrote: >> A little more: decimal_portion >> >> Write a function that takes two number parameters and returns a float >> that is the decimal portion of the result of dividing the first >> parameter by the second. (For example, if the parameters are 5 and 2, >> the result of 5/2 is 2.5, so the return value would be 0.5) >> >> http://imgur.com/a0Csi43 >> >> def decimal_portion(a,b): >> return float((b/a)-((b//a))) >> >> print (decimal_portion(5,2)) >> >> I get 0.4 and the answer is supposed to be 0.5. > >Work out exactly what your program is doing, step by step. Print out >the intermediate steps in the calculation, and compare what the >program's doing to what you expect to be happening. What's (b/a)? >What's (b//a)? What's (b/a)-((b//a))? > >ChrisA I did. I included a screenshot of me doing just that. The formula seems to work in the shell, but does not work as a function, or I am missing something subtle again.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-10-05 01:24 +1000 |
| Message-ID | <mailman.14600.1412436289.18130.python-list@python.org> |
| In reply to | #78693 |
On Sun, Oct 5, 2014 at 1:16 AM, Seymore4Head <Seymore4Head@hotmail.invalid> wrote: > I did. I included a screenshot of me doing just that. > The formula seems to work in the shell, but does not work as a > function, or I am missing something subtle again. Ah, I don't generally click screenshots. Have the function print out exactly what it's doing at each step. See how that compares to your hand-worked version. You'll see the problem. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-10-04 11:38 -0400 |
| Message-ID | <v3503atf0jfptrdctsn008eg1sgmhhc43j@4ax.com> |
| In reply to | #78694 |
On Sun, 5 Oct 2014 01:24:40 +1000, Chris Angelico <rosuav@gmail.com> wrote: >On Sun, Oct 5, 2014 at 1:16 AM, Seymore4Head ><Seymore4Head@hotmail.invalid> wrote: >> I did. I included a screenshot of me doing just that. >> The formula seems to work in the shell, but does not work as a >> function, or I am missing something subtle again. > >Ah, I don't generally click screenshots. > >Have the function print out exactly what it's doing at each step. See >how that compares to your hand-worked version. You'll see the problem. > >ChrisA That worked. Thanks
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-10-05 01:46 +1000 |
| Message-ID | <5430163c$0$12998$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #78691 |
Seymore4Head wrote: > A little more: decimal_portion > > Write a function that takes two number parameters and returns a float > that is the decimal portion of the result of dividing the first > parameter by the second. (For example, if the parameters are 5 and 2, > the result of 5/2 is 2.5, so the return value would be 0.5) > > http://imgur.com/a0Csi43 > > def decimal_portion(a,b): > return float((b/a)-((b//a))) > > print (decimal_portion(5,2)) > > I get 0.4 and the answer is supposed to be 0.5. Hint: given arguments a=5, b=2, you want 5/2. What do you calculate? Look at the order of the arguments a and b in the function def line, and in the calculations you perform. Once you fix that, I can suggest a more efficient way of calculating the answer: use the modulo operator % Given arguments 5 and 2, you want 0.5 == 1/2, and 5%2 returns 1. Given arguments 6 and 2, you want 0.0 == 0/2, and 6%2 returns 0. Given arguments 8 and 3, you want 0.6666... == 2/3, and 8%3 returns 2. P.S. the usual name for this function is "fraction part", sometimes "fp". -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-10-04 12:11 -0400 |
| Message-ID | <1q603apbgntesrvcjd9t0ieq5nqa4rs1es@4ax.com> |
| In reply to | #78696 |
On Sun, 05 Oct 2014 01:46:03 +1000, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: >Seymore4Head wrote: > >> A little more: decimal_portion >> >> Write a function that takes two number parameters and returns a float >> that is the decimal portion of the result of dividing the first >> parameter by the second. (For example, if the parameters are 5 and 2, >> the result of 5/2 is 2.5, so the return value would be 0.5) >> >> http://imgur.com/a0Csi43 >> >> def decimal_portion(a,b): >> return float((b/a)-((b//a))) >> >> print (decimal_portion(5,2)) >> >> I get 0.4 and the answer is supposed to be 0.5. > >Hint: given arguments a=5, b=2, you want 5/2. What do you calculate? Look at >the order of the arguments a and b in the function def line, and in the >calculations you perform. > >Once you fix that, I can suggest a more efficient way of calculating the >answer: use the modulo operator % > >Given arguments 5 and 2, you want 0.5 == 1/2, and 5%2 returns 1. > >Given arguments 6 and 2, you want 0.0 == 0/2, and 6%2 returns 0. > >Given arguments 8 and 3, you want 0.6666... == 2/3, and 8%3 returns 2. > > >P.S. the usual name for this function is "fraction part", sometimes "fp". Yeah, I caught my mistake. Thanks for the suggestions. I was expecting a lesson in rounding errors, so I had ruled out the error was on my part. (Something I should never do)
[toc] | [prev] | [next] | [standalone]
| From | wxjmfauth@gmail.com |
|---|---|
| Date | 2014-10-05 00:14 -0700 |
| Message-ID | <47dfa759-6e6b-41c8-9d90-cb974fff43e0@googlegroups.com> |
| In reply to | #78698 |
Le samedi 4 octobre 2014 18:13:27 UTC+2, Seymore4Head a écrit : > On Sun, 05 Oct 2014 01:46:03 +1000, Steven D'Aprano > > <steve+comp.lang.python@pearwood.info> wrote: > > > > >Seymore4Head wrote: > > > > > >> A little more: decimal_portion > > >> > > >> Write a function that takes two number parameters and returns a float > > >> that is the decimal portion of the result of dividing the first > > >> parameter by the second. (For example, if the parameters are 5 and 2, > > >> the result of 5/2 is 2.5, so the return value would be 0.5) > > >> > > >> http://imgur.com/a0Csi43 > > >> > > >> def decimal_portion(a,b): > > >> return float((b/a)-((b//a))) > > >> > > >> print (decimal_portion(5,2)) > > >> > > >> I get 0.4 and the answer is supposed to be 0.5. > > > > > >Hint: given arguments a=5, b=2, you want 5/2. What do you calculate? Look at > > >the order of the arguments a and b in the function def line, and in the > > >calculations you perform. > > > > > >Once you fix that, I can suggest a more efficient way of calculating the > > >answer: use the modulo operator % > > > > > >Given arguments 5 and 2, you want 0.5 == 1/2, and 5%2 returns 1. > > > > > >Given arguments 6 and 2, you want 0.0 == 0/2, and 6%2 returns 0. > > > > > >Given arguments 8 and 3, you want 0.6666... == 2/3, and 8%3 returns 2. > > > > > > > > >P.S. the usual name for this function is "fraction part", sometimes "fp". > > > > Yeah, I caught my mistake. Thanks for the suggestions. > > I was expecting a lesson in rounding errors, so I had ruled out the > > error was on my part. (Something I should never do) %%%%% >>> # hints >>> a = 5 / 2 >>> a - floor(a) 0.5 >>> >>> import math >>> math.modf(a) (0.5, 2.0) >>> math.modf(pi) (0.14159265358979312, 3.0) >>>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web