Gamestudio Links
Zorro Links
Newest Posts
loading historical data 1st time
by AndrewAMD. 04/14/23 12:54
Trade at bar open
by juanex. 04/13/23 19:43
Bug in Highpass2 filter
by rki. 04/13/23 09:54
Adding Limit Orders For IB
by scatters. 04/11/23 16:16
FisherN
by rki. 04/11/23 08:38
AUM Magazine
Latest Screens
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Hecknex World
Who's Online Now
4 registered members (AndrewAMD, fogman, Grant, juanex), 972 guests, and 7 spiders.
Key: Admin, Global Mod, Mod
Newest Members
rki, FranzIII, indonesiae, The_Judge, storrealba
18919 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: 581
Austria
Petra Offline
Support
Petra  Offline
Support

Joined: Apr 2008
Posts: 581
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