Gamestudio Links
Zorro Links
Newest Posts
Can't plot daily high & low
by Zheka. 10/14/19 18:39
TWS "Market closed" fix
by jcl. 10/14/19 15:09
2xCapital slider to increase size
by jcl. 10/14/19 14:03
Imgui Lite-c Development
by txesmi. 10/13/19 09:40
A9
by Dooley. 10/11/19 18:53
Need Help Finding Cause of Crash
by Dooley. 10/11/19 18:51
What algorithm is used for BALANCED?
by JamesHH. 10/11/19 17:32
AUM Magazine
Latest Screens
The Space Between
Pogostuck: Rage With Your Friends
Worst Case Z
AckCon'18 - Lotter vs the World 2 - Preview Release
Who's Online Now
7 registered members (exile, Dooley, Dalla, kvm, sdh309795gaas, jibanes, Zheka), 661 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
FrankW, TedMar, raimucuk, vivaldi2, filip
18304 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 4 1 2 3 4
file_write var array ? #477896
08/12/19 19:00
08/12/19 19:00
Joined: Jan 2019
Posts: 71
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 71
berlin
I need to use an array to store some data and save them to file.

But i normally not use all slots, sometimes maybe 3, 50 or 77, max 100...

How do i save the needed amount (3 in this example) of var values to file?
Code
function run() {
	
	string my_file = "Data\\FAA_TEST.csv";
	
        int  ar_used = 3;
	var my_array[100];	
	
	my_array[0] = 1000.0001;
	my_array[1] = 2000.0002;
	my_array[2] = 3000.0003;	
	
	file_delete(my_file);	
	file_write(my_file,my_array,ar_used*8);
		
	var my_read[100];
	
	file_read(my_file,my_read,10*8);
	
	int i;

	for(i=0;i<10;i++) printf("\nmy_array[%i] %f | my_read[%i] %f",i,my_array[i],i,my_read[i]);
	
	quit();
	
}
Is it correct this way or are there better ways?
Quote
my_array[0] 1000.000122 | my_read[0] 1000.000122
my_array[1] 2000.000244 | my_read[1] 2000.000244
my_array[2] 3000.000244 | my_read[2] 3000.000244
Later i want to read them, why does this 1000.0001>22< happen?

Thanks!

Last edited by laz; 08/12/19 21:30.
Re: file_write var array ? [Re: laz] #477899
08/12/19 21:43
08/12/19 21:43
Joined: Jan 2019
Posts: 71
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 71
berlin
I think i'll use the data functions for that wink

https://manual.zorro-project.com/data.htm

But, why does this 1000.0001>22< happen?

Last edited by laz; 08/12/19 21:45.
Re: file_write var array ? [Re: laz] #477907
08/13/19 15:24
08/13/19 15:24
Joined: Jul 2000
Posts: 27,029
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,029
Frankfurt
Because computer math is slightly different than the math at school, due to the finite precision of variables.

https://en.wikipedia.org/wiki/Floating-point_arithmetic

Re: file_write var array ? [Re: laz] #477918
08/14/19 02:34
08/14/19 02:34
Joined: Jan 2019
Posts: 71
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 71
berlin
Autsch... Thanks jcl crazy

Sorry but nobody has teached me computer math. I've taught myself what I know about programming, and sometimes I just miss things.

NEW MINIMAL EXAMPLE
Code
function run() {
		
	int i;
	var my_array[10];
	
	my_array[0] = 1000.0111;
	my_array[1] = 2000.0022;
	my_array[2] = 3000.0003;	
	
	watch("test0 = ",(1000.0111 - my_array[0]) == 0.0);
	watch("test1 = ",(2000.0022 - my_array[1]) == 0.0);
	watch("test2 = ",(3000.0003 - my_array[2]) == 0.0);
	
	for(i=0;i<4;i++) printf("\nmy_array[%i] %f",i,my_array[i]);
	
	quit();
	
}
Quote
file-append-array2 compiling...........
test0 = 1
test1 = 1
test2 = 1
my_array[0] 1000.011108
my_array[1] 2000.002197
my_array[2] 3000.000244
my_array[3] 0.000000
Quit
test0,1,2 are TRUE. I would think, that my printing is wrong - but what is wrong?

If i round 3000.000244 to 4 digits, it is 3000.0002 and not 3000.0003, why the initial zero (0) of my_array[3] stays unchanged and the 3000.0003 changes to 3000.000244?

Even if I ask a simple or stupid question, it would be nice if someone answers ...

Thanks for your help...

