Hi Statalist,
I have been using numerical local macros to store "instructions" on how to slice strings using the substr function, where the number before the decimal point refers to the starting point and the decimal fraction refers to the length of the substring. This is really just by means of explaining my examples, the real puzzle and the reason for this post is very unexpected behaviour of mod. To wit,
results in an unexpected "e" rather than the "ef," that I would expect based on the expected expansion of the macros as substr("abcdefghijklm",5,2). Debugging steps to pin point the trouble maker:
results in the expected 2, but
outputs 1.999999999999957. Since substr expects integers as length, it makes sense that it truncates the value to 1, thus resulting in the single letter output of the initial command.
Now, in the first order it puzzles me why mod would return such an odd result. Can somebody please explain that, so I can in the future avoid such pitfalls (it took me a frustratingly long time to find this, especially since my first debugging step seemed to confirm that there was nothing wrong with my code). I have now wrapped the mod in a round as a workaround, but would prefer a proper fix. In the second order it would be interesting to know why the first debug step (displaying the mod result directly) returns a different result than first assigning it to a macro, but that's purely for curiosity.
Thanks,
Christian
I have been using numerical local macros to store "instructions" on how to slice strings using the substr function, where the number before the decimal point refers to the starting point and the decimal fraction refers to the length of the substring. This is really just by means of explaining my examples, the real puzzle and the reason for this post is very unexpected behaviour of mod. To wit,
Code:
local a = 5.02 di substr("abcdefghijklm",int(`a'),mod(`a',1)*100) // expect ef
Code:
di mod(`a',1)*100 // expect 2
Code:
local b = mod(`a',1)*100 di "`b'" // expect 2
Now, in the first order it puzzles me why mod would return such an odd result. Can somebody please explain that, so I can in the future avoid such pitfalls (it took me a frustratingly long time to find this, especially since my first debugging step seemed to confirm that there was nothing wrong with my code). I have now wrapped the mod in a round as a workaround, but would prefer a proper fix. In the second order it would be interesting to know why the first debug step (displaying the mod result directly) returns a different result than first assigning it to a macro, but that's purely for curiosity.
Thanks,
Christian
Comment