Vector and float wierdness

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
kitsu
Posts: 0
Joined: Sun Jun 06, 2004 8:08 am

Vector and float wierdness

Post by kitsu »

I think I commented on this before but the Mathutils Vector class seems to do some odd things with floats. My original problem was with letters in my floats, but I don't have the offending file with me currently. Today though I was working on something and had more problems. So could someone please test this for me:

make a vector, then set vector.x to .0005 or .00005. Now print the vector and print vector.x. I got strange and misleading data from this.

I discovered this when trying to put a faces normal into a vector (printing the face.no and the vector for compairison).

stiv
Posts: 0
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Post by stiv »

You need to learn more about the digital representation of numbers. Especially if you are going to compare their printed representations.

Python and Bpy use the % operator for formatted printing. The format strings are similar to those used by the printf() function in C.

The Vector type repr method uses "%.4f" to print values. example:

print "%.4f" % vector.x

kitsu
Posts: 0
Joined: Sun Jun 06, 2004 8:08 am

Post by kitsu »

Why the hostile tone? I realize you get foolish questions here in bulk but that doesn't mean you have to be curt/rude to everyone who asks a question here.

I understand formatted printing, Why though is the vectors repr rounded to the 4th significant digit? Also try n=vector.x then print n. with vector.x = .0005 I get 0.000500000023749.

A while ago I was writting a flock/steering behavior module for use with my duplivert/animation offset script and couldn't get the vectors to work. I tried to ask the question to the python-scripts mailing list but my attachment was 600k and the message never got authorized... Whatever, its not important right now, I can just use my own vector class. I would still like to know if anyone else has problems with the Mathutils vector class.

stiv
Posts: 0
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Post by stiv »

kitsu wrote:Why the hostile tone? I realize you get foolish questions here in bulk but that doesn't mean you have to be curt/rude to everyone who asks a question here.
Didn't mean to be.
I understand formatted printing, Why though is the vectors repr rounded to the 4th significant digit? Also try n=vector.x then print n. with vector.x = .0005 I get 0.000500000023749.
The Vector type repr method uses "%.4f" to print values. Printing vector.x calls the float repr method, not Vector repr.

kitsu
Posts: 0
Joined: Sun Jun 06, 2004 8:08 am

Post by kitsu »

Good point thank you.

ascotan
Posts: 0
Joined: Thu May 29, 2003 10:32 pm
Location: Maryland, U.S.

Post by ascotan »

Check out this little article from python.org
http://www.python.org/doc/faq/general.h ... inaccurate

stiv
Posts: 0
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Post by stiv »

ascotan wrote:Check out this little article from python.org...
I hope you are not suggesting that he needs to learn more about the digital representation of numbers. (it is a touchy subject!)

Post Reply