DataTypeApplyOpFromText: renamed local variables + comments to avoid confusion about the fact that int and float paths are not totally symetrical. (#671)

This commit is contained in:
omar 2017-08-03 16:41:29 +08:00
parent 6e04cedd5f
commit 40ac84d701

View File

@ -6369,16 +6369,16 @@ static bool DataTypeApplyOpFromText(const char* buf, const char* initial_value_b
scalar_format = "%d"; scalar_format = "%d";
int* v = (int*)data_ptr; int* v = (int*)data_ptr;
const int old_v = *v; const int old_v = *v;
int arg0 = *v; int arg0i = *v;
if (op && sscanf(initial_value_buf, scalar_format, &arg0) < 1) if (op && sscanf(initial_value_buf, scalar_format, &arg0i) < 1)
return false; return false;
// Store operand in a float so we can use fractional value for multipliers (*1.1), but constant always parsed as integer so we can fit big integers (e.g. 2000000003) past float precision // Store operand in a float so we can use fractional value for multipliers (*1.1), but constant always parsed as integer so we can fit big integers (e.g. 2000000003) past float precision
float arg1 = 0.0f; float arg1f = 0.0f;
if (op == '+') { if (sscanf(buf, "%f", &arg1) == 1) *v = (int)(arg0 + arg1); } // Add (use "+-" to subtract) if (op == '+') { if (sscanf(buf, "%f", &arg1f) == 1) *v = (int)(arg0i + arg1f); } // Add (use "+-" to subtract)
else if (op == '*') { if (sscanf(buf, "%f", &arg1) == 1) *v = (int)(arg0 * arg1); } // Multiply else if (op == '*') { if (sscanf(buf, "%f", &arg1f) == 1) *v = (int)(arg0i * arg1f); } // Multiply
else if (op == '/') { if (sscanf(buf, "%f", &arg1) == 1 && arg1 != 0.0f) *v = (int)(arg0 / arg1); }// Divide else if (op == '/') { if (sscanf(buf, "%f", &arg1f) == 1 && arg1f != 0.0f) *v = (int)(arg0i / arg1f); }// Divide
else { if (sscanf(buf, scalar_format, &arg0) == 1) *v = arg0; } // Assign constant else { if (sscanf(buf, scalar_format, &arg0i) == 1) *v = arg0i; } // Assign constant (read as integer so big values are not lossy)
return (old_v != *v); return (old_v != *v);
} }
else if (data_type == ImGuiDataType_Float) else if (data_type == ImGuiDataType_Float)
@ -6387,17 +6387,17 @@ static bool DataTypeApplyOpFromText(const char* buf, const char* initial_value_b
scalar_format = "%f"; scalar_format = "%f";
float* v = (float*)data_ptr; float* v = (float*)data_ptr;
const float old_v = *v; const float old_v = *v;
float arg0 = *v; float arg0f = *v;
if (op && sscanf(initial_value_buf, scalar_format, &arg0) < 1) if (op && sscanf(initial_value_buf, scalar_format, &arg0f) < 1)
return false; return false;
float arg1 = 0.0f; float arg1f = 0.0f;
if (sscanf(buf, scalar_format, &arg1) < 1) if (sscanf(buf, scalar_format, &arg1f) < 1)
return false; return false;
if (op == '+') { *v = arg0 + arg1; } // Add (use "+-" to subtract) if (op == '+') { *v = arg0f + arg1f; } // Add (use "+-" to subtract)
else if (op == '*') { *v = arg0 * arg1; } // Multiply else if (op == '*') { *v = arg0f * arg1f; } // Multiply
else if (op == '/') { if (arg1 != 0.0f) *v = arg0 / arg1; } // Divide else if (op == '/') { if (arg1f != 0.0f) *v = arg0f / arg1f; } // Divide
else { *v = arg1; } // Assign constant else { *v = arg1f; } // Assign constant
return (old_v != *v); return (old_v != *v);
} }