Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (1 invisible), 735 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
type casting issue #475532
12/19/18 12:44
12/19/18 12:44
Joined: Jul 2016
Posts: 51
H
hatten Offline OP
Junior Member
hatten  Offline OP
Junior Member
H

Joined: Jul 2016
Posts: 51
The following type casting issue really took me off-guard and I wonder whether this is expected behaviour. It can certainly cause trouble, if one is unaware of it (zorro S 1.96.4):

The following simple script illustrates what type casting produces for Tradesize = 0.01:

var lotstotrade;
var Tradesize;
int ilots;
int jlots;
int klots;
int llots;
int mlots;

Tradesize = 0.01;
lotstotrade = 100.0*Tradesize;

ilots = (int)lotstotrade;
jlots = (int)(100.0*Tradesize);

klots = lotstotrade;
llots = 100.0 * Tradesize;
mlots = 1000.0 * Tradesize;

int roundlots;
int ceillots;

roundlots = round(lotstotrade,1);
ceillots = ceil(lotstotrade);

printf("nTradesize %f lotstotrade %.4f ilots %i jlots %i klots %i mlots %i llots %i",Tradesize,lotstotrade,ilots,jlots,klots,llots,mlots);

printf("nroundlots %i ceillots %i",roundlots,ceillots);

The output will be as follows:

Tradesize 0.010000 lotstrade 1.0000 ilots 0 jlots 0 klots 0 llots 0 mlots 9

roundlots 1
ceillots 1

Only by using round and ceil you get the proper value!
ilots, jlots, klots llots and also mlots are wrong.

If you set Tradesize = 0.03 then all the results are correct
For the first printf statement you will get
Tradesize 0.030000 lotstrade 3.0000 ilots 3 jlots 3 klots 3 llots 3 mlots 30

I assume it is related to how floats/double are stored in memory, but is this behaviour really expected?!!

Re: type casting issue [Re: hatten] #475539
12/20/18 11:54
12/20/18 11:54
Joined: Apr 2008
Posts: 586
Austria
Petra Offline
Support
Petra  Offline
Support

Joined: Apr 2008
Posts: 586
Austria
Yes its related to that variables are stored in only 8 bytes in computer memory.

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


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1