[英]Multiplying two double value gives negative number in flutter
I need to multiply two large numbers for example,例如,我需要将两个大数相乘,
double x = 318191400000;
double result =x*x;
But i am getting negative value for this when building in flutter .但是在构建 flutter 时,我得到了负值。
Please help me on this.请帮我解决这个问题。
[1]: https://i.stack.imgur.com/eyxJ4.png [1]:https://i.stack.imgur.com/eyxJ4.png
You're not actually multiplying two double
s here, but two int
s which is overflowing the 64-bit integer resulting in a negative number.您实际上不是在这里乘以两个
double
,而是两个int
溢出 64 位整数,从而导致负数。
With doubles:双打:
void main() {
double x = 318191400000;
print(x*x); // Result: 1.0124576703396e+23
}
With ints:使用整数:
void main() {
int x = 318191400000;
print(x*x); // Result: -8411186631728820224
}
If you ever print a double to the console, you'll always see it displayed in either scientific notation (for extremely large or small values) or with a decimal point with at least one trailing digit.如果您曾在控制台上打印双精度数,您总会看到它以科学记数法(对于极大或极小值)或带有至少一个尾随数字的小数点显示。
Finally i have found solution and sharing here for anyone having these kind of issues,最后,我找到了解决方案并在此分享给遇到此类问题的任何人,
xValues[index].toDouble() * yValues[index].toDouble()
This gives the expected result which is 1.0124576703396e+23
这给出了预期的结果,即
1.0124576703396e+23
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.