none
IEEE 754 types, casting and literal suffix RRS feed

  • Question

  • I have a C# program which is doing a lot of calculations mainly based on System.Double.
    I need to compile a single precision (System.Single) version of this program to compare the results with the double version.
    I found using a type alias (e.g. using MyReal = System.Single;) useful, then replacing all variable types declared as System.Double (or just double) to MyReal.
    After doing this it is very simple to switch between the definition of MyReal (either System.Double or System.Single or even System.Decimal).

    However this doesn't cover the types of literals given by suffix (or lack of suffix which is implicitly double).
    What I have done is casting from double literal e.g. (MyReal)1.23 (which would be same as (MyReal)1.23d), this is working but it feels like somehing is missing.
    Is it possible to define own suffixes for use in source code, similar to the type alias?

    Monday, June 17, 2019 10:09 AM

Answers

  • Hi EuroEager,

    Thank you for posting here.

    >>I found using a type alias (e.g. using MyReal = System.Single;) useful, then replacing all variable types declared as System.Double (or just double) to MyReal.

    I don't quite understand why you use the type alias, because I think the type that c# provided could solve our problem as usual.What problem did you encounter when using the type?

    >>So this is just to try to find a way of easy switching precision

    We can use Convert to do this.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by EuroEager Tuesday, June 18, 2019 7:23 AM
    Tuesday, June 18, 2019 6:21 AM
    Moderator

All replies

  • You cannot define your own suffix. The suffix is defined by the language.

    I would not recommend the approach you're going anyway. It is unclear to me why you'd need to recompile your code using single vs double. Just truncating a double to a single would yield the same result excluding precision differences. Just build it into your app if you need to. There are any # of ways to do this. If you want to switch via a simple command line parameter then create a struct that holds both values as fields. Expose a Value property that gets/sets the value as a double. Assign the double field that value. Single field gets the truncated version. No need to change any other code except when you want the single vs double version.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, June 17, 2019 1:47 PM
    Moderator
  • Thanks

    The rationale for running the same code with single and double is that if single gives the same results within a very tiny margin, the program may/should be ported to a PLC instead of PC (realtime OS).

    So this is just to try to find a way of easy switching precision

    Monday, June 17, 2019 4:36 PM
  • Hi EuroEager,

    Thank you for posting here.

    >>I found using a type alias (e.g. using MyReal = System.Single;) useful, then replacing all variable types declared as System.Double (or just double) to MyReal.

    I don't quite understand why you use the type alias, because I think the type that c# provided could solve our problem as usual.What problem did you encounter when using the type?

    >>So this is just to try to find a way of easy switching precision

    We can use Convert to do this.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by EuroEager Tuesday, June 18, 2019 7:23 AM
    Tuesday, June 18, 2019 6:21 AM
    Moderator