Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

error when assigning result of a division operation
 Post subject: error when assigning result of a division operation Post rating: 0   Post Posted: Sun 25 Nov, 2012, 23:56 
User avatar

User rating: 1
Joined: Wed 06 Jul, 2011, 23:12
Posts: 42
Location: Romania, Bucharest
Dear Support,

Sorry for this basic question, but I just can not understand why the following lines of code returns the result "k=0.0"

double k;
k=2/(6+1);
print("k="+k);

I would expect k=0.28571428571428571428571428571429 (I use this for the calculation of a 6 period exponential moving average) but instead it returns 0.


Thank you very much in advance!


 
 Post subject: Re: error when assigning result of a division operation Post rating: 0   Post Posted: Mon 26 Nov, 2012, 08:23 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
iulian wrote:
I just can not understand why the following lines of code returns the result "k=0.0
Because java performs integer division with integers, you either need to operate with double values or make explicit cast, thus either k=2d/(6d+1d); or k=2.0/(6.0+1.0);


 

Jump to:  

  © 1998-2026 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com