Go Back

Are there equivalent .net data types for numeric synergy data types with precision?

Say I have synergy code as follows :

data qty,  D10.3, 1.250
data price, d8.4, 1.3333
data value  d10.2
value = qty * price

This rounds in a predictable manner and eth results can be stored in records on file.

Say I am writing a synergy.net method which returns Value.
 

Do I have to define my properties and return values as Dx.y, or can I use synergy.net data types and get the same result

I know float is not a good optionand does not return the same results

I know I can use INTs for whole numbers.  I'm just not sure about fields with precision and the associated rounding in calculations
 

5 Answers
0   | Posted by Gordon Ireland to Synergy .NET on 9/4/2020, 1:59 PM
Kish Baley
The .Net floating point type "decimal" is probably the one you want to use, though float and double would also work depending on the precision required.

Calculations are performed to the full precision of a decimal type. The caller should interpret to the desired precision. That's different than how DBL decimal works where the precision is intrinsic to the variable.

Ex.
public method MyMethod, decimal
,,,
endmethod

proc
  data mc = new MyClass()
  Console.WriteLine(mc.MyMethod(...).ToString("F2"))  ;Displays 2 decimal precision


 

9/11/2020, 12:33 AM   0  
Gordon Ireland
Thanks Kish

9/11/2020, 8:04 AM   0  
Gordon Ireland
Found this useful little table with max field sizes.  So Double is closer to the D18 limit in synergy, but Decimal would be the most accurate

And Float is the least accurate

 
+---------+----------------+---------+----------+---------------------------------------------+
| C#      | .Net Framework | Signed? | Bytes    | Possible Values                             |
| Type    | (System) type  |         | Occupied |                                             |
+---------+----------------+---------+----------+---------------------------------------------+
| sbyte   | System.Sbyte   | Yes     | 1        | -128 to 127                                 |
| short   | System.Int16   | Yes     | 2        | -32768 to 32767                             |
| int     | System.Int32   | Yes     | 4        | -2147483648 to 2147483647                   |
| long    | System.Int64   | Yes     | 8        | -9223372036854775808 to 9223372036854775807 |
| byte    | System.Byte    | No      | 1        | 0 to 255                                    |
| ushort  | System.Uint16  | No      | 2        | 0 to 65535                                  |
| uint    | System.UInt32  | No      | 4        | 0 to 4294967295                             |
| ulong   | System.Uint64  | No      | 8        | 0 to 18446744073709551615                   |
| float   | System.Single  | Yes     | 4        | Approximately ±1.5 x 10-45 to ±3.4 x 1038   |
|         |                |         |          |  with 7 significant figures                 |
| double  | System.Double  | Yes     | 8        | Approximately ±5.0 x 10-324 to ±1.7 x 10308 |
|         |                |         |          |  with 15 or 16 significant figures          |
| decimal | System.Decimal | Yes     | 12       | Approximately ±1.0 x 10-28 to ±7.9 x 1028   |
|         |                |         |          |  with 28 or 29 significant figures          |
| char    | System.Char    | N/A     | 2        | Any Unicode character (16 bit)              |
| bool    | System.Boolean | N/A     | 1 / 2    | true or false                   

9/11/2020, 10:38 AM   0  
Kish Baley
Note that Synergy implied decimal max is D56.28 while Synergy decimal max is d28, not d18. I'm not sure which version expanded it.

9/11/2020, 12:22 PM   0  
Gordon Ireland
thanks Kish.

sounds like decimal is the safest one to go with
 

9/11/2020, 12:45 PM   0  
Please log in to comment or answer this question.