Why do IDL defaultvalues ​​look rounded?

I have a COM object with a function with an optional last argument. IDL is a bit like this:

interface ICWhatever: IDispatch { [id(96)] HRESULT SomeFunction([in,defaultvalue(50.6)]float parameter); }; 

This works fine: if I don't specify a parameter, 50.6 is populated. But in several development environments (Excel VBA, VB6), the default value is rounded up to display. After entering an open brace, I see:

SomeFunction ( [parameter As Single = 51] )

Does anyone know why this is? This is mistake? This confuses client programmers ...

+6
vba vb6 com idl
source share
1 answer

I was able to reproduce the problem you are facing (VBA), and it seems that it really is a mistake in processing the type of Single (in particular) VB IDE . Namely, VB IDEs will incorrectly set the default value of Single to int before printing it again (as part of the method signature) as a (truncated) single-precision floating-point value.

This problem does not exist in the Microsoft Script editor and does not exist in OleView.exe , etc.

To check, try the following Single default value: 18446744073709551615.0 . In my case, this value is correctly encoded in TLB and OleView.exe and Microsoft Script Editor are displayed correctly as 1.844674E+19 . However, it displays as -2.147484E+09 in the VB IDE. Indeed, when you click (float)18446744073709551615.0 on an int , -2147483648 appears, which is displayed as a float , and displays the observed (incorrect) output of VB IDE -2.147484E+09 .

Similarly, 50.6 gets an int value to create 51 , which is then printed as 51 .

To work around this problem, use Double instead of Single , because Double converted and displayed correctly by all IDEs that I could verify.


On a tangent, you probably already know that some floating point values ​​(for example, 0.1 ) do not have a corresponding exact representation of IEEE 754 and cannot differ from other values ​​(for example, 0.1000000015 .) Thus, specifying a double-precision value in by default 0.1 will be displayed in most IDEs as 0.100000001490116 . One way to alleviate this problem is to choose a different scale for your parameters (for example, switch from seconds to milliseconds, so 0.1 seconds will become 100 milliseconds, unambiguously represented as a floating point with single or double precision as integral values ​​/ parameters. )

+1
source share

All Articles