Jump to content

[Solved]global variable computing problem


GrayHare

Recommended Posts

Hi there,

I am a newbie in vex and tried to start from some basic computing to learn it. In this case I tried to recreate a computing:

$PT/$NPT

and give this value to @Cd. I tried three different ways but only one way works.

float pt;
float npt;
float cr;
pt = @ptnum;
npt = @numpt;
cr = pt/npt;
@Cd = {0,0,0};
@Cd.x= cr;

Follows are not working.

@Cd = {0,0,0};
@Cd.x = @ptnum/@numpt;
@Cd = {0,0,0};
float cr;
cr = @ptnum/@numpt;
@Cd.x = cr;

Looks like I can't use global variables to compute directly. Any explains? Thanks!

 

Gu

Problem.hip

Edited by GrayHare
Link to comment
Share on other sites

the first one would work and others would fail because of types.  one drawback with vex handling multiple types is that it sometimes picks the wrong one when it's ambiguous.

 

ptnum and numpt are integers.  so int/int yields and int.  since ptnum is less than numpt, it will always be 0.

 

your first one works because you first make float versions of your ptnum and numpt values and then divide using floats so you get a float result.

 

 

try:

 

@Cd.x  = float(@ptnum)/@numpt;

 

 

that will cast ptnum to a float for the division which will then yield a float result.

 

 

 

when you use vectors, this gets more annoying.  some functions return multiple types and will pick the type based on the other types involved.

 

for example:

 

v@N = noise(v@P);   # returns a vector

v@N = noise(v@P)*2;  # returns a float (which then promotes to a vector of all the same value)

  • Like 1
Link to comment
Share on other sites

yes, types are important, but you can as well use automatic promotion to your advantage

so if you want to get number 0-1 you would want to do $PT/($NPT-1) (as @PT starts at 0)

so while you can do:

@Cd.x = float(@ptnum)/(@numpt-1);

you can simply type:

@Cd.x = @ptnum/(@numpt-1.0);

as 1.0 is float it will evaluate everything as floats automatically 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...