Zebra Script Bug - r = r; does something
-
- KVRian
- 595 posts since 20 Jan, 2006
Below is a script that produces random numbers in variable z. The script does not do anything useful to Zebra, it is simply a test and illustration of what is probably a serious bug. At line #88 is "r = r;" and if that line is present, z will have various random numbers as expected, but comment line #88 out and z is always 0. Since this line in theory should do nothing, it follows that it should not matter if it is there or not (and regardless of whether the delta() function works, it's behavior should not change depending on that line of code).
#defaults=no
#patchname=no
<?
// Delta Thing - Version 3.1
int DELTA_FROM_RANGE = 1;
int DELTA_FROM_VALUE = 2;
// delta() - returns the changed parameter value
// v - the parameter's current value
// vmin - the minimum of the range of v
// vmax - the maximum of the range of v
// dt - type of delta, either from a value chosen from the range,
// or a value which is a percentage of the current value
// cp - probability of there being a change, from 0.0 to 1.0
// cb - change bias is from -1.0 to 1.0
// sc - scaling applied to the random value/percentage
// cm - multiple to round the change to. Use 0.0 for no rounding
// fm - multiple to round the final result to. Use 0.0 for no rounding
float delta( float v, float vmin, float vmax, int dt, float cp, float cb, float sc, float cm, float fm) {
float r;
float lo;
float hi;
float x;
float h;
int c;
// Assume no change to the current value
x = v;
// Generate a random to determine if a change will occur or not
r = rand(0.0, 1.0);
// If cp exists (i.e. not 0) AND random r is less than the change probability then we do the change
if (cp > 0.0) { if (r <= cp) {
// If delta is to be taken from value's min/max range...
if (dt == DELTA_FROM_RANGE) {
// Calculate the lo/hi values using the change bias
if (cb > 0.0) {
// Positive bias
lo = (vmin - v) - ((vmin - v) * cb);
hi = vmax - v;
} else {
// Negative bias
lo = vmin - v;
hi = (vmax - v) + ((vmax - v) * cb);
}
// Else the delta is based on lo/hi percentages
} else {
// Calculate the lo/hi values using the change bias
if (cb > 0.0) {
// Positive bias
lo = -1.0 + cb;
hi = 1.0;
} else {
// Negative bias
lo = -1.0;
hi = 1.0 + cb;
}
}
// Generate a random scaled change amount
r = rand(lo, hi) * sc;
// If the change amount should be rounded to a multiple
if (cm > 0.0) {
if (r < 0.0) { h = -0.5; } else { h = 0.5; };
c = ((r / cm) + h);
r = c * cm;
}
// Bugs! Have to do this to get it working
r = r;
// If delta is from value's min/max range
if (dp_osc1_tune_delta_type == DELTA_FROM_RANGE) {
// Add change amount to v
x = v + r;
// Else delta is a percentage of current value
} else {
// Add change amount percentage of current value
x = v + (v * r);
}
// If the final result should be rounded to a multiple
if (fm > 0.0) {
if (x < 0.0) { h = -0.5; } else { h = 0.5; };
c = ((x / fm) + h);
x = c * fm;
}
// Although Zebra will attempt to do limits, sometimes it gets wonky, so do it here too
if (x < vmin) x = vmin;
if (x > vmax) x = vmax;
}}
return x;
}
float z = delta(0.0, -48.0, 48.0, DELTA_FROM_RANGE, 1.0, 0.0, 1.0, 0.0, 0.0);
?>
#defaults=no
#patchname=no
<?
// Delta Thing - Version 3.1
int DELTA_FROM_RANGE = 1;
int DELTA_FROM_VALUE = 2;
// delta() - returns the changed parameter value
// v - the parameter's current value
// vmin - the minimum of the range of v
// vmax - the maximum of the range of v
// dt - type of delta, either from a value chosen from the range,
// or a value which is a percentage of the current value
// cp - probability of there being a change, from 0.0 to 1.0
// cb - change bias is from -1.0 to 1.0
// sc - scaling applied to the random value/percentage
// cm - multiple to round the change to. Use 0.0 for no rounding
// fm - multiple to round the final result to. Use 0.0 for no rounding
float delta( float v, float vmin, float vmax, int dt, float cp, float cb, float sc, float cm, float fm) {
float r;
float lo;
float hi;
float x;
float h;
int c;
// Assume no change to the current value
x = v;
// Generate a random to determine if a change will occur or not
r = rand(0.0, 1.0);
// If cp exists (i.e. not 0) AND random r is less than the change probability then we do the change
if (cp > 0.0) { if (r <= cp) {
// If delta is to be taken from value's min/max range...
if (dt == DELTA_FROM_RANGE) {
// Calculate the lo/hi values using the change bias
if (cb > 0.0) {
// Positive bias
lo = (vmin - v) - ((vmin - v) * cb);
hi = vmax - v;
} else {
// Negative bias
lo = vmin - v;
hi = (vmax - v) + ((vmax - v) * cb);
}
// Else the delta is based on lo/hi percentages
} else {
// Calculate the lo/hi values using the change bias
if (cb > 0.0) {
// Positive bias
lo = -1.0 + cb;
hi = 1.0;
} else {
// Negative bias
lo = -1.0;
hi = 1.0 + cb;
}
}
// Generate a random scaled change amount
r = rand(lo, hi) * sc;
// If the change amount should be rounded to a multiple
if (cm > 0.0) {
if (r < 0.0) { h = -0.5; } else { h = 0.5; };
c = ((r / cm) + h);
r = c * cm;
}
// Bugs! Have to do this to get it working
r = r;
// If delta is from value's min/max range
if (dp_osc1_tune_delta_type == DELTA_FROM_RANGE) {
// Add change amount to v
x = v + r;
// Else delta is a percentage of current value
} else {
// Add change amount percentage of current value
x = v + (v * r);
}
// If the final result should be rounded to a multiple
if (fm > 0.0) {
if (x < 0.0) { h = -0.5; } else { h = 0.5; };
c = ((x / fm) + h);
x = c * fm;
}
// Although Zebra will attempt to do limits, sometimes it gets wonky, so do it here too
if (x < vmin) x = vmin;
if (x > vmax) x = vmax;
}}
return x;
}
float z = delta(0.0, -48.0, 48.0, DELTA_FROM_RANGE, 1.0, 0.0, 1.0, 0.0, 0.0);
?>
- u-he
- 30215 posts since 8 Aug, 2002 from Berlin
Hmmm, I'm not sure if you should put if and else into the same line... I always code with braces strictly on the next line so I guess this is soemthing my compiler would prefer...
if (r < 0.0)
{
h = -0.5;
}
else
{
h = 0.5;
}
Does this change things?
if (r < 0.0)
{
h = -0.5;
}
else
{
h = 0.5;
}
Does this change things?
- KVRAF
- 4141 posts since 11 Aug, 2006 from Texas
Heretic!Urs wrote:I always code with braces strictly on the next line so I guess this is soemthing my compiler would prefer...
/me gets out his K&R-burn-infidel-bracists-kit...
-
- KVRian
- Topic Starter
- 595 posts since 20 Jan, 2006
Retested with the Brackets Just Want To Be Alone version and the bug remains. Below is the updated code for reference in case I missed one, with the bug killer line at #100 now:
#defaults=no
#patchname=no
<?
// Delta Thing - Version 3.2
int DELTA_FROM_RANGE = 1;
int DELTA_FROM_VALUE = 2;
// delta() - returns the changed parameter value
// v - the parameter's current value
// vmin - the minimum of the range of v
// vmax - the maximum of the range of v
// dt - type of delta, either from a value chosen from the range,
// or a value which is a percentage of the current value
// cp - probability of there being a change, from 0.0 to 1.0
// cb - change bias is from -1.0 to 1.0
// sc - scaling applied to the random value/percentage
// cm - multiple to round the change to. Use 0.0 for no rounding
// fm - multiple to round the final result to. Use 0.0 for no rounding
float delta( float v, float vmin, float vmax, int dt, float cp, float cb, float sc, float cm, float fm)
{
float r;
float lo;
float hi;
float x;
float h;
int c;
// Assume no change to the current value
x = v;
// Generate a random to determine if a change will occur or not
r = rand(0.0, 1.0);
// If cp exists (i.e. not 0) AND random r is less than the change probability then we do the change
if (cp > 0.0)
{
if (r <= cp)
{
// If delta is to be taken from value's min/max range...
if (dt == DELTA_FROM_RANGE)
{
// Calculate the lo/hi values using the change bias
if (cb > 0.0)
{
// Positive bias
lo = (vmin - v) - ((vmin - v) * cb);
hi = vmax - v;
}
else
{
// Negative bias
lo = vmin - v;
hi = (vmax - v) + ((vmax - v) * cb);
}
// Else the delta is based on lo/hi percentages
}
else
{
// Calculate the lo/hi values using the change bias
if (cb > 0.0)
{
// Positive bias
lo = -1.0 + cb;
hi = 1.0;
}
else
{
// Negative bias
lo = -1.0;
hi = 1.0 + cb;
}
}
// Generate a random scaled change amount
r = rand(lo, hi) * sc;
// If the change amount should be rounded to a multiple
if (cm > 0.0)
{
if (r < 0.0)
{
h = -0.5;
}
else
{
h = 0.5;
}
c = ((r / cm) + h);
r = c * cm;
}
// Bugs! Have to do this to get it working
r = r;
// If delta is from value's min/max range
if (dp_osc1_tune_delta_type == DELTA_FROM_RANGE)
{
// Add change amount to v
x = v + r;
// Else delta is a percentage of current value
}
else
{
// Add change amount percentage of current value
x = v + (v * r);
}
// If the final result should be rounded to a multiple
if (fm > 0.0)
{
if (x < 0.0)
{
h = -0.5;
}
else
{
h = 0.5;
}
c = ((x / fm) + h);
x = c * fm;
}
// Although Zebra will attempt to do limits, sometimes it gets wonky, so do it here too
if (x < vmin)
{
x = vmin;
}
if (x > vmax)
{
x = vmax;
}
}
}
return x;
}
float z = delta(0.0, -48.0, 48.0, DELTA_FROM_RANGE, 1.0, 0.0, 1.0, 0.0, 0.0);
?>
#defaults=no
#patchname=no
<?
// Delta Thing - Version 3.2
int DELTA_FROM_RANGE = 1;
int DELTA_FROM_VALUE = 2;
// delta() - returns the changed parameter value
// v - the parameter's current value
// vmin - the minimum of the range of v
// vmax - the maximum of the range of v
// dt - type of delta, either from a value chosen from the range,
// or a value which is a percentage of the current value
// cp - probability of there being a change, from 0.0 to 1.0
// cb - change bias is from -1.0 to 1.0
// sc - scaling applied to the random value/percentage
// cm - multiple to round the change to. Use 0.0 for no rounding
// fm - multiple to round the final result to. Use 0.0 for no rounding
float delta( float v, float vmin, float vmax, int dt, float cp, float cb, float sc, float cm, float fm)
{
float r;
float lo;
float hi;
float x;
float h;
int c;
// Assume no change to the current value
x = v;
// Generate a random to determine if a change will occur or not
r = rand(0.0, 1.0);
// If cp exists (i.e. not 0) AND random r is less than the change probability then we do the change
if (cp > 0.0)
{
if (r <= cp)
{
// If delta is to be taken from value's min/max range...
if (dt == DELTA_FROM_RANGE)
{
// Calculate the lo/hi values using the change bias
if (cb > 0.0)
{
// Positive bias
lo = (vmin - v) - ((vmin - v) * cb);
hi = vmax - v;
}
else
{
// Negative bias
lo = vmin - v;
hi = (vmax - v) + ((vmax - v) * cb);
}
// Else the delta is based on lo/hi percentages
}
else
{
// Calculate the lo/hi values using the change bias
if (cb > 0.0)
{
// Positive bias
lo = -1.0 + cb;
hi = 1.0;
}
else
{
// Negative bias
lo = -1.0;
hi = 1.0 + cb;
}
}
// Generate a random scaled change amount
r = rand(lo, hi) * sc;
// If the change amount should be rounded to a multiple
if (cm > 0.0)
{
if (r < 0.0)
{
h = -0.5;
}
else
{
h = 0.5;
}
c = ((r / cm) + h);
r = c * cm;
}
// Bugs! Have to do this to get it working
r = r;
// If delta is from value's min/max range
if (dp_osc1_tune_delta_type == DELTA_FROM_RANGE)
{
// Add change amount to v
x = v + r;
// Else delta is a percentage of current value
}
else
{
// Add change amount percentage of current value
x = v + (v * r);
}
// If the final result should be rounded to a multiple
if (fm > 0.0)
{
if (x < 0.0)
{
h = -0.5;
}
else
{
h = 0.5;
}
c = ((x / fm) + h);
x = c * fm;
}
// Although Zebra will attempt to do limits, sometimes it gets wonky, so do it here too
if (x < vmin)
{
x = vmin;
}
if (x > vmax)
{
x = vmax;
}
}
}
return x;
}
float z = delta(0.0, -48.0, 48.0, DELTA_FROM_RANGE, 1.0, 0.0, 1.0, 0.0, 0.0);
?>
-
- KVRian
- Topic Starter
- 595 posts since 20 Jan, 2006
Speaking of white space, the evil forum software clobbers all >2 space strings, so what you see is not what I have. I suppose I should copy/paste it back and verify the bug...
- u-he
- 30215 posts since 8 Aug, 2002 from Berlin
-
- KVRian
- Topic Starter
- 595 posts since 20 Jan, 2006
Well whadayaknow... the KVR forum fixed the bug! So it is indeed white space at issue...
Textaphysicists have long speculated the existence of hypothetical white space that is undetectable by its emitted glyphs, but whose presence can be inferred from gravitational effects on visible characters. White space is postulated to explain the flat rotation curves of spiral fonts and other evidence of "missing text" in the Zebraverse. According to present observations of structures larger than one page, as well as Big Hash-bang fontology, white space and white space energy account for the vast majority of the chars in the observable Zebraverse.

