How do I check if a value is initialised in VEX?

   2370   3   2
User Avatar
Member
10 posts
Joined:
Offline
I want to check if a value is initialised in vex, something like this:

float x;

for (int i = 0; i < length; i++){
float y;
// some logic that assigns a value to y
if (!x){ // <-- This throws an error ("if x doesn't exist yet")
x = y
} else if ( y < x){
x = y
}
}


Here I'm trying to find the smallest value of y returned inside the loop. The first time through the loop I want to assign the value of y found to x, and then on successive iterations only assign y to x if y is lower than the current x.

In other words I want to do something specific the first time through the loop (“if x doesn't exist yet”). However, it seems I can't check to see if x has been initialised.

Is there a way to do this or do I have to initialise x outside the loop to some unlikely huge value and hope the logic doesn't happen to generate a number for y greater than or equal to that value?
User Avatar
Member
1743 posts
Joined: March 2012
Offline
The usual approaches to this (other than initializing x to infinity, for cases where that's safe) are:

1) Make a separate variable keeping track of whether it's been initialized.

float x;
int is_x_initialized = 0;
 
for (int i = 0; i < length; i++){ 
    float y;
    // some logic that assigns a value to y
    if (!is_x_initialized){
        x = y
        is_x_initialized = 1;
    } else if ( y < x){
        x = y   
    }
}

This has the advantage that if lengthmight be zero, or if something else causes xto be uninitialized by the end of the loop, you can check is_x_initializedafter the loop to see if you need special code to handle that case.

2) If you know that xis guaranteed to be initialized, and it's not too complicated to figure out what the initial value will be, just initialize it first.

float x = // some logic that would have assigned the first value of y
for (int i = 1; i < length; i++){ // Note that this starts from 1 now.
    float y;
    // some logic that assigns a value to y
    if (y < x){
        x = y   
    }
}

Hopefully one of these 2 (or 3 if you count assigning infinity to x) approaches will work for you.

If you only care about the value, the infinity approach might be okay, but if you need the index corresponding with the value, that might be problematic if ycan be infinity. That said, we make it a bit awkward to generate infinity in VEX, since it often leads to NaNs later on, but the value 1e1000will always round up to infinity, if you want to use that approach.
Writing code for fun and profit since... 2005? Wow, I'm getting old.
https://www.youtube.com/channel/UC_HFmdvpe9U2G3OMNViKMEQ [www.youtube.com]
User Avatar
Member
2038 posts
Joined: Sept. 2015
Offline
Just assign the initial value of y to x but don't do a check for that initial value. Therefore if it happens to be the smallest value - it stays that way.

float x,y;
int Count, length;

for(Count = 0; Count < length; Count++)
{

// some logic that assigns a value to y

if(Count == 0)
{
x = y;
}

if(Count > 0)
{
if( y < x)
{
x = y;
}
}
}
Edited by BabaJ - April 19, 2018 12:40:56
User Avatar
Member
10 posts
Joined:
Offline
Great suggestions, thank you!
  • Quick Links