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 (Edgar_Herrera, VoroneTZ, Akow), 973 guests, and 4 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
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: 585
Austria
Petra Offline
Support
Petra  Offline
Support

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