Re: Friday Afternoon Puzzler [message #66826] |
Fri, 12 June 2009 15:40 |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
Kenneth P. Bowman wrote:
> In article <MPG.249c6dccba4246d098a6e1@news.giganews.com>,
> David Fanning <news@dfanning.com> wrote:
>
>> Folks,
>>
>> If you've already had a beer, you may want to work
>> on this one. Should we be concerned? Or is this just
>> one more example for the Sky is Falling article?
>> I've voiced my opinion, but it didn't satisfy the
>> complainant. What say you?
>>
>> curly=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>> larry=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>> curly = curly*!PI/180.
>> larry *= !PI/180.
>> diff=curly-larry
>> print,diff
>>
>> Why aren't the first set of differences (where the assignment
>> is made with the *= operator) *exactly* zero?
>>
>> Cheers,
>>
>> David
>
> My guess is that the compiler/interpreter does the multiplication and
> division operations in a different order in the two statements in the
> first case. The other two cases are syntactically identical, so the
> operations should be carried out in the same order.
Yes, adding some parentheses makes the results agree:
IDL> curly=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
IDL> larry=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
IDL> curly = curly*(!PI/180.)
IDL> larry *= !PI/180.
IDL> diff=curly-larry
IDL> print,diff
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000
> *Why* the operations are done in a different order is the meta-
> question.
Well, in
curly = curly * !pi / 180.
operations are just done left to right since they all have the same
precedence. In
larry *= some_value
some_value must first be calculated in order to use the *= operator.
Mike
--
www.michaelgalloy.com
Associate Research Scientist
Tech-X Corporation
|
|
|
Re: Friday Afternoon Puzzler [message #66832 is a reply to message #66826] |
Fri, 12 June 2009 14:40  |
R.G. Stockwell
Messages: 363 Registered: July 1999
|
Senior Member |
|
|
"Paolo" <pgrigis@gmail.com> wrote in message
news:0d6bf583-80e0-4761-bc67-3c17aabe25ab@w35g2000prg.google groups.com...
>
>
> R.G. Stockwell wrote:
>> "R.G. Stockwell" <noemail87@please.com> wrote in message
>> news:h0uh0f$k2n$1@aioe.org...
>>
>>
>>
>> specifically:
>>
>> moe = [-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>> moe2 = [-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>> moe3 = [-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>>
>> moe *= 1. / 180.
>> moe2 /=180.
>
> The first line means: moe=moe*(1./180.)
> The second line: moe=moe/180.
> Different operations, again.
Of course (doh) I simplified too much,
I found the difference between curlyjoe and schemp odd
in my previous post a bit odd, and tried to zero in on it.
cheers,
bob
|
|
|
Re: Friday Afternoon Puzzler [message #66834 is a reply to message #66832] |
Fri, 12 June 2009 14:09  |
Kenneth P. Bowman
Messages: 585 Registered: May 2000
|
Senior Member |
|
|
In article <MPG.249c6dccba4246d098a6e1@news.giganews.com>,
David Fanning <news@dfanning.com> wrote:
> Folks,
>
> If you've already had a beer, you may want to work
> on this one. Should we be concerned? Or is this just
> one more example for the Sky is Falling article?
> I've voiced my opinion, but it didn't satisfy the
> complainant. What say you?
>
> curly=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
> larry=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
> curly = curly*!PI/180.
> larry *= !PI/180.
> diff=curly-larry
> print,diff
>
> Why aren't the first set of differences (where the assignment
> is made with the *= operator) *exactly* zero?
>
> Cheers,
>
> David
My guess is that the compiler/interpreter does the multiplication and
division operations in a different order in the two statements in the
first case. The other two cases are syntactically identical, so the
operations should be carried out in the same order.
*Why* the operations are done in a different order is the meta-
question.
Ken Bowman
|
|
|
Re: Friday Afternoon Puzzler [message #66835 is a reply to message #66834] |
Fri, 12 June 2009 14:32  |
pgrigis
Messages: 436 Registered: September 2007
|
Senior Member |
|
|
R.G. Stockwell wrote:
> "R.G. Stockwell" <noemail87@please.com> wrote in message
> news:h0uh0f$k2n$1@aioe.org...
>
>
>
> specifically:
>
> moe = [-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
> moe2 = [-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
> moe3 = [-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>
> moe *= 1. / 180.
> moe2 /=180.
The first line means: moe=moe*(1./180.)
The second line: moe=moe/180.
Different operations, again.
Ciao,
Paolo
> print, 'moe: ',moe - moe2
> print, 'moe3: ',moe3 - moe3*1.
>
>
> moe: 0.000000-9.31323e-010
> 0.000000-4.65661e-010-1.16415e-010-1.16415e-010-4.65661e-010
>
> moe3: 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
|
|
|
Re: Friday Afternoon Puzzler [message #66836 is a reply to message #66834] |
Fri, 12 June 2009 14:30  |
R.G. Stockwell
Messages: 363 Registered: July 1999
|
Senior Member |
|
|
"R.G. Stockwell" <noemail87@please.com> wrote in message
news:h0uh0f$k2n$1@aioe.org...
specifically:
moe = [-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
moe2 = [-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
moe3 = [-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
moe *= 1. / 180.
moe2 /=180.
print, 'moe: ',moe - moe2
print, 'moe3: ',moe3 - moe3*1.
moe: 0.000000-9.31323e-010
0.000000-4.65661e-010-1.16415e-010-1.16415e-010-4.65661e-010
moe3: 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
|
|
|
Re: Friday Afternoon Puzzler [message #66837 is a reply to message #66834] |
Fri, 12 June 2009 14:23  |
R.G. Stockwell
Messages: 363 Registered: July 1999
|
Senior Member |
|
|
"Paolo" <pgrigis@gmail.com> wrote in message
news:45fda197-6ddf-43c4-afe3-46d4bd1d4047@d38g2000prn.google groups.com...
>
>
> David Fanning wrote:
>> Folks,
...
>> curly=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>> larry=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>> curly = curly*!PI/180.
>> larry *= !PI/180.
>
> I'd argue that you are multiplying curly bi pi and dividing the result
> by 180
> in the first line, and dividing pi by 180 first and multiplying larry
> by the result
> in the second line.
oddly:
curly =[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
larry =[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
schemp =[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
curlyjoe=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
curly = curly*!PI/180.
larry *= !PI
larry /= 180.
curlyjoe /= 180.
curlyjoe *= !PI
schemp *= !PI
schemp *= 1./180.
diff=curly-larry
diff2=curly-schemp
diff3=curly-curlyjoe
print,diff
print,diff2
print,diff3
IDL> .GO
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 1.86265e-009 9.31323e-010 4.65661e-010 4.65661e-010
1.86265e-009
0.000000 0.000000 1.86265e-009-9.31323e-010 0.000000 0.000000 0.000000
|
|
|
Re: Friday Afternoon Puzzler [message #66838 is a reply to message #66834] |
Fri, 12 June 2009 14:15  |
pgrigis
Messages: 436 Registered: September 2007
|
Senior Member |
|
|
Paolo wrote:
> Paolo wrote:
>> David Fanning wrote:
>>> Folks,
>>>
>>> If you've already had a beer, you may want to work
>>> on this one. Should we be concerned? Or is this just
>>> one more example for the Sky is Falling article?
>>> I've voiced my opinion, but it didn't satisfy the
>>> complainant. What say you?
>>>
>>> curly=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>>> larry=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>>> curly = curly*!PI/180.
>>> larry *= !PI/180.
>>
>> I'd argue that you are multiplying curly bi pi and dividing the result
>> by 180
>> in the first line, and dividing pi by 180 first and multiplying larry
>> by the result
>> in the second line. Therefore, you are performing two different set of
>> floating
>> point operations, therefore I am not surprised they give slightly
>> different results.
>
> In other words, (a*b)/c is different than a*(b/c) with floats
IDL> a=0.32
IDL> b=!Pi
IDL> c=180.0
IDL> print,(a*b)/c-a*(b/c)
4.65661e-10
It seems I can't resist to comment on my own posts today :)
Paolo
>
> Paolo
>
>>
>> Ciao,
>> Paolo
>>
>>> diff=curly-larry
>>> print,diff
>>>
>>> curly=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>>> larry=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>>> curly = curly*!PI/180.
>>> larry = larry*!PI/180.
>>> diff=curly-larry
>>> print,diff
>>>
>>> curly=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>>> larry=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>>> curly *= !PI/180.
>>> larry *= !PI/180.
>>> diff=curly-larry
>>> print,diff
>>>
>>> The results:
>>>
>>> -3.72529e-009 0.000000 0.000000-9.31323e-010 0.000000
>>> 0.000000 0.000000
>>> 0.000000 0.000000 0.000000 0.000000 0.000000
>>> 0.000000 0.000000
>>> 0.000000 0.000000 0.000000 0.000000 0.000000
>>> 0.000000 0.000000
>>>
>>>
>>> Why aren't the first set of differences (where the assignment
>>> is made with the *= operator) *exactly* zero?
>>>
>>> Cheers,
>>>
>>> David
>>>
>>>
>>> --
>>> David Fanning, Ph.D.
>>> Fanning Software Consulting, Inc.
>>> Coyote's Guide to IDL Programming: http://www.dfanning.com/
>>> Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
Re: Friday Afternoon Puzzler [message #66839 is a reply to message #66834] |
Fri, 12 June 2009 14:09  |
pgrigis
Messages: 436 Registered: September 2007
|
Senior Member |
|
|
Paolo wrote:
> David Fanning wrote:
>> Folks,
>>
>> If you've already had a beer, you may want to work
>> on this one. Should we be concerned? Or is this just
>> one more example for the Sky is Falling article?
>> I've voiced my opinion, but it didn't satisfy the
>> complainant. What say you?
>>
>> curly=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>> larry=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>> curly = curly*!PI/180.
>> larry *= !PI/180.
>
> I'd argue that you are multiplying curly bi pi and dividing the result
> by 180
> in the first line, and dividing pi by 180 first and multiplying larry
> by the result
> in the second line. Therefore, you are performing two different set of
> floating
> point operations, therefore I am not surprised they give slightly
> different results.
In other words, (a*b)/c is different than a*(b/c) with floats
Paolo
>
> Ciao,
> Paolo
>
>> diff=curly-larry
>> print,diff
>>
>> curly=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>> larry=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>> curly = curly*!PI/180.
>> larry = larry*!PI/180.
>> diff=curly-larry
>> print,diff
>>
>> curly=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>> larry=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
>> curly *= !PI/180.
>> larry *= !PI/180.
>> diff=curly-larry
>> print,diff
>>
>> The results:
>>
>> -3.72529e-009 0.000000 0.000000-9.31323e-010 0.000000
>> 0.000000 0.000000
>> 0.000000 0.000000 0.000000 0.000000 0.000000
>> 0.000000 0.000000
>> 0.000000 0.000000 0.000000 0.000000 0.000000
>> 0.000000 0.000000
>>
>>
>> Why aren't the first set of differences (where the assignment
>> is made with the *= operator) *exactly* zero?
>>
>> Cheers,
>>
>> David
>>
>>
>> --
>> David Fanning, Ph.D.
>> Fanning Software Consulting, Inc.
>> Coyote's Guide to IDL Programming: http://www.dfanning.com/
>> Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
Re: Friday Afternoon Puzzler [message #66840 is a reply to message #66834] |
Fri, 12 June 2009 14:07  |
pgrigis
Messages: 436 Registered: September 2007
|
Senior Member |
|
|
David Fanning wrote:
> Folks,
>
> If you've already had a beer, you may want to work
> on this one. Should we be concerned? Or is this just
> one more example for the Sky is Falling article?
> I've voiced my opinion, but it didn't satisfy the
> complainant. What say you?
>
> curly=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
> larry=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
> curly = curly*!PI/180.
> larry *= !PI/180.
I'd argue that you are multiplying curly bi pi and dividing the result
by 180
in the first line, and dividing pi by 180 first and multiplying larry
by the result
in the second line. Therefore, you are performing two different set of
floating
point operations, therefore I am not surprised they give slightly
different results.
Ciao,
Paolo
> diff=curly-larry
> print,diff
>
> curly=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
> larry=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
> curly = curly*!PI/180.
> larry = larry*!PI/180.
> diff=curly-larry
> print,diff
>
> curly=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
> larry=[-1.912,-1.852,-1.386,-0.818,-0.343,-0.343,-1.233]
> curly *= !PI/180.
> larry *= !PI/180.
> diff=curly-larry
> print,diff
>
> The results:
>
> -3.72529e-009 0.000000 0.000000-9.31323e-010 0.000000
> 0.000000 0.000000
> 0.000000 0.000000 0.000000 0.000000 0.000000
> 0.000000 0.000000
> 0.000000 0.000000 0.000000 0.000000 0.000000
> 0.000000 0.000000
>
>
> Why aren't the first set of differences (where the assignment
> is made with the *= operator) *exactly* zero?
>
> Cheers,
>
> David
>
>
> --
> David Fanning, Ph.D.
> Fanning Software Consulting, Inc.
> Coyote's Guide to IDL Programming: http://www.dfanning.com/
> Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|