float and double data types
float and double data types
Does anyone know if the use of the float and double data types in front of a variable definition makes any difference to the data storage thereof in MC? In other words, does float v1(0) causes v1 to be allocated only 4 bytes of storage, while double v2(0) causes v2 to be allocated 8 bytes of storage? Or do they both get 8 bytes making the types redundant? I know that TS adopts the former approach. This has bearing on the way one passes variables to dll's. I haven't experienced any problems but wanted to make sure I am using the right approaches.
- Bruce DeVault
- Posts: 438
- Joined: 19 Jan 2010
- Location: Washington DC
- Been thanked: 2 times
- Contact:
I've done some tests and it appears float and double make no difference in MC. Also, please read what Andrew said here: http://forum.tssupport.com/viewtopic.ph ... ight=floatIt would need to be consistent with the way TS handles it or TS-based DLLs would be broken in general, and they're not. Does that make sense?
So, what I think is the case is that MC numeric variables are always double type, regardless of what one does at the time of declaration. For example, if one uses this code:
variables: int v1(0), float v2(0), double v3(0);
v1 = SquareRoot(9999);
v2 = SquareRoot(9999);
v3 = SquareRoot(9999);
print (numtostr(v1,20));
print (numtostr(v2,20));
print (numtostr(v3,20));
You will get these results:
99.99499987499375200000
99.99499987499375200000
99.99499987499375200000
So, if someone is writing code to be used in both TS and MC, and they are using dll's, then they should explicitly type cast each numeric variable as double.
It also appears that if you use say float in MC and pass it to a dll with a target variable of 4 bytes (ie, single precision in most languages) then return the same variable back to MC, the number may change slightly, presumably as a result of the lower precision. If one uses double throughout, the number does not change. So, beware - there appears to be some inconsistency in MC! lpfloat and lpdouble do make a difference when defining the dll using DefineDLLFunc.
Last edited by janus on 23 Mar 2010, edited 2 times in total.
- Bruce DeVault
- Posts: 438
- Joined: 19 Jan 2010
- Location: Washington DC
- Been thanked: 2 times
- Contact: