View Issue Details

IDProjectCategoryView StatusLast Update
0000906LDMud 3.7Runtimepublic2022-04-28 22:29
ReporterGnomi Assigned To 
PrioritynormalSeverityfeatureReproducibilityalways
Status newResolutionopen 
Platformi686OSDebian GNU/LInuxOS Version5.0
Summary0000906: Revise default-initialization of float variables
DescriptionTLDR: Initialize floats with integer 0 and prevent arithmetic on this value with RTTCs.

Since LDMud 3.2.11 and 3.3.585 floats are default-initialized with 0.0, whereas all other types are initialized with 0. This brings some inconsistent behavior:
  T var;
  if (!var) { write("Uninitialized.\n"); }

For all T (like string, or float|closure) this will print the string, but not for T = float.

The reasons for the change are unknown. This might have been a suggestion for more intuitive behavior in the following cases or just preventing type inconsistencies there:
    float var;

    return (var + 1) / 2
    var++;
    var += 2;

Previously those expressions would have resulted in 0, 1, 2 as integers, now the result is 0.5, 1.0 and 2.0 as floats.

The change hides an underlying problem with doing arithmetic on the default-zero. In the above example we could initialize var explicitly with 0 and get the old behavior,
thus violating the type restrictions. Also the following code still "works" in LDMud 3.6.5:
    
    string var;
    var+=2;

And results in var being 2.

So in order to remove the default-initialization to 0.0, we need to have a different solution for the arithmetic. Let's start with getting the semantics right.
   
Do we want to do automatic conversion on assignment:
   string var = 2; // Error or "2"?
   float var = 2; // Error or 2.0?
I would vote 'no', otherwise 'string var = 0;' becomes a tricky case. Also it makes runtime behavior very dependent on the knowledge about the type (so pragma save_types might have a heavy influence) and union types will complicate matters.
   
Do we want to do automatic conversion on usage:
   float var;
   return (var + 1) / 2; // 0 or 0.5?
Same argument is before: Making runtime behavior dependent on the knowledge of the type seems a bad idea.

Combination of the above questions for +=:
   string var;
   var += 2;

   float var;
   var += 2;
This should have the same meaning as 'var = var + 2;', so it depends on the previous answers.

So in the end I would suggest to:
1. Remove the 0.0 initialization
2. Add RTTC checks to ++, --, +=, -=

But this would break stuff and would only be detectable at runtime...
TagsNo tags attached.

Activities

There are no notes attached to this issue.

Issue History

Date Modified Username Field Change
2022-04-28 22:29 Gnomi New Issue
2022-04-28 22:29 Gnomi Project LDMud => LDMud 3.7