Skip to content

Commit

Permalink
IMP解决浮点数精度问题
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsonglei committed Sep 19, 2023
1 parent dd03adc commit 3eb3473
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ trait Rules extends DimRules {
pattern = List(isDimension(Numeral).predicate, "分之".regex, isPositive.predicate),
prod = tokens {
case Token(_, n1: NumeralData) :: _ :: Token(_, n2: NumeralData) :: _ =>
val v = if (n1.value == 0) 0 else n2.value / n1.value
val v = if (n1.value == 0) 0 else (BigDecimal.apply(n2.value)./(BigDecimal.apply(n1.value))).toDouble
fraction(v, n2.value, n1.value)
}
)
Expand All @@ -39,7 +39,7 @@ trait Rules extends DimRules {
pattern = List(isDimension(Numeral).predicate, "/".regex, isDimension(Numeral).predicate),
prod = tokens {
case Token(_, n1: NumeralData) :: _ :: Token(_, n2: NumeralData) :: _ =>
val v = if (n2.value == 0) 0 else n1.value / n2.value
val v = if (n2.value == 0) 0 else (BigDecimal.apply(n1.value)./(BigDecimal.apply(n2.value))).toDouble
fraction(v, n1.value, n2.value)
}
)
Expand All @@ -55,7 +55,7 @@ trait Rules extends DimRules {
case "" => if (neg) -1000.0 else 1000.0
case "" => if (neg) -10000.0 else 10000.0
}
fraction(n.value / scalar, n.value, scalar)
fraction((BigDecimal.apply(n.value)./(BigDecimal.apply(scalar))).toDouble, n.value, scalar)
}
)

Expand All @@ -68,7 +68,7 @@ trait Rules extends DimRules {
case "%" => 0.01
case "" => 0.001
}
fraction(n.value * scalar, n.value, 1/scalar)
fraction((BigDecimal.apply(n.value).*(BigDecimal.apply(scalar))).toDouble, n.value, 1/scalar)
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ object Examples extends DimExamples {
override def pairs: List[(Types.ResolvedValue, List[String])] =
List(
(0.6, 60.0, 100.0, List("百分之六十")),
(0.7, 70.0, 100.0, List("70%")),
(0.6, 600.0, 1000.0, List("千分之六百")),
(0.75, 3.0, 4.0, List("4分之3", "四分之3", "3/4")),
(0.5, 64.0, 128.0, List("一百二十八分之64")),
Expand Down

0 comments on commit 3eb3473

Please sign in to comment.