如何检查浮子是否有两个有效数字？(How can I check if a float has two significant figures?)

 我正在编写一个单元测试来确定字符串值是否出现2个有效数字，即。 “N.NN”
    strokeValue = [NSString stringWithFormat:@"%.2f",someFloatValue];

如何编写断言的测试，我的字符串总是有2个小数位？ I am writing a unit test to determine if a string value appears with 2 significant figures, ie. "N.NN"
    strokeValue = [NSString stringWithFormat:@"%.2f",someFloatValue];

How can I write a test that asserts, my string always has 2 decimal places?原文：https://stackoverflow.com/questions/13220531

最满意答案

 由于您使用%.2f格式说明符格式化浮点值，因此根据定义，结果字符串将始终具有两个小数位。 如果someFloatValue为5，你将获得5.00。 如果someFloatValue是3.1415926，你将获得3.14。
没有必要进行测试。 对于给定的格式说明符，它始终是真的。
编辑：我发现你可能确实想要确认你使用的是正确的格式说明符。 检查结果字符串的一种方法是：
NSRange range = [strokeValue rangeOfString:@"."];
assert(range.location != NSNotFound && range.location == strokeValue.length - 3, @"String doesn't have two decimals places");
Since you are formatting a float value using the %.2f format specifier, by definition, the resulting string will always have two decimal places. If someFloatValue is 5 you will get 5.00. If someFloatValue is 3.1415926 you will get 3.14.
There is no need to test. It will always be true with the given format specifier.
Edit: It occurs to me that you may actually want to confirm that in fact you are using the correct format specifier. One way to check the resulting string would be:
NSRange range = [strokeValue rangeOfString:@"."];
assert(range.location != NSNotFound && range.location == strokeValue.length - 3, @"String doesn't have two decimals places");

2012-11-04

在Z3中有效地检查两个约束的句法等价性(Checking syntactic equivalence of two constraints efficiently in Z3)

Expr类派生自AST，它具有用于此目的的equals ， compareTo和hashCode函数。 由于Z3使用哈希引用，这是非常有效的，基本上只是比较指针（在Java中是一个类型）。 The Expr class derives from AST, which has equals, compareTo, and hashCode functions for this purpose. Since Z3 uses hash consing, this is very efficient, e ...