object to float conversion/casting

This forum is meant for questions and discussions about the X# language and tools
Post Reply
User avatar
wriedmann
Posts: 3748
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

object to float conversion/casting

Post 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:
ConvertToFloat.zip
(1.1 KiB) Downloaded 62 times
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
robert
Posts: 4478
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

object to float conversion/casting

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
wriedmann
Posts: 3748
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

object to float conversion/casting

Post by wriedmann »

Hi Robert,

thank you very much!

IMHO every conversion that makes sense should be implemented.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
robert
Posts: 4478
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

object to float conversion/casting

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
wriedmann
Posts: 3748
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

object to float conversion/casting

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
robert
Posts: 4478
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

object to float conversion/casting

Post by robert »

Wolfgang,
wriedmann wrote: Will this function be added to the runtime?
Yes.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
wriedmann
Posts: 3748
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

object to float conversion/casting

Post by wriedmann »

Thank you very much, Robert!

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply