Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

lambda with floats

1 view
Skip to first unread message

monkeys paw

unread,
Apr 6, 2010, 11:16:18 PM4/6/10
to
I have the following acre meter which works for integers,
how do i convert this to float? I tried

return float ((208.0 * 208.0) * n)

>>> def s(n):
... return lambda x: (208 * 208) * n
...
>>> f = s(1)
>>> f(1)
43264
>>> 208 * 208
43264
>>> f(.25)
43264

Patrick Maupin

unread,
Apr 7, 2010, 12:04:45 AM4/7/10
to

Not sure why you are returning a lambda (which is just a function that
does not have a name) from an outer function.

A function that does this multiplication would simply be:

def s(n):
return 208.0 * 208.0 * n

Regards,
Pat

Patrick Maupin

unread,
Apr 7, 2010, 12:10:42 AM4/7/10
to


I realized I didn't show the use. A bit different than what you were
doing:

>>> def s(n):
... return 208.0 * 208.0 * n
...
>>> s(1)
43264.0
>>> s(0.5)
21632.0
>>> s(3)
129792.0


I'm not sure exactly what you mean by "acre meter" though; this
returns the number of square feet in 'n' acres.

Regards,
Pat

Patrick Maupin

unread,
Apr 7, 2010, 12:15:46 AM4/7/10
to

I should stop making a habit of responding to myself, BUT. This isn't
quite an acre in square feet. I just saw the 43xxx and assumed it
was, and then realized it couldn't be, because it wasn't divisible by
10. (I used to measure land with my grandfather with a 66 foot long
chain, and learned at an early age that an acre was 1 chain by 10
chains, or 66 * 66 * 10 = 43560 sqft.)
That's an exact number, and 208 is a poor approximation of its square
root.

Regards,
Pat

Peter Pearson

unread,
Apr 7, 2010, 1:08:04 PM4/7/10
to

The expression "lambda x: (208 * 208) * n" is independent of x.
Is that what you intended?


--
To email me, substitute nowhere->spamcop, invalid->net.

monkeys paw

unread,
Apr 8, 2010, 7:06:47 PM4/8/10
to
On 4/7/2010 1:08 PM, Peter Pearson wrote:
> On Tue, 06 Apr 2010 23:16:18 -0400, monkeys paw<mon...@joemoney.net> wrote:
>> I have the following acre meter which works for integers,
>> how do i convert this to float? I tried
>>
>> return float ((208.0 * 208.0) * n)
>>
>>>>> def s(n):
>> ... return lambda x: (208 * 208) * n
>> ...
>>>>> f = s(1)
>>>>> f(1)
>> 43264
>>>>> 208 * 208
>> 43264
>>>>> f(.25)
>> 43264
>
> The expression "lambda x: (208 * 208) * n" is independent of x.
> Is that what you intended?
>
>

Seems i should have done this:
g = lambda x: 208.0 * 208.0 * x
g(1)
43264.0

Patrick Maupin

unread,
Apr 8, 2010, 7:19:08 PM4/8/10
to

Yes, but then what is the 'n' for. When you do that, you are not
using it, and it is still confusing.

Regards,
Pat

monkeys paw

unread,
Apr 8, 2010, 9:32:10 PM4/8/10
to

I was going from example and looking for something useful from
the lambda feature. I come from C -> Perl -> Python (recent). I
don't find lambda very useful yet.

monkeys paw

unread,
Apr 8, 2010, 11:02:40 PM4/8/10
to

You are absolutely right Pat, so here is the correct equate which also
utilizes my original question of using floats in a lambda, perfectly...

g = lambda x: 208.71 * 208.71 * x
g(1)
43559.864100000006

but truly the easiest to remember is based on your chain:

g = lambda x: 660 * 66 * x
g(1)
43560

Now back to python...

Steven D'Aprano

unread,
Apr 9, 2010, 12:31:56 AM4/9/10
to
On Thu, 08 Apr 2010 21:32:10 -0400, monkeys paw wrote:

> I was going from example and looking for something useful from the
> lambda feature. I come from C -> Perl -> Python (recent). I don't find
> lambda very useful yet.

Perhaps you feel that lambda is a special kind of object. It isn't. It's
just a short-cut for creating an anonymous function object.

f = lambda x: x+1

is almost exactly the same as:

def function(x):
return x+1

f = function
del function


The only advantages of lambda are:

(1) you can write a simple function as a one-liner; and
(2) it's an expression, so you can embed it in another expression:

list_of_functions = [math.sin, lambda x: 2*x-1, lambda x, y=1: x**y]
for func in list_of_functions:
plot(func)


The disadvantage of lambda is that you can only include a single
expression as the body of the function.

You will generally find lambdas used as callback functions, and almost
nowhere else.


--
Steven

Bas

