Gamestudio Links
Zorro Links
Newest Posts
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
Release 2.68 replacement of the .par format
by Martin_HH. 09/23/25 20:48
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
2 registered members (TipmyPip, AndrewAMD), 15,897 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 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: 594
Austria
Petra Offline
Support
Petra  Offline
Support

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