Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (VoroneTZ, monk12, Quad), 829 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 2 of 4 1 2 3 4
Re: file_write var array ? [Re: laz] #477977
08/20/19 13:24
08/20/19 13:24
Joined: Feb 2017
Posts: 1,718
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,718
Chicago
A var is actually a double. The characteristics of computer mathematics affect how doubles add and subtract. This has nothing to do with Zorro.

Re: file_write var array ? [Re: laz] #477981
08/20/19 17:18
08/20/19 17:18
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

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=PZRI1IfStY0
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

I 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.

Code
> 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
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 73
berlin
Guys, I'm really sorry - but I still need help with that crazy...

I read all the links (and much more):

https://en.wikipedia.org/wiki/Floating-point_arithmetic
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
https://opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=467836
https://zorro-project.com/manual/en/aarray.htm
https://zorro-project.com/manual/en/format.htm

Code
var 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.htm
Type 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?

Quote
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.htm

But double to float?
Quote
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?

Quote
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-numbers

I'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] #478065
08/31/19 01:33
08/31/19 01:33
Joined: Oct 2018
Posts: 72
J
JamesHH Offline
Junior Member
JamesHH  Offline
Junior Member
J

Joined: Oct 2018
Posts: 72
Actually, I noticed something very similar the other day: I was working with type var and they only seemed to have float precision instead of double, which is concerning.

I will test some more when I have time.

You can do arithmetic operations instead of printf to test the accuracy. For example, you can subtract close values, like 1.00000001 - 1.0,
and check the result which would be 0 for type float but >0 for type double.

Re: file_write var array ? [Re: laz] #478066
08/31/19 11:42
08/31/19 11:42
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

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] #478080
09/04/19 05:18
09/04/19 05:18
Joined: Oct 2018
Posts: 72
J
JamesHH Offline
Junior Member
JamesHH  Offline
Junior Member
J

Joined: Oct 2018
Posts: 72
There seems to be two different possibilities here. 1) The value in memory is double precision, but is being printed as float. This would be strange. printf has been part of C for decades and the format string is standard. I can't remember, but you can look up how to print a specified number of digits after the decimal point. Something like %f.12 or %f12, I can't recall ...

2) The value in memory is only floating point precision.

Have you ruled out 2?

Re: file_write var array ? [Re: laz] #478087
09/05/19 22:19
09/05/19 22:19
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

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.

Code
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
J
JamesHH Offline
Junior Member
JamesHH  Offline
Junior Member
J

Joined: Oct 2018
Posts: 72
Originally Posted by laz
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

Code
var t1 = 0.1;


with

Code
float t1 = 0.1;
?

Re: file_write var array ? [Re: laz] #478109
09/09/19 09:19
09/09/19 09:19
Joined: Jul 2000
Posts: 27,977
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,977
Frankfurt
The types var and double are identical. They have 64 bit precision, while float has 32 bit precision.

Re: file_write var array ? [Re: jcl] #478133
09/11/19 16:47
09/11/19 16:47
Joined: Oct 2018
Posts: 72
J
JamesHH Offline
Junior Member
JamesHH  Offline
Junior Member
J

Joined: Oct 2018
Posts: 72
Originally Posted by jcl
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!!

Page 2 of 4 1 2 3 4

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1