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 (AndrewAMD, Nymphodora, Quad), 923 guests, and 5 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 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: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 73
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: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 73
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,977
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,977
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: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 73
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: 390
Czech Republic
G
Grat Offline
Senior Member
Grat  Offline
Senior Member
G

Joined: May 2015
Posts: 390
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: 390
Czech Republic
G
Grat Offline
Senior Member
Grat  Offline
Senior Member
G

Joined: May 2015
Posts: 390
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: 390
Czech Republic
G
Grat Offline
Senior Member
Grat  Offline
Senior Member
G

Joined: May 2015
Posts: 390
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: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 73
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,977
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,977
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: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 73
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

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1