fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: object to float conversion/casting

object to float conversion/casting 7 months 2 days ago #1

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Offline
  • Posts: 1423
  • Karma: 6
Hello,

I have the following code:
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:

File Attachment:

File Name: ConvertToFloat.zip
File Size:1 KB


Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it
Attachments:

Please Log in or Create an account to join the conversation.

object to float conversion/casting 7 months 2 days ago #2

  • robert
  • robert's Avatar
  • Away
  • Posts: 844
  • Karma: 6
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
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

Last edit: by robert.

object to float conversion/casting 7 months 2 days ago #3

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Offline
  • Posts: 1423
  • Karma: 6
Hi Robert,

thank you very much!

IMHO every conversion that makes sense should be implemented.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

object to float conversion/casting 7 months 2 days ago #4

  • robert
  • robert's Avatar
  • Away
  • Posts: 844
  • Karma: 6
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
  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
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

object to float conversion/casting 7 months 2 days ago #5

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Offline
  • Posts: 1423
  • Karma: 6
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
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

object to float conversion/casting 7 months 2 days ago #6

  • robert
  • robert's Avatar
  • Away
  • Posts: 844
  • Karma: 6
Wolfgang,

wriedmann wrote: Will this function be added to the runtime?


Yes.

Robert
XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

object to float conversion/casting 7 months 2 days ago #7

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Offline
  • Posts: 1423
  • Karma: 6
Thank you very much, Robert!

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

  • Page:
  • 1