简体   繁体   中英

Double variables give unexpected result

I am not sure about what is going on with these two variables. "milliV" is a double, "param1a" is a double, being param1a= 65, I should get out 1.588. The result I get instead is = 0 . How is that possible?

  double milliV=0.0;
  milliV= (5/1023*param1a/200*1000);

在此输入图像描述

Because 5/1023 that's an integer division so the result is integer too. Try with this

milliV= (5.0/1023*param1a/200.0*1000);

As long as one of the elements of the operation is double , the result will be double too

5/1023是问题5,1023是整数,因此5/1023 = 0

it looks that 5/1023 = 0 since both 5 and 1023 are integers

try change 5/1023*param1a/200*1000 into

5.0 / 1023.0 * param1a / 200.0 * 1000.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM