Pop Quizzzzzzzz

Created at 05 Jan 2014, 12:17
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
jeex's avatar

jeex

Joined 18.10.2013

Pop Quizzzzzzzz
05 Jan 2014, 12:17


A short Pop quizzzzz:

1. What is the output of?

double r = 5 / 100;
Print(r.ToString());

 


@jeex
Replies

atrader
07 Jan 2014, 11:17

try:

double r = 5.0 / 100;
Print(r);

 


@atrader

jeex
07 Jan 2014, 16:55

t

aTrader: that's exactly my point.

It's absolutely absurd that r = 0, while r is a double. If i want r to be have the value that suits a double, i have to typecast both numerator and denominator.

double r = (double)x / (double) y; 

I do not know of one language that does not use / as a simple devider with a double as result, but makes it as ambiguous as c#.


@jeex

modarkat
08 Jan 2014, 08:54

It is a common behaviour for most languages. It will work the same way in: C, C++, MQL

 


@modarkat

hichem
08 Jan 2014, 11:46

RE: t

If you knew how compilers work you wouldn't find it absurd. because x / y is evaluated before r. The compiler doesn't care if the result will be affected to a double when calculating x/y, for the compiler x/y is a division of two integers, thus the result should be an integer.

jeex said:

aTrader: that's exactly my point.

It's absolutely absurd that r = 0, while r is a double. If i want r to be have the value that suits a double, i have to typecast both numerator and denominator.

double r = (double)x / (double) y; 

I do not know of one language that does not use / as a simple devider with a double as result, but makes it as ambiguous as c#.

 


@hichem

jeex
08 Jan 2014, 12:18

I'm awfully sorry for my ignorance. I've only been in the software development business for 29 years, but never worked with Microsoft Visual or C.


@jeex