Vector Size Error?

Anything that doesn't fit elsewhere.
Post Reply
shailesh
Posts: 104
Joined: Thu Mar 10, 2011 12:11 am

Vector Size Error?

Post by shailesh » Thu Aug 15, 2013 11:43 pm

A seemingly very trivial problem, but I can't figure out whats going wrong with the below code for defining a vector:

Code: Select all

//Range
MINS = 1.2
INCS = 0.05
MAXS = 5

objref vecs

vecs = new Vector(((MAXS-MINS)/INCS)+1)
print "vector Size: ", vecs.size()
print "vector defined Size: ", (((MAXS-MINS)/INCS)+1)

print "\nRetry - With size variable"
x = (((MAXS-MINS)/INCS)+1)
vecs = new Vector(x)
print "vector Size: ", vecs.size()
print "vector defined Size: ", x

print "\nRetry - Without any variables"
vecs = new Vector(((5-1.2)/0.05)+1)
print "vector Size: ", vecs.size()
print "vector defined Size: ", (((5-1.2)/0.05)+1)

print "\nRetry - With direct size"
vecs = new Vector(77)
print "vector Size: ", vecs.size()
print "vector defined Size: ", 77
The output & NEURON specs:
NEURON -- Release 7.3 (849:5be3d097b917) 2013-04-11
Duke, Yale, and the BlueBrain Project -- Copyright 1984-2013
See http://www.neuron.yale.edu/neuron/credits

vector size: 76
vector defined size: 77

Retry - With size variable
vector size: 76
vector defined size: 77

Retry - Without any variables
vector size: 76
vector defined size: 77

Retry - With direct size
vector size: 77
vector defined size: 77
Why is the size "1 less" when defining through expressions/variables - as compared to direct assignment?

ted
Site Admin
Posts: 5592
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Vector Size Error?

Post by ted » Fri Aug 16, 2013 11:43 am

Roundoff error.

Code: Select all

(((MAXS-MINS)/INCS)+1) // prints to screen as 77, but
(((MAXS-MINS)/INCS)+1) - 77 // returns -1.4210855e-14
float_epsilon // is 1e-11 by default,
// but apparently vec = new Vector(size) and vec.resize(size) ignore float_epsilon.
// However, int() does not--
int(((MAXS-MINS)/INCS)+1) // returns 77
(((MAXS-MINS)/INCS)+1) - 77 // returns 0
// so use this workaround:
vecs = new Vector(int(((MAXS-MINS)/INCS)+1))

shailesh
Posts: 104
Joined: Thu Mar 10, 2011 12:11 am

Re: Vector Size Error?

Post by shailesh » Sat Aug 17, 2013 3:20 am

Thanks for the workaround!

Post Reply