Bug in input program?

classic Classic list List threaded Threaded
5 messages Options
SD
Reply | Threaded
Open this post in threaded view
|

Bug in input program?

SD
Hello again,

if I run the following code in v20:

input program.
loop a=1.01 to 2.01 by 0.01.
end case.
end loop.
end file.
end input program.
execute.

compute test1_12 = (a=1.12).
compute test1_13 = (a=1.13).
execute.

test1_12 is exactly 1 where it should be, but test1_13 is 0 where a=1.13.
I already got a solution (compute test1_13 = (rnd(a,0.01).) to work around it, but I'm curious why it occurs or if I'm doing something wrong what I simply don't see.

Thanks in advance.


Reply | Threaded
Open this post in threaded view
|

Re: Bug in input program?

Jon K Peck
Run this program.

input program.
loop a=1.01 to 2.01 by 0.01.
end case.
end loop.
end file.
end input program.
compute  z = a- 1.13.
format z(E22.15).
execute.

You will see that the values are very slightly different from what you expect.  This is the nature of floating point arithmetic.  You should never assume that fractional parts of floating point numbers are exactly equal to what you usually see.  You can read an explanation here: http://en.wikipedia.org/wiki/Floating_point.

Statistical procedures do their calculations in carefully chosen round about ways in order to address this problem, but in simple calculations there will generally be small differences between, say, 1.01 + 0.01 repeated times and the value 1.13.  In this case, the difference is 2.220446049250313 x 10**-16 where your code expects a zero.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        SD <[hidden email]>
To:        [hidden email],
Date:        06/14/2013 05:19 AM
Subject:        [SPSSX-L] Bug in input program?
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hello again,

if I run the following code in v20:

input program.
loop a=1.01 to 2.01 by 0.01.
end case.
end loop.
end file.
end input program.
execute.

compute test1_12 = (a=1.12).
compute test1_13 = (a=1.13).
execute.

test1_12 is exactly 1 where it should be, but test1_13 is 0 where a=1.13.
I already got a solution (compute test1_13 = (rnd(a,0.01).) to work around
it, but I'm curious why it occurs or if I'm doing something wrong what I
simply don't see.

Thanks in advance.






--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Bug-in-input-program-tp5720724.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD


Reply | Threaded
Open this post in threaded view
|

Re: Bug in input program?

David Marso
Administrator
In reply to this post by SD
You should NEVER assume exact equality when comparing floating point numbers.
The following approach will keep your code from exploding!
You may need to adjust the value of #eps, but this should suffice.

COMPUTE #eps=.00000000000001.
COMPUTE test1_12 = RANGE(a,1.12-#eps,1.12+#eps).
COMPUTE test1_13 = RANGE(a,1.13-#eps,1.13+#eps).

SD wrote
Hello again,

if I run the following code in v20:

input program.
loop a=1.01 to 2.01 by 0.01.
end case.
end loop.
end file.
end input program.
execute.

compute test1_12 = (a=1.12).
compute test1_13 = (a=1.13).
execute.

test1_12 is exactly 1 where it should be, but test1_13 is 0 where a=1.13.
I already got a solution (compute test1_13 = (rnd(a,0.01).) to work around it, but I'm curious why it occurs or if I'm doing something wrong what I simply don't see.

Thanks in advance.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Automatic reply: Bug in input program?

Morris, Ramona (MCSCS)
I'm sorry I can't reply to you.  I will be out of the office until Monday June 24, 2013

For assistance please contact my colleague  Brian McNair: [hidden email] or by phone 519 773 4288.

Thank you
Ramona Morris
R&E Ontario Police College

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
SD
Reply | Threaded
Open this post in threaded view
|

Re: Bug in input program?

SD
In reply to this post by Jon K Peck
Ok, thank you both. Good to know.