Last edited by laz; 08/15/19 14:44.
Re: file_write var array ? [Re: laz] #477945
08/17/19 11:24
08/17/19 11:24
Joined: May 2015
Posts: 163
Czech Republic
Grat Offline
Member
Grat  Offline
Member

Joined: May 2015
Posts: 163
Czech Republic
I make small modification
Code
function main() {
		
	int i;
	//var *my_array=zalloc(4*sizeof(var));
	var my_array[4]={0.0,0.0,0.0,0.0};
  
       for(i=0;i<4;i++) printf("\nmy_array[%i] %.5f",i,my_array[i]);
	
	my_array[0] =(var) 1000.0111000;
	my_array[1] =(var) 2000.0022000;
	my_array[2] =(var) 3000.0003000;	
	
	var n=0;
	for(i=0;i<4;i++){ 
            printf("\nmy_array[%i] %.8f",i,my_array[i]);
            n+=my_array[i];
       }
       printf("\n%f ",n);
       for(i=0;i<4;i++) printf("\nmy_array[%i] %.8g",i,my_array[i]);
	
}


output...

Quote

my_array[1] 0.00000
my_array[2] 0.00000
my_array[3] 0.00000
my_array[0] 1000.01110840
my_array[1] 2000.00219727
my_array[2] 3000.00024414
my_array[3] 0.00000000
6000.013550
my_array[0] 1000.0111
my_array[1] 2000.0022
my_array[2] 3000.0002
my_array[3] 0



So, i thinking something is a wrong.

1000.0111+2000.0022+3000.0002 = 6000.013600

but no 6000.013550


Last edited by Grat; 08/17/19 11:37.
Re: file_write var array ? [Re: laz] #477946
08/17/19 11:34
08/17/19 11:34
Joined: May 2015
Posts: 163
Czech Republic
Grat Offline
Member
Grat  Offline
Member

Joined: May 2015
Posts: 163
Czech Republic
in the python:
Code
my_array = [1000.0111000, 2000.0022000, 3000.0003000]
n=0
for x in my_array:
    print ("{:.9f}".format(x),)
    n+=x
print ("{:.9f}".format(n),)


is output:
1000.011100000
2000.002200000
3000.000300000
6000.013600000

Re: file_write var array ? [Re: laz] #477947
08/17/19 11:47
08/17/19 11:47
Joined: May 2015
Posts: 163
Czech Republic
Grat Offline
Member
Grat  Offline
Member

Joined: May 2015
Posts: 163
Czech Republic
Ok, next test:

Code
function main() {
		
  double d3=100.0003;
  double d2=1.0002;
  printf("\n%f+ %f= %f",d3,d2,d3+d2);

  var v3=100.0003;
  var v2=1.0002;
  printf("\n%f+ %f= %f",v3,v2,v3+v2);

  float f3=100.0003;
  float f2=1.0002;
  printf("\n%f+ %f= %f",f3,f2,f3+f2);
  
}


with output:


test1 compiling................ ok

100.000298+ 1.000200= 101.000498
100.000298+ 1.000200= 101.000498
0.007825+ -2.000000= -26815622246983973000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000

Re: file_write var array ? [Re: laz] #477954
08/19/19 12:33
08/19/19 12:33
Joined: Jan 2019
Posts: 71
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 71
berlin
Thanks grat for testing, there is a small typo in your first post:

Quote
1000.0111+2000.0022+3000.0002 = 6000.013600

1 000.0111 + 2 000.0022 + 3 000.0003 = 6 000.0136

What are we missing jcl?

Last edited by laz; 08/19/19 12:40.
Re: file_write var array ? [Re: laz] #477971
08/20/19 09:03
08/20/19 09:03
Joined: Jul 2000
Posts: 27,029
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,029
Frankfurt
I don't even know what the problem is, let alone what you are missing.

Re: file_write var array ? [Re: laz] #477974
08/20/19 12:54
08/20/19 12:54
Joined: Jan 2019
Posts: 71
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 71
berlin
The question is why Zorro shows changed values:

my_array[0] 1000.01110840
my_array[1] 2000.00219727
my_array[2] 3000.00024414

instead of

my_array[0] = 1000.0111;
my_array[1] = 2000.0022;
my_array[2] = 3000.0003;

Your answer was :

Quote
Because computer math is slightly different than the math at school, due to the finite precision of variables.

https://en.wikipedia.org/wiki/Floating-point_arithmetic


I don't understand that, can you please explain?

Page 1 of 4 1 2 3 4

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1