当将“链式”双发电机与jqwik一起使用时,我会得到一个缩放错误消息java.util.concurrent.ExecutionException: net.jqwik.api.JqwikException: Decimal value -1.6099999999999999 cannot be represented with scale 4.。
你能给我提供一些关于如何设置这个尺度和这个参数的含义的一些细节吗?
下面是我使用的生成器函数:
@Provide("close doubles")
Arbitrary<Tuple.Tuple2<Double,Double>> closeDoubles(@ForAll() Double aDouble) {
return Arbitraries.doubles()
.between(aDouble-2.5, aDouble+2.5)
.withSpecialValue(aDouble)
.ofScale(4)
.map(num ->Tuple.of(aDouble,num));
}然后将其组合成一个业务对象实例。
我的最终目标是产生两个彼此“接近”的双打(这里的距离是2.5)。
发布于 2022-06-24 08:38:57
您所遇到的问题是由于双重数字的舍入错误,以及jqwik严格要求只允许符合指定比例的上、下边界。
我看到了几种解决这一问题的方法,一种是使用BigDecimals进行生成,然后将它们映射为双倍。这可能看起来像开销,但实际上并不是因为jqwik在幕后所做的事情。这看起来可能是:
@Provide
Arbitrary<Tuple.Tuple2<Double, Double>> closeDoubles(@ForAll @Scale(4) BigDecimal aBigDecimal) {
BigDecimal twoPointFive = new BigDecimal("2.5");
return Arbitraries.bigDecimals().between(aBigDecimal.subtract(twoPointFive), aBigDecimal.add(twoPointFive))
.ofScale(4)
.map(num -> Tuple.of(aBigDecimal.doubleValue(), num.doubleValue()));
}请注意,原始数字也应该使用与目标值相同的标度,否则它的默认标度为2。
就我个人而言,我更愿意生成一个数字和增量,它改进了收缩行为,并且将更经常地创建一个具有相同数字的元组:
@Provide
Arbitrary<Tuple.Tuple2<Double, Double>> closeDoubles2(@ForAll @Scale(4) BigDecimal aBigDecimal) {
BigDecimal twoPointFive = new BigDecimal("2.5");
return Arbitraries.bigDecimals().between(twoPointFive.negate(), twoPointFive)
.ofScale(4)
.map(num -> Tuple.of(aBigDecimal.doubleValue(), aBigDecimal.add(num).doubleValue()));
}https://stackoverflow.com/questions/72740761
复制相似问题