0 registered members (),
975
guests, and 2
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: file_write var array ?
[Re: laz]
#477981
08/20/19 17:18
08/20/19 17:18
|
Joined: Jan 2019
Posts: 73 berlin
laz
OP
Junior Member
|
OP
Junior Member
Joined: Jan 2019
Posts: 73
berlin
|
It means (if i understand right) that if i do this: my_array[2] = 3000.0003; The internal value of my_array[2] is 3000.00024414 ? And that is why (3000.0003 (real 3000.00024414) - my_array[2] (real 3000.00024414)) == 0 ? That helped me a bit: https://www.youtube.com/watch?v=PZRI1IfStY0https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.htmlI think I got it (in parts), I have to admit that I have some knowledge gaps in this area. I keep reading about it, and while I'm doing this i can see why i never had problems with it. I always set a precision (like in R options(digits = 12)) and round to the accuracy i need, and i normally never do this if (double == double) without rounding. > R-CODE
options(digits = 12)
> a<-3000.0003
> a
[1] 3000.0003
> options(digits = 15)
> a<-3000.0003
> a
[1] 3000.0003
> options(digits = 16)
> a<-3000.0003
> a
[1] 3000.0003
> options(digits = 17)
> a<-3000.0003
> a
[1] 3000.0003000000002
> options(digits = 18)
> a<-3000.0003
> a
[1] 3000.00030000000015
> options(digits = 20)
> a<-3000.0003
> a
[1] 3000.0003000000001521 Why is R not adding anything until digits >= 17? Or better: Why is the value in C-Lite 3000.00024414 and in R 3000.0003>>00000000<< up to digits = 16? How can i influence this in C-lite? Because at this point rounding(3000.00024414, to 4 digits) will result in 3000.0002, not 3000.0003 - and in R that works fine? Many thanks for helping me with that basic stuff!
Last edited by laz; 08/21/19 01:02.
|
|
|
Re: file_write var array ?
[Re: laz]
#478045
08/29/19 12:27
08/29/19 12:27
|
Joined: Jan 2019
Posts: 73 berlin
laz
OP
Junior Member
|
OP
Junior Member
Joined: Jan 2019
Posts: 73
berlin
|
Guys, I'm really sorry - but I still need help with that ... I read all the links (and much more): https://en.wikipedia.org/wiki/Floating-point_arithmetichttps://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.htmlhttps://opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=467836https://zorro-project.com/manual/en/aarray.htmhttps://zorro-project.com/manual/en/format.htmvar t1 = 0.1;
file_append(tfn,strf("%i,%.14f,%.14f,%.14f\n",Bar,t1,(var)t1,(double)t1),0);
result: 10,0.10000000149012,0.10000000149012,0.10000000149012 https://zorro-project.com/manual/en/aarray.htmType double, var | Size (bytes) 8 | Digits ~14 http://www.binaryconvert.com/result_float.html?decimal=048046049 | float(.1) | 0.100000001490116119384765625 http://www.binaryconvert.com/result_double.html?decimal=048046049 | double(.1) | 0.1000000000000000055511151231257827021181583404541015625 Why does file_append/strf write var/double as floats?As the printf class of functions are variadic (denoted by the elipsis „…“ in the function signature) the default argument promotions take place. Finally the call printf("float = %f\n", f) will after promotion look like printf("float = %f\n", (double)f) and printf can safely assume that every %f, %g, and %e denotes a parameter of type double. https://blogs.fau.de/wittmann/2013/11/c-default-argument-promotions/ Why does printf print doubles as a float?"%f Floating-point number (var, double) " https://zorro-project.com/manual/en/format.htm"The printf function does not automatically promote float to double." https://manual.zorro-project.com/printf.htmBut double to float? The %f format specifier for the printf/scanf class of functions denotes a variable of type float which consumes 32 bits. In the example above d is of type double which uses 64 bits and actually requires %lf. The provided argument d (64 bits) would be interpreted as a variable of type float (32 bits). https://blogs.fau.de/wittmann/2013/11/c-default-argument-promotions/ %lf does not exist in clite or does it?Rounding errors occur when a number can’t be stored precisely. This can happen even with simple numbers, like 0.1. Therefore, rounding errors can, and do, happen all the time. A corollary of this rule is: never use floating point numbers for financial or currency data. https://www.learncpp.com/cpp-tutorial/floating-point-numbersI'm clearly missing something here and i don't get it... Many thanks!
Last edited by laz; 08/29/19 22:56.
|
|
|
Re: file_write var array ?
[Re: laz]
#478066
08/31/19 11:42
08/31/19 11:42
|
Joined: Jan 2019
Posts: 73 berlin
laz
OP
Junior Member
|
OP
Junior Member
Joined: Jan 2019
Posts: 73
berlin
|
Thanks for joining james...
Either I have overlooked it or it is nowhere mentioned that data written/printed will be transformed and thereby lose precision. The R-bridge also sends data (double) different from those written by Zorro (float) in text files.I have now read more intensively about the topic and unfortunately there are many pitfalls, I would find it easier and better, if we can determine the precision.
Example:% f for floats% lf or something for doubles
How to print/write data with more precision?
I can't use printf, strf, file functions and so on or is that wrong - or is it possible and I'm just too stupid?
Last edited by laz; 09/03/19 19:47.
|
|
|
Re: file_write var array ?
[Re: laz]
#478087
09/05/19 22:19
09/05/19 22:19
|
Joined: Jan 2019
Posts: 73 berlin
laz
OP
Junior Member
|
OP
Junior Member
Joined: Jan 2019
Posts: 73
berlin
|
1) The value in memory is double precision, but is being printed as float.^^^ I think that's it... "This would be strange. printf has been part of C for decades and the format string is standard."That was the reason why i was confused - i did not expect that - apart from my knowledge gaps in floating point arithmetic. The manual says: "For printing float variables with the %f placeholder, typecast them to (var) or (double) (f.i. printf("Profit: %.2f",(var)TradeProfit);). " But as you can see in my examples, typecasting to (var) or (double) does not work. var t1 = 0.1;
file_append("Data/test-write.csv",strf("%i,%.14f,%.14f,%.14f\n",Bar,t1,(var)t1,(double)t1),0);
result: 10,0.10000000149012,0.10000000149012,0.10000000149012 If i printf a (var) I'll expect a (var==double) to be printed, precision depending on the format string like "%.8f"... Even writing into a csv with double precision is not possible at the moment - or am I wrong? Maybe jcl can explain it in easy words, what's correct and why it is like it is?
Last edited by laz; 09/05/19 22:39.
|
|
|
Re: file_write var array ?
[Re: laz]
#478094
09/06/19 16:47
09/06/19 16:47
|
Joined: Oct 2018
Posts: 72
JamesHH
Junior Member
|
Junior Member
Joined: Oct 2018
Posts: 72
|
1) The value in memory is double precision, but is being printed as float.
^^^ I think that's it...
Well, as far as I can tell you have not confirmed that this is the case. If t1 in your code snippet was a float, then your output would be exactly as expected. What happens if you replace with ?
|
|
|
Re: file_write var array ?
[Re: jcl]
#478133
09/11/19 16:47
09/11/19 16:47
|
Joined: Oct 2018
Posts: 72
JamesHH
Junior Member
|
Junior Member
Joined: Oct 2018
Posts: 72
|
The types var and double are identical. They have 64 bit precision, while float has 32 bit precision. They are identical, but in Zorro scripts double is *not* an IEEE754 Double precision 64-bit. The following script demonstrates a double that is being represented the same as an IEEE754 32-bit float: function main() { double x = 1.00000001; if(x == 1.0) { printf("\nx is float"); } else { printf("\nx is double"); } } Can you please clarify what is going on? This is very serious!!
|
|
|
|