Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (dr_panther, 7th_zorro), 1,203 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
ifelse's strange perception of zero #455947
11/04/15 00:20
11/04/15 00:20
Joined: Dec 2013
Posts: 568
Fuerth, DE
Sphin Offline OP
User
Sphin  Offline OP
User

Joined: Dec 2013
Posts: 568
Fuerth, DE
Code:
function main() {
	var a = 1.0;
	var b = ifelse(a > 0, a, 0);
	printf("b = %.1f",b);
}

Result:
b = 1.0

Code:
function main() {
	var a = 0.9;
	var b = ifelse(a > 0, a, 0);
	printf("b = %.1f",b);
}

Result:
b = 0.0

means: 0.9 > 0 is not true. I spent hours to search for an error in my script ... laugh

Re: ifelse's strange perception of zero [Re: Sphin] #455950
11/04/15 09:01
11/04/15 09:01
Joined: Apr 2015
Posts: 20
Vietnam
F
Florastamine Offline
Newbie
Florastamine  Offline
Newbie
F

Joined: Apr 2015
Posts: 20
Vietnam
printf() doesn't take var/float arguments. You need to explicitly cast it to double.

Code:
printf( "b = %.1f", (double) b );


Re: ifelse's strange perception of zero [Re: Florastamine] #455952
11/04/15 09:39
11/04/15 09:39
Joined: Dec 2013
Posts: 568
Fuerth, DE
Sphin Offline OP
User
Sphin  Offline OP
User

Joined: Dec 2013
Posts: 568
Fuerth, DE
From the manual (printf/print/msg):

Quote:
For printing float variables with the %f placeholder, typecast them to (var).


For my variables are already declared as var I did not typecast them. What's right now?

Re: ifelse's strange perception of zero [Re: Sphin] #455957
11/04/15 16:39
11/04/15 16:39
Joined: Jun 2013
Posts: 1,609
D
DdlV Offline
Serious User
DdlV  Offline
Serious User
D

Joined: Jun 2013
Posts: 1,609
Hi Sphin,

I haven't tested, but a > 0.? On the assumption that your a > 0 is converting a to int before the comparison?

Regards.

Re: ifelse's strange perception of zero [Re: DdlV] #455958
11/04/15 16:59
11/04/15 16:59
Joined: Dec 2013
Posts: 568
Fuerth, DE
Sphin Offline OP
User
Sphin  Offline OP
User

Joined: Dec 2013
Posts: 568
Fuerth, DE
Oh my God ... I found the solution, thanks for the hint DdlV that drove me the right direction.

It is indeed the '0' in the comparism that converts a to an int. If this is not wanted you have to write:

var b = ifelse(a > 0, a, 0.); <- it is the '.' behind the 0 that makes the difference.

I actually need more time to get along with the programming language than to develop trading strategies ...

Re: ifelse's strange perception of zero [Re: Sphin] #455996
11/05/15 11:11
11/05/15 11:11
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
The "a > 0" should also be "a > 0.". It does not matter here, but it's better for always being on the safe side.

In a programming language, 0.9 is greater that 0.8, but it is not greater than 0 . When mixing integers and floats one must be very careful, so avoid mixing when possible. The compiler automatically converts different types, and mostly in the way that you want, but sometimes not.

Re: ifelse's strange perception of zero [Re: jcl] #456021
11/05/15 23:43
11/05/15 23:43
Joined: Dec 2013
Posts: 568
Fuerth, DE
Sphin Offline OP
User
Sphin  Offline OP
User

Joined: Dec 2013
Posts: 568
Fuerth, DE
Strict distinction of the different types of variables and the consequences thereof are quite new for me switching from Perl to C.

Code:
Returns:
x when c is true, otherwise y. 
Remarks:
The returned variable type depends on the type of the arguments;[...]



Of course it's objectively right, but I thought if x is a var and y is an int then if c is true the returned variable is a var, otherwise it is an int. I never have guessed that the arguments themselves have been touched for conversion to either int or var.


Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1