Textaphysicists have long speculated the existence of hypothetical white space that is undetectable by its emitted glyphs, but whose presence can be inferred from gravitational effects on visible characters. White space is postulated to explain the flat rotation curves of spiral fonts and other evidence of "missing text" in the Zebraverse. According to present observations of structures larger than one page, as well as Big Hash-bang fontology, white space and white space energy account for the vast majority of the chars in the observable Zebraverse.
-
- KVRian
- Topic Starter
- 595 posts since 20 Jan, 2006
Urs wrote:You could enclose the code inCode: Select all
tags...[/quote] OK here goes (and I am posting the broken version with r = r; commented out): [code] #defaults=no #patchname=no <? // Delta Thing - Version 3.2 int DELTA_FROM_RANGE = 1; int DELTA_FROM_VALUE = 2; // delta() - returns the changed parameter value // v - the parameter's current value // vmin - the minimum of the range of v // vmax - the maximum of the range of v // dt - type of delta, either from a value chosen from the range, // or a value which is a percentage of the current value // cp - probability of there being a change, from 0.0 to 1.0 // cb - change bias is from -1.0 to 1.0 // sc - scaling applied to the random value/percentage // cm - multiple to round the change to. Use 0.0 for no rounding // fm - multiple to round the final result to. Use 0.0 for no rounding float delta( float v, float vmin, float vmax, int dt, float cp, float cb, float sc, float cm, float fm) { float r; float lo; float hi; float x; float h; int c; // Assume no change to the current value x = v; // Generate a random to determine if a change will occur or not r = rand(0.0, 1.0); // If cp exists (i.e. not 0) AND random r is less than the change probability then we do the change if (cp > 0.0) { if (r <= cp) { // If delta is to be taken from value's min/max range... if (dt == DELTA_FROM_RANGE) { // Calculate the lo/hi values using the change bias if (cb > 0.0) { // Positive bias lo = (vmin - v) - ((vmin - v) * cb); hi = vmax - v; } else { // Negative bias lo = vmin - v; hi = (vmax - v) + ((vmax - v) * cb); } // Else the delta is based on lo/hi percentages } else { // Calculate the lo/hi values using the change bias if (cb > 0.0) { // Positive bias lo = -1.0 + cb; hi = 1.0; } else { // Negative bias lo = -1.0; hi = 1.0 + cb; } } // Generate a random scaled change amount r = rand(lo, hi) * sc; // If the change amount should be rounded to a multiple if (cm > 0.0) { if (r < 0.0) { h = -0.5; } else { h = 0.5; } c = ((r / cm) + h); r = c * cm; } // Bugs! Have to do this to get it working // r = r; // If delta is from value's min/max range if (dp_osc1_tune_delta_type == DELTA_FROM_RANGE) { // Add change amount to v x = v + r; // Else delta is a percentage of current value } else { // Add change amount percentage of current value x = v + (v * r); } // If the final result should be rounded to a multiple if (fm > 0.0) { if (x < 0.0) { h = -0.5; } else { h = 0.5; } c = ((x / fm) + h); x = c * fm; } // Although Zebra will attempt to do limits, sometimes it gets wonky, so do it here too if (x < vmin) { x = vmin; } if (x > vmax) { x = vmax; } } } return x; } float z = delta(0.0, -48.0, 48.0, DELTA_FROM_RANGE, 1.0, 0.0, 1.0, 0.0, 0.0); ?>
- u-he
- 30215 posts since 8 Aug, 2002 from Berlin
Sheesh. I will look into it. At some point.billstei wrote:Confirmed. The copied back code above exhibits the bug.
This is probably the most complex code ever written for Zebra's scripting engine. I'll spend some time analysing the byte code...
(I'm currently working on a database scheme for patches marked as favourite or junk... weird stuff... but has to be done...)
Cheers,
-
- KVRian
- Topic Starter
- 595 posts since 20 Jan, 2006
Okay, while you're doing that I will try to contact an alien race and see about having you cloned. Twice. So with three of you working in shifts this shouldn't be too bad.
-
- KVRian
- Topic Starter
- 595 posts since 20 Jan, 2006
Aha! I found where the problem(s) is. There is an undefined variable (i.e. a bug in my code) immediately after the bug killer r = r; line of code. The variable dp_osc1_tune_delta_type should not exist, but the compiler (apparently) allowed it. The question then is whether the compiler should have bailed with an error code (I would say yes). And why variations in white space, or the big killer line, would "fix" it... 
