Hi Chris,
if I remember correctly, you had used not the latest VO version, and Robert wrote that he had discovered flaws in the runtime that could cause 5333s when using floats.
Wolfgang
P.S. anyway, this is a very interesting thread. I will bookmark it in both my pearls page and the X# Documentation Project
Float, Real8, or Decimal?
Float, Real8, or Decimal?
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Float, Real8, or Decimal?
The problem that I found had to do with calling methods or functions where one of the arguments was defined as USUAL or FLOAT. When the value passed in was the result of some kind of calculation then the (dynamic) value was calculated and allocated in dynamic memory and its address was pushed on the stack but not under the control of the GC. If any of the expressions for other parameters would trigger the GC to run then this reference on the stack was not updated.
The solution that I created calculates each parameter first and stores the results in temporary variables (which were of course registered with the GC) and after all parameters were known then their values or addresses were pushed on the stack and the function/method would be called.
This solved most of the FLOAT related errors (but this could happen with strings as well).
Robert
The solution that I created calculates each parameter first and stores the results in temporary variables (which were of course registered with the GC) and after all parameters were known then their values or addresses were pushed on the stack and the function/method would be called.
This solved most of the FLOAT related errors (but this could happen with strings as well).
Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
The Netherlands
robert@xsharp.eu
- ArneOrtlinghaus
- Posts: 412
- Joined: Tue Nov 10, 2015 7:48 am
- Location: Italy
Float, Real8, or Decimal?
I feel proud having created such a nice long discussion... B)
In VO we had the experience that mixing of types created many problems. Although most of the program worked with float, we tried to use real8 in some places and we had to change many real8 definitions back to float. I believe it was the combination together with usuals in the basic classes.
I have now made some performance tests in X# with the X# runtime, also mixing with usuals. I would summarize it as follows:
Although Real8 is the winner, using decimal performs still better than float and usual and has the precision advantages. Usual and float have nearly the same performance. So if trying to replace floats in old programs it can be a good idea to use decimals and not real8.
For using decimals I had to change the literals in the code which can create much work:
Error XS0664 Literal of type double cannot be implicitly converted to type 'decimal'; use an 'M' suffix to create a literal of this type Consolefloat2 C:DatenvulcantestConsolefloatConsolefloat2Program.prg 39
Arne
In VO we had the experience that mixing of types created many problems. Although most of the program worked with float, we tried to use real8 in some places and we had to change many real8 definitions back to float. I believe it was the combination together with usuals in the basic classes.
I have now made some performance tests in X# with the X# runtime, also mixing with usuals. I would summarize it as follows:
Although Real8 is the winner, using decimal performs still better than float and usual and has the precision advantages. Usual and float have nearly the same performance. So if trying to replace floats in old programs it can be a good idea to use decimals and not real8.
For using decimals I had to change the literals in the code which can create much work:
Error XS0664 Literal of type double cannot be implicitly converted to type 'decimal'; use an 'M' suffix to create a literal of this type Consolefloat2 C:DatenvulcantestConsolefloatConsolefloat2Program.prg 39
Arne
- Attachments
-
- testfloat.txt
- (2.84 KiB) Downloaded 82 times
-
- float.png (119.93 KiB) Viewed 578 times
Float, Real8, or Decimal?
Hi Arne, hi Robert,
it would be a good thing to have automatic conversions from float or real8 values to decimal.
Wolfgang
it would be a good thing to have automatic conversions from float or real8 values to decimal.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Float, Real8, or Decimal?
Wolfgang,
There is already an implicit conversion from decimal to float and back. You can see it in the file
https://github.com/X-Sharp/XSharpPublic/blob/feature/Runtime/Runtime/XSharp.VO/Types/Float.prg
For doubles we can't add these implicit operators (they are defined in the framework). We would have to change the compiler to silently do this. I will see if that can be done without breaking things.
Robert
There is already an implicit conversion from decimal to float and back. You can see it in the file
https://github.com/X-Sharp/XSharpPublic/blob/feature/Runtime/Runtime/XSharp.VO/Types/Float.prg
Code: Select all
static operator implicit(value as System.Decimal) as Float
return Float{ (real8) value, RuntimeState.Decimals}
static operator implicit(fl as Float) as System.Decimal
return (System.Decimal) fl:_value
Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
The Netherlands
robert@xsharp.eu
- lumberjack
- Posts: 727
- Joined: Fri Sep 25, 2015 3:11 pm
- Location: South Africa
Float, Real8, or Decimal?
Hi Arne,
I actually miss having a go at him these days. Dick too nice for such things...
Well, maybe we should convince Geoff Schaller to get involve in X#... Then every forum post will be a vvvvveeeeerrrryyyyy lllllloooooonnnnngggggggggg discussion....ArneOrtlinghaus wrote:I feel proud having created such a nice long discussion... B)
I actually miss having a go at him these days. Dick too nice for such things...
______________________
Johan Nel
Boshof, South Africa
Johan Nel
Boshof, South Africa
Float, Real8, or Decimal?
Hi Robert,
thank you very much!
Maybe it would be enough to make this work only for literals - if that is easier.
I'm thinking to change my variables from float to decimal in my VO applications after the migration is complete.
Wolfgang
thank you very much!
Maybe it would be enough to make this work only for literals - if that is easier.
I'm thinking to change my variables from float to decimal in my VO applications after the migration is complete.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Float, Real8, or Decimal?
Hello Johan,
One of these days I think I am going to call to see if he's interested....
DIck
Thank you for that although some people may disagree .I actually miss having a go at him these days. Dick too nice for such things...
One of these days I think I am going to call to see if he's interested....
DIck
Float, Real8, or Decimal?
Decimals have much higher precision and are usually used within financial applications that require a high degree of accuracy. Decimals are much slower (up to 20X times in some tests) than a double/float. Decimals and Floats/Doubles cannot be compared without a cast whereas Floats and Doubles can. Decimals also allow the encoding or trailing zeros.
- ArneOrtlinghaus
- Posts: 412
- Joined: Tue Nov 10, 2015 7:48 am
- Location: Italy
Float, Real8, or Decimal?
I am back with testing what is best for our programs:
Stay with floats or convert to another type, that's the question...
Currently it seems for me that the X#-Runtime fits best to real8 and it is less riskier to convert everything to real8 than to stay with the float.
I had now an issue using the function Seconds() together with float variables in x#.
Seconds return a real8 in X#, whereas it returns a usual in VO.
In my program I have something like:
local fsecstart := Seconds() as float
local fdiff as float
... do something
fdiff := Seconds()-fsecstart
// in the following condition the difference of the new seconds()
// minus the previous seconds() call converted to float was unexpectedly smaller than 0
// The reason seem to be the mixing of float and real8
// In the VO-program this was treated correctly, but also in the X#-Program converting everything to real8
if fdiff < 0
fdiff += 86400 // for midnight change
endif
- When looking at the function Val it seems to returns a real8 in case of decimals.
- Usuals seem to treat floats and real8 equally: The value assigned to seem to be returned without conversion.
What may be the main differences is the use of the NTRIM function (unfortunately used often), that may return more undesired decimals using Real8. But already in VO we had some timeconsuming issues where the expected number of decimals in float was not what was really in the variable.
Arne
Stay with floats or convert to another type, that's the question...
Currently it seems for me that the X#-Runtime fits best to real8 and it is less riskier to convert everything to real8 than to stay with the float.
I had now an issue using the function Seconds() together with float variables in x#.
Seconds return a real8 in X#, whereas it returns a usual in VO.
In my program I have something like:
local fsecstart := Seconds() as float
local fdiff as float
... do something
fdiff := Seconds()-fsecstart
// in the following condition the difference of the new seconds()
// minus the previous seconds() call converted to float was unexpectedly smaller than 0
// The reason seem to be the mixing of float and real8
// In the VO-program this was treated correctly, but also in the X#-Program converting everything to real8
if fdiff < 0
fdiff += 86400 // for midnight change
endif
- When looking at the function Val it seems to returns a real8 in case of decimals.
- Usuals seem to treat floats and real8 equally: The value assigned to seem to be returned without conversion.
What may be the main differences is the use of the NTRIM function (unfortunately used often), that may return more undesired decimals using Real8. But already in VO we had some timeconsuming issues where the expected number of decimals in float was not what was really in the variable.
Arne