Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NumUtil.divide 存在的问题 #47

Open
LiWenHui96 opened this issue Feb 14, 2022 · 4 comments
Open

NumUtil.divide 存在的问题 #47

LiWenHui96 opened this issue Feb 14, 2022 · 4 comments

Comments

@LiWenHui96
Copy link

NumUtil.divide 方法,当 a/b = 1/3 时,

报错信息如下:

'package:decimal/decimal.dart': Failed assertion: line 27 pos 38: '_rational.hasFinitePrecision': is not true.

image

使用 toDecimal() 方法需要添加 scaleOnInfinitePrecision 参数

@pyzworld
Copy link

pyzworld commented Mar 8, 2022

NumUtil.divide(3.14, 180);

3.14 / 180 也会出现这样的问题。

貌似只要小数点比较多就会出现:
'package:decimal/decimal.dart': Failed assertion: line 27 pos 38: '_rational.hasFinitePrecision': is not true.

@LiWenHui96 LiWenHui96 reopened this Mar 9, 2022
@LiWenHui96
Copy link
Author

是无理数的情况下,才会出现

@purgeyao
Copy link

找到解决方案了么

@LiWenHui96
Copy link
Author

找到解决方案了么

我想你可以自己去重写除的方法,去加上 scaleOnInfinitePrecision 这个参数,当然这里也有我的一份作为参考。

  double divide(num? num1, num? num2, {int? scaleOnInfinitePrecision) {
    final Rational value = Decimal.parse(num1.toString()) / Decimal.parse(num2.toString());
    return value
        .toDecimal(scaleOnInfinitePrecision: scaleOnInfinitePrecision)
        .toDouble();
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants