Explicit casting and manual datatype specification in VEX

   4362   4   1
User Avatar
Member
28 posts
Joined: Sept. 2019
Offline
Hi.

I've just started learning VEX and I'd like to know how they are different exactly.
For example, in the following line,

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

I need to use explicit casting giving float datatype to ptnum attribute in order to get float type result from integer datatypes. But why do I get error sign if I try like,

@Cd = f@ptnum / @numpt;
Edited by sweetroll - Jan. 18, 2020 15:02:33
User Avatar
Member
555 posts
Joined: Feb. 2017
Offline
cos Cd is a vector ? it has 3 components r g b so you often see Cd.r

so if you assign 0.1 to Cd…how does it know which component you are talking about ?

@Cd.r = f@ptnum / @numpt; should work
Edited by vusta - Jan. 19, 2020 05:19:45
User Avatar
Member
8551 posts
Joined: July 2007
Online
it doesn't work because f@ptnum can't be bound to any data, since @ptnum is i@ptnum, so you either cast it to float as your first snippet or you can also include floating point number in the equation to have it automatically cast to float
somethinglike this
@Cd= 1.0 * @ptnum / @numpt;

however most likely you would want something like this anyway, so that you cover full range 0-1
@Cd= @ptnum / (@numpt - 1.0);
Tomas Slancik
FX Supervisor
Method Studios, NY
User Avatar
Member
28 posts
Joined: Sept. 2019
Offline
tamte
it doesn't work because f@ptnum can't be bound to any data, since @ptnum is i@ptnum, so you either cast it to float as your first snippet or you can also include floating point number in the equation to have it automatically cast to float
somethinglike this
@Cd= 1.0 * @ptnum / @numpt;

So f@ptnum is for case it actually has float data in it already, and the rest of the methods are to make it possible to have float data afterward. I hope I understood right. Thanks both of you!
User Avatar
Member
8551 posts
Joined: July 2007
Online
capivara2077
So f@ptnum is for case it actually has float data in it already
not exactly, as mentioned, there is no such thing as f@ptnum
in Attribwrangle SOP @ptnum binds to an internal data which contains point number
this data is of type int and therefore i@ptnum, but since it's one of the known binding variables, you can use @ptnum and Houdini will know it's int. (the same way as you can use @P, @N, @Cd, @orient, … without type specification since Houdini knows the preferred type of those for convenience)

so if you type f@ptnum, you will get an error, knowever theoretically you should just get 0.0

so this
1.0 * @ptnum / @numpt
is literally just saying
1.0 * i@ptnum / i@numpt
so if you substitute for numbers, for example this
1.0 * 3 / 10
you will get 0.3
while 3 / 10 = 0

so you can see that in both cases 3 is still an integer, same for @ptnum, however within the equation Houdini casts it to float automatically if it needs to do operation between in teger and float
however it will keep it as integer if the operation is only between int and int

so for example
1.0 * 3 / 10
can be split to
1.0 * 3 and the result / 10
1.0 * 3 will be automatically casted to 1.0 * 3.0 which is 3.0
then 3.0 / 10 will again be automatically casted to 3.0 / 10.0 which is 0.3

if however you have
3 / 10 * 1.0
then 3 / 10 is still just division between 2 integers so nothing is casted and 3 / 10 is 0
then 0 * 1.0 is automaticlally casted to 0.0 * 1.0 and that's 0.0
so you can see that the order and the position of that inserted 1.0 in the first example was important and used intentionally to make all operations float

lastly the example of
@ptnum / (@numpt - 1.0)
so let's imagine 2 integers instead of @ variables
3 / (11 - 1.0)
by the order of operations () are computed first so
11 - 1.0 will be automatically casted as one of them is float so 11.0 - 1.0 = 10.0
then 3 / 10.0 again will become 3.0 / 10.0 = 0.3

so you can see that in all examples @ptnum as shown on substituted value is still an int and what makes it behave as it does is the VEX rules for automatic casting of integers to float whenever there is basic operation between int and float
Edited by tamte - Jan. 19, 2020 12:44:40
Tomas Slancik
FX Supervisor
Method Studios, NY
  • Quick Links