unread,
Apr 9, 2010, 3:43:48 AM4/9/10
to
On Apr 7, 6:15 am, Patrick Maupin <pmau...@gmail.com> wrote:
> I should stop making a habit of responding to myself, BUT.  This isn't
> quite an acre in square feet.  I just saw the 43xxx and assumed it
> was, and then realized it couldn't be, because it wasn't divisible by
> 10.  (I used to measure land with my grandfather with a 66 foot long
> chain, and learned at an early age that an acre was 1 chain by 10
> chains, or 66 * 66 * 10 = 43560 sqft.)
> That's an exact number, and 208 is a poor approximation of its square
> root.

There is no need to remember those numbers for the imperially
challenged people:

In [1]: import scipy.constants as c

In [2]: def acre2sqft(a):
...: return a * c.acre / (c.foot * c.foot)
...:

In [3]: acre2sqft(1)
Out[3]: 43560.0


Cheers,
Bas

monkeys paw

unread,
Apr 9, 2010, 2:22:46 PM4/9/10
to
On 4/9/2010 3:43 AM, Bas wrote:
> On Apr 7, 6:15 am, Patrick Maupin<pmau...@gmail.com> wrote:
>> I should stop making a habit of responding to myself, BUT. This isn't
>> quite an acre in square feet. I just saw the 43xxx and assumed it
>> was, and then realized it couldn't be, because it wasn't divisible by
>> 10. (I used to measure land with my grandfather with a 66 foot long
>> chain, and learned at an early age that an acre was 1 chain by 10
>> chains, or 66 * 66 * 10 = 43560 sqft.)
>> That's an exact number, and 208 is a poor approximation of its square
>> root.
>
> There is no need to remember those numbers for the imperially
> challenged people:
>
> In [1]: import scipy.constants as c

scipy.constants ??

doesn't work for me.

Patrick Maupin

unread,
Apr 9, 2010, 2:40:35 PM4/9/10
to

Basically, he's saying that, instead of remembering the very simple
"66" and "10" values, you can download and install a multi-megabyte
gzipped tar file for the scipy project. ;-)

(Of course, you get a few nice functions thrown in for free along with
your constants, but downloading scipy for its constants is like
choosing a sports car for its cupholders.)

monkeys paw

unread,
Apr 9, 2010, 3:03:13 PM4/9/10
to

yea, the 66 foot chain story is a good one, i cant forget that.
Appreciate the help, i just looked up the SciPY Project download,
it is 40 MB. I'm gonna check it out none the less...

Albert van der Horst

unread,
Apr 15, 2010, 11:55:00 AM4/15/10
to
In article <3790faeb-ae4c-4b45...@k13g2000yqe.googlegroups.com>,
Patrick Maupin <pma...@gmail.com> wrote:
>On Apr 6, 11:10=A0pm, Patrick Maupin <pmau...@gmail.com> wrote:
>> On Apr 6, 11:04=A0pm, Patrick Maupin <pmau...@gmail.com> wrote:

>>
>>
>>
>> > On Apr 6, 10:16=A0pm, monkeys paw <mon...@joemoney.net> wrote:
>>
>> > > I have the following acre meter which works for integers,
>> > > how do i convert this to float? I tried
>>
>> > > return float ((208.0 * 208.0) * n)
>>
>> > > =A0>>> def s(n):
>> > > ... =A0 =A0 return lambda x: (208 * 208) * n
>> > > ...
>> > > =A0>>> f =3D s(1)
>> > > =A0>>> f(1)
>> > > 43264
>> > > =A0>>> 208 * 208
>> > > 43264
>> > > =A0>>> f(.25)

>> > > 43264
>>
>> > Not sure why you are returning a lambda (which is just a function that
>> > does not have a name) from an outer function.
>>
>> > A function that does this multiplication would simply be:
>>
>> > def s(n):
>> > =A0 =A0 return 208.0 * 208.0 * n
>>
>> > Regards,
>> > Pat
>>
>> I realized I didn't show the use. =A0A bit different than what you were
>> doing:
>>
>> >>> def s(n):
>>
>> ... =A0 =A0 return 208.0 * 208.0 * n

>> ...>>> s(1)
>> 43264.0
>> >>> s(0.5)
>> 21632.0
>> >>> s(3)
>>
>> 129792.0
>>
>> I'm not sure exactly what you mean by "acre meter" though; this
>> returns the number of square feet in 'n' acres.
>>
>> Regards,
>> Pat
>
>I should stop making a habit of responding to myself, BUT. This isn't
>quite an acre in square feet. I just saw the 43xxx and assumed it
>was, and then realized it couldn't be, because it wasn't divisible by
>10. (I used to measure land with my grandfather with a 66 foot long
>chain, and learned at an early age that an acre was 1 chain by 10
>chains, or 66 * 66 * 10 =3D 43560 sqft.)

>That's an exact number, and 208 is a poor approximation of its square
>root.

208 = 2^6 * 13^2

66 = 2 * 3 * 11

Now I understand why the state of Wisconsin wants to drop out of
the USA and join the EU ;-)

>
>Regards,
>Pat

Groetjes Albert

--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

Message has been deleted
0 new messages