Page 1 of 1
object to float conversion/casting
Posted: Tue Aug 21, 2018 6:24 am
by wriedmann
Hello,
I have the following code:
Code: Select all
local oFloat as object
local uFloat as usual
local fFloat as float
uFloat := 12.34
oFloat := 12.34
fFloat := uFloat
fFloat := float( oFloat )
This code compiles, but at runtime the conversion from oFloat to fFloat gives an exception.
This is occuring with both the X# runtime and the Vulcan runtime.
A sample application is added as XIDE Export File:
Wolfgang
object to float conversion/casting
Posted: Tue Aug 21, 2018 7:12 am
by robert
Wolfgang,
The problem seems to be the fact that the literals are stored as REAL8 and not as FLOAT. The exception occurs when casting the OBJECT containing a REAL8 to a FLOAT.
When you compile with /vo14 the program runs fine.
The Vulcan compiler has the same problem b.t.w.
I will see what I can do to fix this.
Robert
object to float conversion/casting
Posted: Tue Aug 21, 2018 8:27 am
by wriedmann
Hi Robert,
thank you very much!
IMHO every conversion that makes sense should be implemented.
Wolfgang
object to float conversion/casting
Posted: Tue Aug 21, 2018 3:36 pm
by robert
Wolfgang,
I looked at this and it is not so easy, especially since the Vulcan runtime has problems in this area.
The safest solution for now is to add a call to the function below to convert from Object to FLOAT
Code: Select all
FUNCTION Object2Float(oValue AS OBJECT) AS FLOAT
LOCAL typ := oValue:GetType() AS System.Type
IF typ == typeof(FLOAT)
RETURN (FLOAT) oValue
ENDIF
LOCAL tc := System.Type.GetTypeCode(typ) AS TypeCode
SWITCH tc
CASE System.TypeCode.SByte
RETURN (FLOAT) (System.SByte) oValue
CASE System.TypeCode.Byte
RETURN (FLOAT) (System.Byte) oValue
CASE System.TypeCode.Double
RETURN (FLOAT) (System.Double) oValue
CASE System.TypeCode.Single
RETURN (FLOAT) (System.Single) oValue
CASE System.TypeCode.UInt16
RETURN (FLOAT) (System.UInt16) oValue
CASE System.TypeCode.UInt32
RETURN (FLOAT) (System.UInt32) oValue
CASE System.TypeCode.UInt64
RETURN (FLOAT) (System.UInt64) oValue
CASE System.TypeCode.Int16
RETURN (FLOAT) (System.Int16) oValue
CASE System.TypeCode.Int32
RETURN (FLOAT) (System.Int32) oValue
CASE System.TypeCode.Int64
RETURN (FLOAT) (System.Int64) oValue
CASE System.TypeCode.Decimal
RETURN (FLOAT) (System.Decimal) oValue
OTHERWISE
THROW InvalidCastException{"Cannot convert from type "+typ:FullName+" to FLOAT"}
END SWITCH
object to float conversion/casting
Posted: Tue Aug 21, 2018 3:40 pm
by wriedmann
Hi Robert,
thank you very much!
Will this function be added to the runtime?
I don't know how many people used something like this in Vulcan, but in new X# code I'm using "object" instead of "usual" when I need variable datatypes for flexibility.
I had found this problem when I worked on the migration of my report engine, and for now the only way to solve it was a Round() call.
Wolfgang
object to float conversion/casting
Posted: Tue Aug 21, 2018 3:48 pm
by robert
Wolfgang,
wriedmann wrote:
Will this function be added to the runtime?
Yes.
Robert
object to float conversion/casting
Posted: Tue Aug 21, 2018 3:51 pm
by wriedmann
Thank you very much, Robert!
Wolfgang